Search code examples
javaswinglayout-managergridbaglayout

How to use the GridBagLayout to create 3 JPanels one on top of the other, of variable height


I want to write a simple text editor in java.

I have organized my layout and came to a conclusion that I basically need 3 JPanels, one on top of the other. The 1st and the 2nd one will be very short in height because they will be menubar and a JPanel containg 2 JLabels respectively. The middle one need to be the one with the most height, because all the text will be contained in it.

I think I need to use a GridBagLayout but that does not work, I need them to occupy the big one 10x more than the small ones. And all of them will utilize as much width as provided by the JFrame.

So far the code snippet is -

GridBagConstraints gbc = new GridBagConstraints
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
mainFrame.add(upperGrid, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridheight = 10;
mainFrame.add(upperGrid, gbc);
gbc.gridx = 0;
gbc.gridy = 11;
mainFrame.add(upperGrid, GBC);

What I get as a result is this -

Distorted GridBagLayout


Solution

  • I suggest you drop the idea of the GridLayout. I'd do the following instead:

    1. Use JMenuBar for your menu bar (https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html)

    2. Use a BorderLayout:

      JFrame frame = new JFrame();
      JPanel topPanel = new JPanel();
      topPanel.setLayout(new FlowLayout());
      topPanel.add(new JLabel("Label 1"));
      topPanel.add(new JLabel("Label 2"));
      frame.add(topPanel, BorderLayout.NORTH);
      JPanel bigPanel = new JPanel();
      frame.add(bigPanel, BorderLayout.CENTER);
      

    You can use a GridLayout when you need to arrange a dialog with a lot of text fields for example. But for this "rougher" stuff, a BorderLayout is better, also because its probably faster. (Probably, I don't know for sure)

    EDIT: If you absolutely have to use a GridBagLayout, then you could do the following:

    JPanel panel = new JPanel();
    GridBagLayout layout = new GridBagLayout();
    layout.columnWidths = new int[] { 0, 0 };
    layout.rowHeights = new int[] { 0, 0, 0, 0 };
    layout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
    layout.rowWeights = new double[] { 0.0, 0.0, 1.0, Double.MIN_VALUE };
    panel.setLayout(layout);
    
    JPanel menuBar = new JPanel();
    GridBagConstraints contraints = new GridBagConstraints();
    contraints.fill = GridBagConstraints.BOTH;
    contraints.gridx = 0;
    contraints.gridy = 0;
    panel.add(menuBar, contraints);
    
    JPanel panelForLabels = new JPanel();
    contraints = new GridBagConstraints();
    contraints.fill = GridBagConstraints.BOTH;
    contraints.gridx = 0;
    contraints.gridy = 1;
    panel.add(panelForLabels, contraints);
    
    JPanel bigPanel = new JPanel();
    contraints = new GridBagConstraints();
    contraints.fill = GridBagConstraints.BOTH;
    contraints.gridx = 0;
    contraints.gridy = 2;
    panel.add(bigPanel, contraints);