gpt4 book ai didi

javafx - JavaFX 中的着色表行

转载 作者:行者123 更新时间:2023-12-02 06:41:45 25 4
gpt4 key购买 nike

此问题与 this 相关。现在我想为字段值等于某个值的行着色。

    @FXML
private TableView<FaDeal> tv_mm_view;
@FXML
private TableColumn<FaDeal, String> tc_inst;
tc_inst.setCellValueFactory(cellData -> new SimpleStringProperty(""+cellData.getValue().getInstrumentId()));

tc_inst.setCellFactory(column -> new TableCell<FaDeal, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);

if (item == null || empty) {
setText(null);

} else {

setText(item);
// Style row where balance < 0 with a different color.

TableRow currentRow = getTableRow();
if (item.equals("1070")) {
currentRow.setStyle("-fx-background-color: tomato;");

} else currentRow.setStyle("");
}
}
});

问题是我不想在表格中显示tc_inst。因此,我将 SceneBuilder 中的 visible 复选框设置为 false。在这种情况下,着色部分根本不起作用。如何隐藏 tc_inst 以便着色起作用?

最佳答案

如果要更改整行的颜色,请使用行工厂而不是单元工厂:

tv_mm_view.setRowFactory(tv -> new TableRow<FaDeal>() {
@Override
public void updateItem(FaDeal item, boolean empty) {
super.updateItem(item, empty) ;
if (item == null) {
setStyle("");
} else if (item.getInstrumentId().equals("1070")) {
setStyle("-fx-background-color: tomato;");
} else {
setStyle("");
}
}
});

请注意,如果在显示行时 instrumentId 的值发生变化,则上述代码的颜色不会自动更改,除非您执行一些额外的工作。实现这一点的最简单方法是使用返回 instrumentIdProperty() 的提取器构建项目列表(假设您在 FaDeal 中使用 JavaFX 属性模式)。

关于javafx - JavaFX 中的着色表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32119277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com