2

I'm trying to border to "grouped" cells in gridpane (or frames)

so this is basically my gridpane:

GridPane grid = new GridPane();
grid.setHgap(H_SIZE);
grid.setVgap(V_SIZE);

and I want to do something like this:

grid.setBorder(1,1, H_SIZE/3, V_SIZE/3);
grid.setBorder(H_SIZE*2/3,V_SIZE*2/3, H_SIZE-1, V_SIZE-1);

I'd also like to be able to change the border in case there will be changes while the program is running to the pane (i.e. adding new button to the pane).

Thanks in advance.

So With a bit of work I manage to do it as I wanted:

public void setBorder(GridPane grid, int rowStart, int colStart, int rowEnd, int colEnd){
    Node node;
    node = getNodeByRowColumnIndex(rowStart, colStart, grid);
    node.getStyleClass().add("game-grid-cell");
    node.getStyleClass().add("corner-top-left");
    node = getNodeByRowColumnIndex(rowEnd, colStart, grid);
    node.getStyleClass().add("game-grid-cell");
    node.getStyleClass().add("corner-bot-left");
    node = getNodeByRowColumnIndex(rowEnd, colEnd, grid);
    node.getStyleClass().add("game-grid-cell");
    node.getStyleClass().add("corner-bot-right");
    node = getNodeByRowColumnIndex(rowStart, colEnd, grid);
    node.getStyleClass().add("game-grid-cell");
    node.getStyleClass().add("corner-top-right");

    for(int i = rowStart + 1; i < rowEnd; i++){
        node = getNodeByRowColumnIndex(i, colStart, grid);
        node.getStyleClass().add("game-grid-cell");
        node.getStyleClass().add("left");

        node = getNodeByRowColumnIndex(i, colEnd, grid);
        node.getStyleClass().add("game-grid-cell");
        node.getStyleClass().add("right");
    }

    for(int i = colStart+1; i < colEnd; i++){
        node = getNodeByRowColumnIndex(rowStart, i, grid);
        node.getStyleClass().add("game-grid-cell");
        node.getStyleClass().add("top");

        node = getNodeByRowColumnIndex(rowEnd, i, grid);
        node.getStyleClass().add("game-grid-cell");
        node.getStyleClass().add("bot");
    }
}

public Node getNodeByRowColumnIndex (final int row, final int column, GridPane gridPane) {
    Node result = null;
    ObservableList<Node> childrens = gridPane.getChildren();

    for (Node node : childrens) {
        if(GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == column) {
            return node;
        }
    }

    return result;
}

Where the CSS file is game.css:

.game-grid {
    -fx-background-color: white ;
    -fx-padding: 10 ;
}
.game-grid-cell {
    -fx-background-color: blue, white ;
    /*-fx-background-insets: 0, 0 1 1 0 ;*/
}
.game-grid-cell.corner-top-left{
    -fx-background-radius: 10 0 0 0;
    -fx-background-insets: 0, 1 0 0 1 ;
}
.game-grid-cell.corner-top-right{
    -fx-background-radius: 0 10 0 0;
    -fx-background-insets: 0, 1 1 0 0 ;
}
.game-grid-cell.corner-bot-left{
    -fx-background-radius: 0 0 0 10;
    -fx-background-insets: 0, 0 0 1 1 ;
}
.game-grid-cell.corner-bot-right{
    -fx-background-radius: 0 0 10 0;
    -fx-background-insets: 0, 0 1 1 0 ;
}
.game-grid-cell.top{
    -fx-background-insets: 0, 1 0 0 0 ;
}
.game-grid-cell.bot{
    -fx-background-insets: 0, 0 0 1 0 ;
}
.game-grid-cell.right{
    -fx-background-insets: 0, 0 1 0 0 ;
}
.game-grid-cell.left{
    -fx-background-insets: 0, 0 0 0 1 ;
}
Moran Barzilay
  • 133
  • 2
  • 13

0 Answers0