Java FX 8 table row highlighting

I have a Java FX8 table, I populated it with an observable list. The table displays the completed data.

During user interaction, a new line is created, I add this new data to the observed list. The table is being updated.

I know how to move focus as well as scroll through this newly added line. However, I want this line to be highlighted to show that it has been recently added.

How to get the link to the whole line as Node so that I can use this node value to highlight the line.

0
source share
1 answer

rowFactory TableView, TableRow. TableRow Node, . , " ".

.

  • ObjectProperty<Person> . null, Person, Person:

final ObjectProperty<Person> recentlyAddedPerson = new SimpleObjectProperty<>();

  • ListListener . , recentlyAddedPerson. , "" , , reset recentlyAddedPerson null ( ).:

    final Duration timeToGetOld = Duration.seconds(1.0);
    
    table.getItems().addListener((Change<? extends Person> change) -> {
        while (change.next()) {
            if (change.wasAdded()) {
                List<? extends Person> addedPeople = change.getAddedSubList();
                Person lastAddedPerson = addedPeople.get(addedPeople.size()-1);
                recentlyAddedPerson.set(lastAddedPerson);
    
                // set back to null after a short delay, unless changed since then:
                PauseTransition agingTime = new PauseTransition(timeToGetOld);
                agingTime.setOnFinished(event -> {
                    if (recentlyAddedPerson.get() == lastAddedPerson) {
                        recentlyAddedPerson.set(null);
                    }
                });
                agingTime.play();
            }
        }
    });
    
  • factory . factory TableRow. TableRow BooleanBinding, true, . ( true, recentlyAddedPerson.)

  • , CSS PseudoClass. ; BooleanBinding, TableRow.

, - :

    final PseudoClass newPersonPseudoClass = PseudoClass.getPseudoClass("new");

rowFactory

    table.setRowFactory(tableView -> new TableRow<Person>() {
        // Bindings API uses weak listeners, so this needs to be a field to
        // make sure it stays in scope as long as the TableRow is in scope.
        private final BooleanBinding itemIsNewPerson 
            = Bindings.isNotNull(itemProperty())
            .and(Bindings.equal(itemProperty(), recentlyAddedPerson));

        {
            // anonymous constructor:
            itemIsNewPerson.addListener((obs, wasNew, isNew)
                    -> pseudoClassStateChanged(newPersonPseudoClass, isNew));
        }
    });

, CSS :

.table-row-cell:new {
    -fx-background-color: darkseagreen ;
}

.

+3

All Articles