根据我的经验,将样式放在外部样式表中总是效果更好。
我认为您遇到的问题是 TableRow 不支持 -fx-text-fill 属性;它由它包含的TableCells 支持。您可以在所有列上设置单元格工厂,但如果您使用外部样式表,则可以操作行的样式类,然后更改其文本填充及其后代(因为 css 样式将由子节点继承) .
由于您使用的是 JavaFX 8,因此您可以使用 PseudoClasss,它的 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);
}
}
table-row-style-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;
}