I have 2 sets of events that change the appearance of the table row.
- table flashing when some conditions flash
- table highlighting when a new item is added
Suppose my TableView
is called table
, my code structure is like this:
TableView<Trade> table = ... ;
table.setRowFactory( private final BooleanBinding itemIsNewTrade = Bindings.isNotNull(itemProperty()).and(Bindings.equal(itemProperty(), recentlyAddedTrade));
{
// anonymous constructor:
itemIsNewTrade.addListener((obs, wasNew, isNew) -> pseudoClassStateChanged(newTradePseudoClass, isNew));
}
tv -> {
TableRow<Trade> row = new TableRow<>();
Timeline flasher = new Timeline(
new KeyFrame(Duration.seconds(0.5), e -> {
row.pseudoClassStateChanged(flashHighlight, true);
}),
new KeyFrame(Duration.seconds(1.0), e -> {
row.pseudoClassStateChanged(flashHighlight, false);
})
);
flasher.setCycleCount(Animation.INDEFINITE);
ChangeListener<Boolean> cautionListener = (obs, cautionWasSet, cautionIsNowSet) -> {
if (cautionIsNowSet) {
flasher.play();
} else {
flasher.stop();
row.pseudoClassStateChanged(flashHighlight, false);
}
};
row.itemProperty().addListener((obs, oldItem, newItem) -> {
if (oldItem != null) {
oldItem.cautionProperty().removeListener(cautionListener);
}
if (newItem == null) {
flasher.stop();
row.pseudoClassStateChanged(flashHighlight, false);
} else {
newItem.cautionProperty().addListener(cautionListener);
if (newItem.cautionProperty().get()) {
flasher.play();
} else {
flasher.stop();
row.pseudoClassStateChanged(flashHighlight, false);
}
}
});
return row ;
}
);
UPDATE: The code for table flashing comes from here, while the code for table highlighting comes from here. I tried to combine both together by having a structure like above.
But I am getting error. I just do not know how to combine Anonymous inner class and lambda expression together
How should I overcome this ?