【问题标题】:Highlight gridpane Column突出显示网格窗格列
【发布时间】:2017-07-09 01:03:43
【问题描述】:

好的,所以当我将鼠标悬停在列中的任何节点上时,我试图突出显示网格窗格中垂直列中的所有节点。所以现在我正在获取鼠标所在节点的 columnIndex,然后创建一个包含共享该列索引的所有节点的数组。将该数组返回给 main 方法,然后将数组中节点的背景颜色更改为一种颜色。

这是鼠标悬停功能:

for (Node node : officeHoursGridPane.getChildren()) {
                node.setOnMouseEntered((MouseEvent t) -> {
                    node.setStyle("-fx-background-color:#f9f3c5;");
                    Node source = (Node)t.getSource();
                    Integer colIndex =     GridPane.getColumnIndex(source);
                    Integer rowIndex =     GridPane.getRowIndex(source);
                    //ystem.out.println("Column #: " + colIndex +     "\nRow #: " + rowIndex);                   
                    for(int c = 0; c <= colIndex; c++){
                        Node[] colNode = getNodeByColumnIndex(colIndex, officeHoursGridPane);
                        int colCount=0;
                        for(int v = 0; v <= colNode.length; v++){
                            Node vertNode = colNode[v];
                            vertNode.setStyle("-fx-background-color:#f9f3c5;");   
                        }                    
                    }                   
               });
                   node.setOnMouseExited((MouseEvent t) -> {
                      node.setStyle("-fx-background-color:#ffffff;");
                });          
            }

这是我的 Node[] 构建器:

    public Node[] getNodeByColumnIndex (final int column, GridPane gridPane) {
        Node[] result = null;
        ObservableList<Node> childrens = gridPane.getChildren();
        int count = 0;
        for (Node node : childrens) {
            if(GridPane.getColumnIndex(node) == column) {
                result[count] = node;
                count++;
                if(count > column){
                    break;
                }
            } 
        }

        return result;
    }

【问题讨论】:

  • 问题是?问题出在哪里?
  • 我试图突出显示我的鼠标悬停在节点上的列。我没有得到任何错误或任何东西。这就是我尝试实现它的方式。
  • 我不知道该怎么做
  • @ShawnWilliams 你试过我的建议了吗?
  • @Enigo 是的,就是这样。检查我在您的帖子下方添加的 cmets

标签: javafx


【解决方案1】:

您应该在网格窗格的子项中找到具有相同列索引的所有节点:

for (Node node : officeHoursGridPane.getChildren()) {
    node.setOnMouseEntered(e -> officeHoursGridPane.getChildren().forEach(c -> {
        Integer targetIndex = GridPane.getColumnIndex(node);
        if (GridPane.getColumnIndex(c) == targetIndex) {
            c.setStyle("-fx-background-color:#f9f3c5;");
        }
    }));
    node.setOnMouseExited(e -> officeHoursGridPane.getChildren().forEach(c -> {
        Integer targetIndex = GridPane.getColumnIndex(node);
        if (GridPane.getColumnIndex(c) == targetIndex) {
            c.setStyle("-fx-background-color:#ffffff;");
        }
    }));
}

注意,为了不突出显示额外的节点,您可能还应该检查行索引。

【讨论】:

  • 这很好用。它给了我我需要的框架。我故意只想要这个专栏,因为我想自己做一些工作。我只是需要一个起点。
  • 我在 Row Highlighting 中添加并限制两者只显示反转 L 形状的突出显示根据我的需要
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
相关资源
最近更新 更多