r/JavaFX • • 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?

5 Upvotes

14 comments sorted by

View all comments

Show parent comment

1

u/_dk7 Aug 04 '24

Thank you once again for your prompt response! I was thinking about this approach and now I am more confident.