如何使用JavaFX从选定表行中的表格单元格中删除选择

时间:2014-09-05 13:05:35

标签: java javafx tableview selection tablecell

我有一些带有一些列的TableView。一列包含自定义TableCell节点。我想在选择行时从该列单元格中删除选择突出显示。

以下是显示我想要的示例图像:

enter image description here

我该怎么做?

1 个答案:

答案 0 :(得分:1)

使用自定义单元工厂向单元格添加样式类:

    firstNameCol.setCellFactory(col -> {
        TableCell<Person, String> cell = new TableCell<Person, String>() {
            @Override
            public void updateItem(String name, boolean empty) {
                super.updateItem(name, empty);
                if (empty) {
                    setText("");
                } else {
                    setText(name);
                }
            }
        };
        cell.getStyleClass().add("no-select-cell");
        return cell ;
    });

然后在外部样式表中,将所选行中no-select-cell的样式恢复为默认值:

.table-row-cell:selected .no-select-cell {

    -fx-background: -fx-control-inner-background;
    -fx-background-color: -fx-background;
    -fx-text-fill: -fx-text-background-color;
}

.table-row-cell:odd:selected .no-select-cell {
    -fx-background: -fx-control-inner-background-alt ;
}
相关问题