【发布时间】:2015-11-14 03:45:21
【问题描述】:
此问题与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 以便着色工作?
【问题讨论】:
-
要为整行着色,请在表格上使用
rowFactory,而不是在列上使用cellFactory。您需要在此处发布有关TableView和正在测试的属性的更多信息以获得完整答案。 -
@James_D,但上面的代码运行良好。问题是如何隐藏那一列,而不影响结果。或者,我是不是误会了什么?
-
@James_D,我添加了 FXML 注释和与本专栏相关的其他部分。我想要做的是当 tc_inst 等于 1070 时 - 番茄的颜色行,当它不是时 - 不要做任何事情。我再重复一次。不隐藏列时效果很好,但我想以某种方式隐藏它
-
当你同声说你不能让它做你想做的事时,我从来不理解“效果很好”的想法。如果您不能在不显示列的情况下更改行的颜色,并且您不想显示该列,那么根据定义,它不会按照您想要的方式工作。