I'm making a JavaFX project and using the Jfoenix custom library for nicer components. In a schedule table I have, I need the rows to become red if the start date of the event has passed already however I cannot find any answers online anywhere as to how I should iterate through the rows.
In my CSS file, I need this line to set the rows to red if they match the given criteria with the pseudo class toggleRed
.
.jfx-tree-table-view > .virtual-flow > .clipped-container > .sheet > .tree-table-row-cell:filled:toggleRed {
-fx-background-color: red;
}
So in my controller initialize method, I'm going to have this line if the row object is valid
row.pseudoClassStateChanged(PseudoClass.getPseudoClass("toggleRed"), true);
I need some kind of for-loop to get every table row in the table to call on this line but haven't found anything that works yet. Please help. I'm completely lost and have wasted far too much time on this. Thanks!!!
You need to change the rowFactory
and update the pseudoclass state according to the item's data property and the current time.
The following example should provide you with an idea of how to implement this:
final PseudoClass toggleRed = PseudoClass.getPseudoClass("toggleRed");
ObjectProperty<LocalDate> currentDate = ...;
treeTableView.setRowFactory(ttv -> new JFXTreeTableRow<Job>() {
private final InvalidationListener listener = o -> {
Job item = getItem();
pseudoClassStateChanged(toggleRed, item != null && item.getStartDate().isAfter(currentDate.get()));
};
private final WeakInvalidationListener l = new WeakInvalidationListener(listener);
{
// listen to changes of the currentDate property
currentDate.addListener(l);
}
@Override
protected void updateItem(Job item, boolean empty) {
// stop listening to property of old object
Job oldItem = getItem();
if (oldItem != null) {
oldItem.startDateProperty().removeListener(l);
}
super.updateItem(item, empty);
// listen to property of new object
if (item != null) {
item.startDateProperty().addListener(l);
}
// update pseudoclass
listener.invalidated(null);
}
});
If the start dates and/or the current date are immutable, you could reduce the amount of listeners used.