1 Reply Latest reply on Apr 6, 2013 4:48 PM by James_D

    Alignment Woes

    1001443
      Sorry for the Newbie question, but I'm having some strange results trying to align contents of a GridPane.

      First, here's my hierarchy:
      In a TabPane, I have a Tab which contains a BorderPane. The BorderPane has a Label in the Top, a Label in the Left, and a GridPane in the Center. Row 1 contains 10 Labels labelled "1" through "10" starting in column 2. Column 1 contains 10 Labels also labelled "1" through "10" starting in row 2. I then have a 10x10 grid of RadioButtons beginning in grid 2,2 through 11,11.

      When I resize the application, the two labels in the BorderPane move to their center as desired, but the nodes within the GridPane won't resize. I tried using row and column constraints, setting them to 10%, but I'm getting strange results. The columns will resize, but the last column is off the window, and the rows haven't changed. I also see that the label in the BorderPane's left area is no longer visible.

      Any suggestions on how I can get the GridPane to resize correctly?

      Thanks!
        • 1. Re: Alignment Woes
          James_D
          Isn't the problem just that you have 11 rows/columns, each with 10% of the height/width?

          This works for me:
          import javafx.application.Application;
          import javafx.geometry.Pos;
          import javafx.scene.Scene;
          import javafx.scene.control.Label;
          import javafx.scene.control.RadioButton;
          import javafx.scene.layout.BorderPane;
          import javafx.scene.layout.ColumnConstraints;
          import javafx.scene.layout.GridPane;
          import javafx.scene.layout.RowConstraints;
          import javafx.stage.Stage;
          
          public class RadioButtonGrid extends Application {
          
            @Override
            public void start(Stage primaryStage) {
              final BorderPane root = new BorderPane();
              final Label colHeader = new Label("Main Column Header");
              colHeader.setMaxWidth(Double.POSITIVE_INFINITY);
              colHeader.setAlignment(Pos.CENTER);
              root.setTop(colHeader);
              final Label rowHeader = new Label("Main Row Header");
              root.setLeft(rowHeader);
              rowHeader.setRotate(90);
              rowHeader.setMaxHeight(Double.POSITIVE_INFINITY);
              rowHeader.setAlignment(Pos.CENTER);
              final int numRows = 10;
              final int numCols = 10;
              final GridPane grid = new GridPane();
              ColumnConstraints leftColConstraints = new ColumnConstraints();
              leftColConstraints.setPercentWidth(10);
              RowConstraints topRowConstraints = new RowConstraints();
              topRowConstraints.setPercentHeight(10);
              grid.getColumnConstraints().add(leftColConstraints);
              grid.getRowConstraints().add(topRowConstraints);
              for (int x = 0; x < numCols; x++) {
                grid.add(new Label("ABCDEFGHIJ".substring(x, x + 1)), x + 1, 0);
                ColumnConstraints colConstraints = new ColumnConstraints();
                colConstraints.setPercentWidth(9);
                grid.getColumnConstraints().add(colConstraints);
                for (int y = 0; y < numRows; y++) {
                  grid.add(new RadioButton(), x + 1, y + 1);
                }
              }
              for (int y = 0; y < numRows; y++) {
                grid.add(new Label(Integer.toString(y + 1)), 0, y + 1);
                RowConstraints rowConstraints = new RowConstraints();
                rowConstraints.setPercentHeight(9);
                grid.getRowConstraints().add(rowConstraints);
              }
              root.setCenter(grid);
              primaryStage.setScene(new Scene(root, 600, 600));
              primaryStage.show();
            }
          
            public static void main(String[] args) {
              launch(args);
            }
          }