cp5
cp5

Reputation: 1133

How to change the row color based on data that can change in a tableview?

I am writing a podcast player and have a list of files that I display.

The user can double click an entry and that files play. If they select another one. I don’t know what has been selected in the list. The idea is to highlight the file that is playing in a different color. With still keeping the ability to select another file (don’t want to lose that functionality). Most audio players work on the same principle.

I had a look at this JavaFX tableview colors and this changes the color of the row based on data. But if the data changes the row color do not refresh as the data changes.

I have a table with cell factory and based on the variable "isSelectedFile" I set the cell style.

If the isSelectedFile=True at load time all is ok but once I change the data while the rows are loaded into the tableview it doesn't refresh.

My cell factory is as follows:

entryDisplayNameColumn.setCellFactory(new Callback<TableColumn<PlaylistEntry, String>, TableCell<PlaylistEntry, String>>() {
            @Override
            public TableCell call(TableColumn p) {

                TableCell cell = new TableCell<Task, Object>() {
                    @Override
                    public void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        setText(empty ? null : getString());
                        setGraphic(null);
                        TableRow currentRow = getTableRow();
                        PlaylistEntry ple = currentRow == null ? null : (PlaylistEntry) currentRow.getItem();

                        if (ple != null) {
                            clearPriorityStyle();
                            setPriorityStyle(ple.isSelectedFile());
                        }
                    }

                    @Override
                    public void updateSelected(boolean upd) {
                        super.updateSelected(upd);
                        System.out.println("is update");
                    }

                    private void clearPriorityStyle() {
                        ObservableList<String> styleClasses = getStyleClass();
                        styleClasses.remove("priorityLow");
                        styleClasses.remove("priorityMedium");
                        styleClasses.remove("priorityHigh");
                    }

                    private void setPriorityStyle(boolean activeValue) {
                        if (activeValue) {
                            getStyleClass().add("priorityLow");
                        } else {
                            getStyleClass().add("priorityMedium");
                        }
                    }

                    private String getString() {
                        return getItem() == null ? "" : getItem().toString();
                    }
                };
                return cell;
            }
        });

My CSS:

.priorityLow { 
-fx-control-inner-background: snow;
-fx-accent: derive(-fx-control-inner-background, -40%);
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}

.priorityMedium { 
-fx-control-inner-background: #1d1d1d;
-fx-accent: derive(-fx-control-inner-background, -40%);
-fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
 }

Upvotes: 0

Views: 2273

Answers (1)

Anshul Parashar
Anshul Parashar

Reputation: 3126

try this its perfectly worked...

tablecol.setCellFactory(new Callback<TableColumn<CheckDo, String>, TableCell<CheckDo, String>>() {

        @Override
        public TableCell<CheckDo, String> call(TableColumn<CheckDo, String> p) {


             return new TableCell<CheckDo, String>() {

            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if (!isEmpty())
                    this.setStyle("-fx-background-color:red");
                    this.getTableRow().setStyle("-fx-background-color:red");
                        setText(item);
                }
            }
        };

Upvotes: 2

Related Questions