How do I get buttons to fill a javafx gridpane?

ncmathsadist

Java Swing has GridLayout, which allows you to specify a size for an array of widgets such as 3X4. The widgets then fill the panel they occupy. How do you get a similar effect in JavaFX?

James_D

I think you are asking how you get a node to fill the space allocated to its cell in a grid pane.

There are a couple of ways to do this. You can either use the static GridPane methods to apply GridPane-specific property settings to the nodes:

GridPane.setFillWidth(myButton, true);
GridPane.setFillHeight(myButton, true);

The other way is to specify column constraints and row constraints on the grid pane itself:

GridPane grid = new GridPane();
for (int rowIndex = 0; rowIndex < numRows; rowIndex++) {
    RowConstraints rc = new RowConstraints();
    rc.setVgrow(Priority.ALWAYS) ; // allow row to grow
    rc.setFillHeight(true); // ask nodes to fill height for row
    // other settings as needed...
    grid.getRowConstraints().add(rc);
}
for (int colIndex = 0; colIndex < numColumns; colIndex++) {
    ColumnConstraints cc = new ColumnConstraints();
    cc.setHgrow(Priority.ALWAYS) ; // allow column to grow
    cc.setFillWidth(true); // ask nodes to fill space for column
    // other settings as needed...
    grid.getColumnConstraints().add(cc);
}

In either case, you need to let the nodes grow in order for them to respect the request to grow that the grid pane makes. So, e.g.:

Button myButton = new Button("Click");
myButton.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
grid.add(myButton, 0, 0);

SSCCE (using row and column constraints method):

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

public class KeyPad extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane grid = new GridPane();
        int numRows = 4 ;
        int numColumns = 3 ;
        for (int row = 0 ; row < numRows ; row++ ){
            RowConstraints rc = new RowConstraints();
            rc.setFillHeight(true);
            rc.setVgrow(Priority.ALWAYS);
            grid.getRowConstraints().add(rc);
        }
        for (int col = 0 ; col < numColumns; col++ ) {
            ColumnConstraints cc = new ColumnConstraints();
            cc.setFillWidth(true);
            cc.setHgrow(Priority.ALWAYS);
            grid.getColumnConstraints().add(cc);
        }

        for (int i = 0 ; i < 9 ; i++) {
            Button button = createButton(Integer.toString(i+1));
            grid.add(button, i % 3, i / 3);
        }
        grid.add(createButton("#"), 0, 3);
        grid.add(createButton("0"), 1, 3);
        grid.add(createButton("*"), 2, 3);

        Scene scene = new Scene(grid);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Button createButton(String text) {
        Button button = new Button(text);
        button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        button.setOnAction(e -> System.out.println(text));
        return button ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I position buttons that span multiple columns in JavaFX GridPane?

From Dev

Javafx 2 : How do I delete a row or column in Gridpane

From Dev

JavaFX : How to get column and row index in gridpane?

From Dev

JavaFX : How to get column and row index in gridpane?

From Dev

Centering buttons and text in a GridPane (JavaFX)

From Dev

(JavaFX FXML) While trying to make a custom button, how do I get an image to completely fill a button?

From Dev

How do i get the scrolling bar with buttons?

From Dev

adding dynamic buttons on GridPane FXML JavaFX

From Dev

Buttons in GridPane with prefWidth="Infinity" break JavaFX

From Dev

getting the buttons in a gridPane and change their text using javaFx

From Dev

How to get same vertical size of TitledPanes in a Gridpane? (JavaFX, Scenebuilder)

From Dev

get the number row of gridpane Javafx

From Dev

Get the number of rows in a JavaFX GridPane?

From Dev

How to add buttons to a JavaFX gui via the controller.java file using the fx:id of a GridPane

From Dev

JavaFX: How to shift elements in a GridPane?

From Dev

How do I make one of two inline buttons fill the remaining available space?

From Dev

How do I get values dynamically from radio buttons?

From Dev

How do I get buttons below the soft keyboard in WP 8.1?

From Dev

How do I get images to appear on buttons in Glade Designer?

From Dev

How do I get images to appear on buttons in Glade Designer?

From Dev

How do I get the form to be on the same line as by navigation buttons?

From Dev

How do I get the buttons to stop at first and last image

From Dev

How do I get a header and two buttons to align properly to a border?

From Dev

How do I get full-height bootstrap nav buttons?

From Dev

How do I get a textblock with textwrapping to fill a viewbox

From Dev

How do I constrain nodes added to a GridPane in code?

From Dev

How do I constrain nodes added to a GridPane in code?

From Dev

JavaFX GridPane Dynamic Resizng of Child Nodes to Fill Assigned Area

From Dev

GridPane not visible unless i resize window javaFx

Related Related

  1. 1

    How do I position buttons that span multiple columns in JavaFX GridPane?

  2. 2

    Javafx 2 : How do I delete a row or column in Gridpane

  3. 3

    JavaFX : How to get column and row index in gridpane?

  4. 4

    JavaFX : How to get column and row index in gridpane?

  5. 5

    Centering buttons and text in a GridPane (JavaFX)

  6. 6

    (JavaFX FXML) While trying to make a custom button, how do I get an image to completely fill a button?

  7. 7

    How do i get the scrolling bar with buttons?

  8. 8

    adding dynamic buttons on GridPane FXML JavaFX

  9. 9

    Buttons in GridPane with prefWidth="Infinity" break JavaFX

  10. 10

    getting the buttons in a gridPane and change their text using javaFx

  11. 11

    How to get same vertical size of TitledPanes in a Gridpane? (JavaFX, Scenebuilder)

  12. 12

    get the number row of gridpane Javafx

  13. 13

    Get the number of rows in a JavaFX GridPane?

  14. 14

    How to add buttons to a JavaFX gui via the controller.java file using the fx:id of a GridPane

  15. 15

    JavaFX: How to shift elements in a GridPane?

  16. 16

    How do I make one of two inline buttons fill the remaining available space?

  17. 17

    How do I get values dynamically from radio buttons?

  18. 18

    How do I get buttons below the soft keyboard in WP 8.1?

  19. 19

    How do I get images to appear on buttons in Glade Designer?

  20. 20

    How do I get images to appear on buttons in Glade Designer?

  21. 21

    How do I get the form to be on the same line as by navigation buttons?

  22. 22

    How do I get the buttons to stop at first and last image

  23. 23

    How do I get a header and two buttons to align properly to a border?

  24. 24

    How do I get full-height bootstrap nav buttons?

  25. 25

    How do I get a textblock with textwrapping to fill a viewbox

  26. 26

    How do I constrain nodes added to a GridPane in code?

  27. 27

    How do I constrain nodes added to a GridPane in code?

  28. 28

    JavaFX GridPane Dynamic Resizng of Child Nodes to Fill Assigned Area

  29. 29

    GridPane not visible unless i resize window javaFx

HotTag

Archive