如何在JavaFX上设置tableView的行前景色?
我有一个tableView,其中有很多项目,在其中一些项目中,我需要将前景色设置为红色。所以它是动态设置的。我成功地将一个rowFactory添加到表中,并通过css更改背景颜色,但前景不变。它总是黑色的:如何在JavaFX上设置tableView的行前景色?,java,javafx,javafx-2,javafx-8,Java,Javafx,Javafx 2,Javafx 8,我有一个tableView,其中有很多项目,在其中一些项目中,我需要将前景色设置为红色。所以它是动态设置的。我成功地将一个rowFactory添加到表中,并通过css更改背景颜色,但前景不变。它总是黑色的: tableViewAbastecidas.setRowFactory(param -> new TableRow<LeituraPista>() { @Override protected void updateItem(LeituraPista item,
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);
}
});
tableViewAbastecidas.setRowFactory(参数->新建TableRow(){
@凌驾
受保护的void updateItem(LeituraPista项,布尔值为空){
如果(项==null){
返回;
}
字符串estilo;
如果(tableViewAbastecidas.getSelectionModel().getSelectedItems()包含(项)){
estilo=“-fx背景色:线性渐变(#95caff 0%,#77acff 90%,#E0 90%);”;
}否则{
estilo=“-fx背景色:线性渐变(白色0%,白色90%,#e0 90%);”;
}
if(真菌病诊断仪){
estilo+=“-fx填充:#ff0000;-fx文本填充:图表重用;”;//不起作用,始终为黑色
}
setStyle(estilo);
}
});
根据我的经验,将样式放在外部样式表中总是效果更好
我认为您遇到的问题是TableRow
不支持-fx text-fill
属性;它由它所包含的表格单元格
支持。可以在所有列上设置单元工厂,但如果使用外部样式表,则可以操作行的样式类,然后更改行及其子代的文本填充(因为css样式将由子节点继承)
因为您使用的是JavaFX8,所以可以使用,它的API比处理样式类列表要干净得多
所以(我没有直接测试)
下面是一个完整的示例:
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);
}
}
您缺少
super.updateItem(项,空)代码>呼叫。不确定这是否是问题所在,但它做了很多重要的“内务管理”。Ref和Ref我尝试调用super.updateItem(item,empty)
,但它似乎不起作用。有趣的是,背景颜色变化正确,如果需要,我甚至可以更改字体和字体大小,但不能更改颜色。@Reegan Miranda,我查看了您的引用,只看到它们在表列上使用cellFactory,而不是在表视图本身上使用rowFactory。试过了,它不起作用。试过了,伪类被正确地激发,我在内放置了另一个-fx背景颜色:粉红色。表格行单元格:我的条件
,背景正确地变为粉红色,但字体颜色没有变为#ff0000或chartrese。请参见编辑(在css文件中)。显然,您必须直接在表格单元格
上设置-fx text fill
(这很容易用CSS实现)。太棒了!诀窍是将-fx文本填充
放在表格单元格中,而不是表格行中!这是所有类似问题的最佳答案。它的灵活性和优雅。非常感谢你。
.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-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;
}