r/JavaFX • u/_dk7 • Jul 30 '24
Help Need help with styling JavaFX TableView
Hello everyone!!
I need help styling the table view in JavaFX. So what I want is essentially after creating a TableView, someone can set the following to true or false:
table.getSelectionModel().setCellSelectionEnabled(false);
Now, irrespective of what the user has set above, I want the row highlighting to come up along with the cell that user has selected to be highlighted in blue. Something like this:

Now, after referring in the internet and going around, I have the following code:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;
import javafx.util.Callback;
import java.util.Objects;
import java.util.Random;
public class JavaFXTables extends Application {
public static class Person {
private final SimpleStringProperty[] columns;
private Person(int numColumns) {
columns = new SimpleStringProperty[numColumns];
for (int i = 0; i < numColumns; i++) {
columns[i] = new SimpleStringProperty(generateRandomString(3) + i);
}
}
public SimpleStringProperty getColumn(int index) {
return columns[index];
}
public void setColumn(int index, String value) {
columns[index].set(value);
}
public static String generateRandomString(int length) {
String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random random = new Random();
StringBuilder randomString = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = random.nextInt(letters.length());
randomString.append(letters.charAt(index));
}
return randomString.toString();
}
}
private final TableView<Person> table = new TableView<>();
private final ObservableList<Person> data = createALotOfPeople(50000, 1000);
private static ObservableList<Person> createALotOfPeople(int numRows, int numColumns) {
ObservableList<Person> objects = FXCollections.observableArrayList();
for (int i = 0; i < numRows; i++) {
objects.add(new Person(numColumns));
}
return objects;
}
public static void main(String[] args) {
launch(args);
}
u/Override
public void start(Stage stage) {
Scene scene = new Scene(table);
stage.setTitle("Editable Table");
stage.setWidth(1000);
stage.setHeight(600);
table.setEditable(true);
int numColumns = 100;
for (int i = 0; i < numColumns; i++) {
TableColumn<Person, String> column = new TableColumn<>("Column " + (i + 1));
int colIndex = i;
column.setMinWidth(100);
column.setCellValueFactory(
new Callback<>() {
public ObservableValue<String> call(TableColumn.CellDataFeatures<Person, String> p) {
return p.getValue().getColumn(colIndex);
}
});
column.setCellFactory(TextFieldTableCell.forTableColumn());
column.setOnEditCommit(
(TableColumn.CellEditEvent<Person, String> t) -> {
t.getTableView().getItems().get(
t.getTablePosition().getRow()).setColumn(colIndex, t.getNewValue());
}
);
table.getColumns().add(column);
}
table.setItems(data);
table.getSelectionModel().setCellSelectionEnabled(false);
table.getStylesheets().add(Objects.requireNonNull(getClass().getResource("style.css")).toExternalForm());
stage.setScene(scene);
stage.show();
}
}Style.css as follows:
.table-cell.select-me {
-fx-border-color: #3296B9;
-fx-background-color: #CDE6EB;
-fx-text-fill: black;
}
.table-cell:selected {
-fx-border-color: #3296B9;
-fx-background-color: #CDE6EB;
-fx-text-fill: black;
}
.table-row-cell.contains-selection {
-fx-background-color: #CDE6EB;
}
.table-row-cell:selected {
-fx-background-color: #CDE6EB;
-fx-text-fill: black;
}
.table-view {
-fx-skin: "javafx.skins.CustomTableViewSkin";
}Skin as follows:
import javafx.collections.ListChangeListener;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.skin.TableViewSkin;
import javafx.scene.input.ScrollEvent;
public class CustomTableViewSkin<T> extends TableViewSkin<T> {
public CustomTableViewSkin(TableView<T> table) {
super(table);
if (table.getSelectionModel().isCellSelectionEnabled()) {
table.getSelectionModel().getSelectedCells().addListener((ListChangeListener<TablePosition>) change -> {
while (change.next()) {
if (change.wasAdded() || change.wasRemoved()) {
updateRowStyles(table);
}
}
});
} else {
table.getSelectionModel().getSelectedCells().addListener((ListChangeListener<TablePosition>) change -> {
while (change.next()) {
if (change.wasAdded() || change.wasRemoved()) {
updateCellStyles(table);
}
}
});
table.addEventFilter(ScrollEvent.ANY, event -> {
System.out.println("This change was triggered as we are scrolling.");
updateCellStyles(table);
});
}
}
private void updateRowStyles(TableView<T> table) {
for (Node row : table.lookupAll(".table-row-cell")) {
updateRowStyle((TableRow<?>) row, table);
}
}
private void updateRowStyle(TableRow<?> row, TableView<T> table) {
if (row.getItem() != null) {
boolean hasSelectedCells = table.getSelectionModel().getSelectedCells().stream()
.anyMatch(pos -> pos.getRow() == row.getIndex());
if (hasSelectedCells) {
row.getStyleClass().add("contains-selection");
} else {
row.getStyleClass().removeAll("contains-selection");
}
}
}
private void updateCellStyles(TableView<T> table) {
for (Node cell : table.lookupAll(".table-cell")) {
TableCell<?, ?> tableCell = (TableCell<?, ?>) cell;
tableCell.editingProperty().addListener((obs, wasEditing, isNowEditing) -> {
if (isNowEditing) {
table.lookupAll(".select-me").forEach(node -> node.getStyleClass().removeAll("select-me"));
}
});
updateCellStyle(tableCell, table);
}
}
private void updateCellStyle(TableCell<?, ?> cell, TableView<T> table) {
TablePosition<?, ?> cellPosition = new TablePosition<>(table, cell.getIndex(), (TableColumn<T, ? extends Object>) cell.getTableColumn());
if (table.getSelectionModel().getSelectedCells().contains(cellPosition)) {
cell.getStyleClass().add("select-me");
} else {
cell.getStyleClass().removeAll("select-me");
}
}
}The result I am getting is essentially what I want (Please ignore the code refactoring, I will do it later once I figure this out)
But the issue is while I was testing for performance on such a large data, and I am scrolling, the highlight essentially comes up once again when I am presuming the table view is reused. I had added the listener to scroll to update the table again and I am unable to figure out why that does not work first time, and then it works.
Is there a better way we get to get this entire thing done??
The expectation here is the user can apply this css & skin will auto apply which will result in desired row selection (look n feel) & selected cell to get highlighted.
I went through this link: https://stackoverflow.com/questions/50459063/javafx-tableview-highlight-row-on-setcellselectionenabledtrue
But this is having an issue of freezing after a while, which I could reproduce. Does someone have an idea on how to do this?
View all comments
Show parent comment
2
u/hamsterrage1 Aug 03 '24
I'm not sure skinning will do you any good, because whatever they are doing in their custom row factories could override it. And this kind of functionality is intended to go with the row, so if you try to do an end run around that by putting crazy stuff in you skin that takes it over you're likely just to cause problems.
It feels to me that most people customize the row factory, but it's usually based on instantiating a vanilla
TableRowand then configuring thatTableRow(). So it's a custom row factory, but still the standardTableRow.Because this approach adds a whole new
Propertyto support the PseudoCode, this row factory just delivers up a customTableRow. So you could split it out to be a proper class,OurTableRow<>, and then have people instantiate it in their custom row factories instead of the vanillaTableRow.How do you ensure that they do this? You can't. People are going to do what they are going to do. But you can make it easier to use what you've built than to not use it.
So what I would be tempted to do is to look at the things that people are generally using custom row factories for, and maybe incorporating that stuff into your custom
TableRow- possibly with configuration flags. That way it's actually easier to just use your customTableRow.