【问题标题】:JavaFX how to hide empty column cell in tableViewJavaFX如何在tableView中隐藏空列单元格
【发布时间】:2018-09-12 11:40:28
【问题描述】:

我正在使用带有 UNCONSTRAINED_RESIZE_POLICY 的 tableView。 我想将所有空列单元格设置为白色背景。

这是当前视图。

我正在尝试搜索空节点,但是即使通过以下代码(测试函数)搜索tableView中包含的所有节点也找不到它:

 private void test() {
        ArrayList<Node> nodes = getAllNodes(tableView);
        nodes.forEach(node -> {
            if(node instanceof TableCell) {
                if(((TableCell) node).getText() == null || ((TableCell) node).getText().isEmpty()) {
                    System.out.println(true);
                }
            }
        });
    }

    public static ArrayList<Node> getAllNodes(Parent root) {
        ArrayList<Node> nodes = new ArrayList<Node>();
        addAllDescendents(root, nodes);
        return nodes;
    }

    private static void addAllDescendents(Parent parent, ArrayList<Node> nodes) {
        for (Node node : parent.getChildrenUnmodifiable()) {
            nodes.add(node);
            if (node instanceof Parent)
                addAllDescendents((Parent)node, nodes);
        }
    }

【问题讨论】:

  • 不太清楚你的意思:列右边的空白不是tableCell,而是tableRowCell(不,你从来没有尝试过抓住单元格;) -由它的 :odd 样式属性处理(它通过 tableCells 发光,因为它们是透明的)。您可以尝试让 tableCell 进行奇怪的样式并将其从 tableRow 中删除
  • @kleopatra 感谢您的回复。我不明白你的意思是让 tableCell 做奇怪的样式并将其从 tableRow 中删除。我既不能通过 css 选择器选择那些空单元格,也不能在我的应用程序中获取对象。那么如何将 tableCell 更改为奇数样式?
  • 看看 tableRowCell 是如何做到的,如果不为空,则应用到自定义 tableCell (并仔细阅读我上一条评论中的第一句话:)

标签: css javafx tableview javafx-8


【解决方案1】:

最简单的方法是:

.table-row-cell:empty {
    -fx-background-color: transparent;
}

【讨论】:

  • 问题是关于剩余空间的条带化(列的右侧),即该行不为空
【解决方案2】:

使用 CSS 样式表应用样式以从 TableRowCells 中删除背景,并将背景添加到 TableCells:

/* overwrite default row style */
.table-row-cell {
    -fx-background-color: transparent;
    -fx-background-insets: 0;
}

/* apply row style to cells instead */
.table-row-cell .table-cell {
    -fx-background-color: -fx-table-cell-border-color, -fx-background;
    -fx-background-insets: 0, 0 0 1 0;
}

.table-row-cell:odd {
    -fx-background: -fx-control-inner-background-alt;
}

/* fix markup for selected cells/cells in a selected row */
.table-row-cell:filled > .table-cell:selected,
.table-row-cell:filled:selected > .table-cell {
    -fx-background: -fx-selection-bar-non-focused;
    -fx-table-cell-border-color: derive(-fx-background, 20%);
}

.table-view:focused > .virtual-flow > .clipped-container > .sheet > .table-row-cell:filled:selected .table-cell,
.table-view:focused > .virtual-flow > .clipped-container > .sheet > .table-row-cell .table-cell:selected {
    -fx-background: -fx-selection-bar;
}
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

注意:现有列之外没有TableCells。背景应用于TableRowCells。
从虚拟化控件中检索单元也是一个坏主意:

  • 在第一次布局过程中创建单元格。在您运行代码时,它们可能不存在。
  • 与控件交互(例如,通过调整大小、滚动等)可能会导致创建其他单元格。您之前通过遍历场景对找到的单元所做的任何修改都不会自动应用于这些新节点。

【讨论】:

    猜你喜欢
    • 2015-01-22
    • 2017-12-11
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    相关资源
    最近更新 更多