S
S
striver2019-12-29 16:31:57
JavaFX
striver, 2019-12-29 16:31:57

How to change color in table in JavaFX depending on conditions?

The data is unloaded from the database, so I found such a solution for unloading data and changing the color.

private void styleRowColor() {
        Callback<TableColumn<ModelTableDriversLogArea, String>, TableCell<ModelTableDriversLogArea, String>> cellFactory
                = 
                new Callback<TableColumn<ModelTableDriversLogArea, String>, TableCell<ModelTableDriversLogArea, String>>() {
            @Override
            public TableCell<ModelTableDriversLogArea, String> call(final TableColumn<ModelTableDriversLogArea, String> param) {
                final TableCell<ModelTableDriversLogArea, String> cell = new TableCell<ModelTableDriversLogArea, String>() {

                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                            setText(null);
                        } else {
                            setText(item);
                            TableRow<ModelTableDriversLogArea> row = getTableRow();
                            if (row.getItem().getDivision().equals("-")) {
                                row.getStyleClass().clear();
                                 setStyle("-fx-background-color: green");
                            }
                        }
                    }
                };
                return cell;
            }
        };
        col_divisionDriversAreaLog.setCellFactory(cellFactory);
    }

The problem is that when you scroll down, arbitrary cells in the table start to turn green. At the same time, if you scroll up and down, then in the end the entire column with data will be painted over in green. Is it possible to change something, or are there other options for changing colors depending on the data in the plate?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question