Trent Trent - 6 months ago 21
Java Question

Java Swing JPanel FlowLayout Height Issues

I'm busy writing a Java Swing GUI and I need two JPanes Side by Side.

I have a Single JPane that I am using as a Parent JPane which holds
two Child JPanes that are sitting side by side each one with a border
for separation.

Each Child JPane has a JPane inside with a GridBagLayout for
positioning elements. My code is as follows

import javax.swing.*;
import java.awt.*;

public class JavaGui extends JFrame {

public static void main(String[] args) {

JavaGui window = new JavaGui();

// setup window here

// main panels

JPanel container = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel panelLeft = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel panelRight = new JPanel(new FlowLayout(FlowLayout.LEFT));

window.add(container);
container.add(panelLeft);
container.add(panelRight);

// grid layout panels

JPanel leftGrid = new JPanel(new GridBagLayout());
JPanel rightGrid = new JPanel(new GridBagLayout());
GridBagConstraints grid = new GridBagConstraints();

panelLeft.add(leftGrid);
panelRight.add(rightGrid);

// test labels

JLabel label01 = new JLabel("Label 01");
JLabel label02 = new JLabel("Label 02");
JLabel label03 = new JLabel("Label 03");
JLabel label04 = new JLabel("Label 04");

// add labels to grids

grid.gridx = 0;
grid.gridy = 0;
leftGrid.add(label01, grid);
grid.gridx = 0;
grid.gridy = 0;
rightGrid.add(label02, grid);
grid.gridx = 0;
grid.gridy = 1;
rightGrid.add(label03, grid);
grid.gridx = 0;
grid.gridy = 2;
rightGrid.add(label04, grid);

// add borders to see sizes

panelLeft.setBorder(BorderFactory.createLineBorder(Color.black));
panelRight.setBorder(BorderFactory.createLineBorder(Color.black));

};

};


This does what I expected however when I add borders to see the sizes of
my Left and Right Panels I see that they have different heights as
shown bellow when it is run

CURRENT

enter image description here

Is it possible to get these Panels to be the same height?

REQUIRED

enter image description here

Answer

I suggest you try the following :

import javax.swing.*;
import java.awt.*;

public class JavaGui extends JFrame {

   public static void main(String[] args) {

      JavaGui window = new JavaGui();

      // setup window here

      // main panels    

      JPanel container = new JPanel(new FlowLayout(FlowLayout.LEFT));
      JPanel panelLeft = new JPanel(new GridBagLayout());
      JPanel panelRight = new JPanel(new GridBagLayout());
      JPanel centralPanel = new JPanel(new GridLayout(1, 2));
      centralPanel.add(panelLeft);
      centralPanel.add(panelRight);
      window.add(container);
      container.add(centralPanel);

      // grid layout panels

      GridBagConstraints grid = new GridBagConstraints();


      // test labels

      JLabel label01 = new JLabel("Label 01");
      JLabel label02 = new JLabel("Label 02");
      JLabel label03 = new JLabel("Label 03");
      JLabel label04 = new JLabel("Label 04");

      // add labels to grids

      grid.gridx = 0;
      grid.gridy = 0;
      panelLeft.add(label01, grid);
      grid.gridx = 0;
      grid.gridy = 0;
      panelRight.add(label02, grid);
      grid.gridx = 0;
      grid.gridy = 1;
      panelRight.add(label03, grid);
      grid.gridx = 0;
      grid.gridy = 2;
      panelRight.add(label04, grid);

      // add borders to see sizes

      panelLeft.setBorder(BorderFactory.createLineBorder(Color.black));
      panelRight.setBorder(BorderFactory.createLineBorder(Color.black));
      window.setVisible(true);
      window.pack();
   };

};