突出显示gridpane列

时间:2017-02-19 21:09:35

标签: javafx

好吧,当我将鼠标悬停在列中的任何节点上时,我试图突出显示网格窗格中垂直列中的所有节点。所以现在我得到我的鼠标结束的节点的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;
    }

1 个答案:

答案 0 :(得分:1)

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

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;");
        }
    }));
}

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