RoiEX RoiEX - 1 year ago 100
Java Question

JPanel GridBagLayout to GridPane

I'm currently trying to migrate an application to JavaFX (It's actually partially using AWT) and while switching JPanels with border layouts to BorderPanes is fairly easy, I'm having quite some trouble figuring out on how to do that with the GridBagLayout and GridPanes. (I never used this Layout in Swing before)

In my code, the GridBagLayout is used 2 times (and I'm not sure if this is auto generated Code):

JPanel bottomMessagePanel = new JPanel();
bottomMessagePanel.setLayout(new GridBagLayout());
bottomMessagePanel.add(someJComponent, new GridBagConstraints(0, 0, 1, 1, .35, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
bottomMessagePanel.add(someOtherJComponent, new GridBagConstraints(1, 0, 1, 1, .65, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));


JPanel stepPanel = new JPanel();
stepPanel.setLayout(new GridBagLayout());
stepPanel.add(someJComponent, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
stepPanel.add(someOtherJComponent, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
stepPanel.add(some3rdJComponent, new GridBagConstraints(2, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

How would I do this using a JavaFX GridPane?

Don't worry about converting those JComponents, since I already converted those...

Any help is very appreciated!

Answer Source

You specify the GridBagConstraints passing the values to the constructor.

        int gridx,
        int gridy,
        int gridwidth,
        int gridheight,
        double weightx,
        double weighty,
        int anchor,
        int fill,
        Insets insets,
        int ipadx,
        int ipady)

Let's describe how to use the equivalents of those parameters in JavaFX:

Node node = ...
GridPane gridPane = ...

gridx, gridy, gridwidth, gridheight

Usually you use the appropriate add method of GridPane to specify these values. They are called columnIndex, rowIndex, columnSpan and rowSpan in JavaFX. If columnSpan and rowSpan are 1, it suffices to use the add method taking 3 parameters:

gridPane.add(node, gridx, gridy);

If one of the columnSpan/rowSpan is greater, you can use a overloaded version of the method:

gridPane.add(node, gridx, gridy, gridwidth, gridheight);

weightx, weighty

Those do not have a direct equivalent. Instead you'd need to define this for a whole row/column by using percentWidth and percentHeight of the ColumnConstraints and the RowConstraints (that is unless you're satisfied with the standard layout).

Row and column constraints are added to the columnConstraints and the rowConstraints lists.


You can use the halignment/valignment properties of the ColumnConstrants/RowConstraints for this or specify this for the individual node using GridPane.setHalignment and GridPane.setValignment:

GridPane.setHalignment(node, HPos.LEFT);
GridPane.setValignment(node, VPos.TOP);


The equivalent of this is either setting the fillHeight and fillWidth values of row and column constraints of specifying this for individual node using GridPane.setFillWidth and GridPane.setFillHeight:

GridPane.setFillWidth(node, Boolean.FALSE);
GridPane.setFillHeight(node, Boolean.FALSE);

The default value is true for these properties.


You can specify this by using GridPane.setMargin. If you're using 0 for all values, you don't need to specify this.

GridPane.setMargin(node, new Insets(top, right, bottom, left));

ipadx, ipady

There is no equivalent for this in JavaFX.