我有一个tableView,其中我有很多项目,在其中一些项目中我需要将前景色设置为红色。所以它是动态设置的。我成功的做法是在表中添加一个rowFactory并通过css更改背景颜色,但前景不会改变。它总是黑的:
tableViewAbastecidas.setRowFactory(param -> new TableRow<LeituraPista>() {
@Override
protected void updateItem(LeituraPista item, boolean empty) {
if (item == null) {
return;
}
String estilo;
if (tableViewAbastecidas.getSelectionModel().getSelectedItems().contains(item)) {
estilo = "-fx-background-color: linear-gradient(#95caff 0%, #77acff 90%, #e0e0e0 90%);";
} else {
estilo = "-fx-background-color: linear-gradient(white 0%, white 90%, #e0e0e0 90%);";
}
if (myConditionIsTrue) {
estilo += "-fx-fill: #ff0000;-fx-text-fill: chartreuse;"; //Doesn't work, always black
}
setStyle(estilo);
}
});
答案 0 :(得分:5)
根据我的经验,将样式放在外部样式表中总是更好。
我认为您遇到的问题是TableRow
不支持-fx-text-fill
属性;它由它包含的TableCell
支持。您可以在所有列上设置单元格工厂,但是如果使用外部样式表,则可以操作该行的样式类,然后更改它的文本填充及其后代(因为css样式将由子节点继承) 。
由于您正在使用JavaFX 8,因此您可以使用PseudoClass
s,它具有比操作样式类列表更清晰的API。
所以(我还没有直接测试过这个)
// the string "my-condition" is the name of the pseudoclass and appears in the
// external css file (see below):
PseudoClass myConditionPseudoClass = PseduoClass.getPseudoClass("my-condition");
tableViewAbastecidas.setRowFactory(param -> new TableRow<LeituraPista>() {
@Override
protected void updateItem(LeituraPista item, boolean empty) {
super.updateItem();
pseudoClassStateChanged(myConditionPseudoClass, (! empty) && myCondition);
}
});
然后你在外部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:my-condition .table-cell {
-fx-fill: #ff0000;
-fx-text-fill: chartreuse;
}
这是一个完整的例子:
import java.util.Random;
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.css.PseudoClass;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TableRowStyleExample extends Application {
@Override
public void start(Stage primaryStage) {
TableView<Item> table = new TableView<>();
TableColumn<Item, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getName()));
TableColumn<Item, Number> valueCol = new TableColumn<>("Value");
valueCol.setCellValueFactory(cellData -> new ReadOnlyIntegerWrapper(cellData.getValue().getValue()));
table.getColumns().add(nameCol);
table.getColumns().add(valueCol);
final Random rng = new Random();
IntStream.rangeClosed(1, 20)
.mapToObj( i -> new Item("Item "+i, rng.nextInt(10)+1))
.forEach(table.getItems()::add);
PseudoClass highValuePseudoClass = PseudoClass.getPseudoClass("high-value");
table.setRowFactory(tv -> new TableRow<Item>() {
@Override
public void updateItem(Item item, boolean empty) {
super.updateItem(item, empty);
pseudoClassStateChanged(highValuePseudoClass, (! empty) && item.getValue() >= 9);
}
});
Scene scene = new Scene(new BorderPane(table), 600, 400);
scene.getStylesheets().add("table-row-style-example.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static class Item {
private final String name ;
private final int value ;
public Item(String name, int value) {
this.name = name ;
this.value = value ;
}
public String getName() {
return name ;
}
public int getValue() {
return value ;
}
}
public static void main(String[] args) {
launch(args);
}
}
表行式-example.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:high-value .table-cell {
-fx-text-fill: chartreuse;
}