Mat*_*ari 4 java javafx javafx-2 javafx-8
我有一个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);
}
});
Run Code Online (Sandbox Code Playgroud)
以我的经验,将样式放在外部样式表中总是更好。
我认为您遇到的问题是TableRow不支持-fx-text-fill房地产;TableCell它包含的s 支持它。您可以在所有列上设置单元格工厂,但是如果使用外部样式表,则可以操作行的样式类,然后更改其及其后代的文本填充(因为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);
}
});
Run Code Online (Sandbox Code Playgroud)
然后在外部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;
}
Run Code Online (Sandbox Code Playgroud)
这是一个完整的示例:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4298 次 |
| 最近记录: |