我想根據狀態爲表格中的行着色。搜索後,我發現這Formatting Rows in a JavaFX TableView Using CSS Pseudo Classes。Javafx tableview changelistener更改爲經常
我試圖實現它,爲我的目的。它也可以,但是如果我滾動,顏色會變回默認的顏色。
我在做什麼錯?
@FXML
private void initialize(){
PseudoClass online = PseudoClass.getPseudoClass("online");
PseudoClass offline = PseudoClass.getPseudoClass("offline");
PseudoClass unknown = PseudoClass.getPseudoClass("unknown");
//Set a rowFactory for the table view.
overviewTable.setRowFactory(tableView -> {
TableRow<State> row = new TableRow<>();
ChangeListener<String> changeListener = (obs, oldValue, newValue) -> {
System.out.println("1: " + obs + " : " + oldValue + " : " + newValue);
row.pseudoClassStateChanged(online, newValue.equals("online"));
row.pseudoClassStateChanged(offline, newValue.equals("offline"));
row.pseudoClassStateChanged(unknown, newValue.equals("unknown"));
};
row.itemProperty().addListener((obs, oldValue, newValue) -> {
System.out.println("2: " + obs + " : " + oldValue + " : " + newValue);
if (oldValue != null) {
oldValue.stateProperty().removeListener(changeListener);
}
if (newValue != null) {
newValue.stateProperty().addListener(changeListener);
row.pseudoClassStateChanged(online, newValue.equals("online"));
row.pseudoClassStateChanged(offline, newValue.equals("offline"));
row.pseudoClassStateChanged(unknown, newValue.equals("unknown"));
} else {
row.pseudoClassStateChanged(online, false);
row.pseudoClassStateChanged(offline, false);
row.pseudoClassStateChanged(unknown, false);
}
});
return row;
});
}
CSS:
.table-row-cell:unknown{
-fx-background-color: blue;
}
.table-row-cell:online{
-fx-background-color: #2EAB15;
}
.table-row-cell:offline{
-fx-background-color: #BB0000;
}
.table-row-cell .text{
-fx-fill: white;
}
「但如果我滾動顏色將改回默認的顏色。」這是什麼意思?僞類是否被改變(IE:你是否獲得了syso的)?僞類仍然是相同的,但顏色只是在改變? – Ironcache