我想根据单元格中的数据更新我的 TableView 行颜色,所以我使用了一个 pseeudoClass 来引用 Css 中的样式。行的颜色是我想要的,但是它失去了选择和鼠标悬停效果,现在我有一个彩色的行,没有对所选行的任何指示。这是我的代码:
PseudoClass myPseudoClass = PseudoClass.getPseudoClass("dtta_dep");
PseudoClass myPseudoClass1 = PseudoClass.getPseudoClass("dtta_dest");
fplTableView.setRowFactory(tv -> new TableRow<FlightPlan>() {
@Override
public void updateItem(FlightPlan item, boolean empty) {
super.updateItem(item, empty);
this.setFocused(true);
this.setHover(true);
System.out.println("myPseudoClass = "+myPseudoClass.getPseudoClassName());
pseudoClassStateChanged(myPseudoClass, (! empty) && item.Dep_aerodomProperty().get().equalsIgnoreCase("DTTA"));
pseudoClassStateChanged(myPseudoClass1, (! empty) && item.Dest_aerodomProperty().get().equalsIgnoreCase("DTTA"));
}
});
getData();
for (int i = 0; i < listF.size(); i++) {
System.out.println(listF.get(i).Dep_aerodomProperty().get());
}
selectWithService();
});
CSS文件:
.table-row-cell {
-fx-background-color: linear-gradient(white 0%, white 90%, #e0e0e0 90%);
}
.table-row-cell:selected {
-fx-background-color: linear-gradient(#95caff 0%, #77acff 90%, #e0e0e0 90%);
}
.table-row-cell:dtta_dep .table-cell {
-fx-text-fill: red;
-fx-background-color:beige;
}
.table-row-cell:dtta_dest .table-cell {
-fx-text-fill: blue;
-fx-background-color:greenyellow;
}
最佳答案
在表格行单元格上使用-fx-background
而不是-fx-background-color
来设置未选中的背景。您可以使用 -fx-selection-bar
设置所选颜色。文本填充在具有 -fx-text-background-color
颜色的表格单元格中定义(即文本填充用于 -fx-background
上的文本),因此您可以覆盖单元格中的文本填充。
.table-row-cell {
-fx-background: linear-gradient(white 0%, white 90%, #e0e0e0 90%);
-fx-selection-bar: linear-gradient(#95caff 0%, #77acff 90%, #e0e0e0 90%);
}
.table-row-cell:dtta_dep {
-fx-text-background-color: red;
-fx-background: beige;
}
.table-row-cell:dtta_dest {
-fx-text-background-color: blue;
-fx-background:greenyellow;
}
关于css - JavaFX TableView 行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36086896/