Search code examples
javaswinglayout-managergridbaglayout

Getting rid of extra spaces between components when the JFrame is resized


I am using the GridBagLayout to arrange some components in a frame.
When the frame is first created, the components have a decent space in between them.enter image description here

But as soon as I resize the frame there are alot of unwanted space between the components enter image description here

I tried adjusting the weights and insets as suggested by some users, but it does not seem to fix the problem

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JTextArea;

public class Frame1 extends JFrame {

    JLabel one = new JLabel("one");
    JLabel two = new JLabel("two");
    JLabel three = new JLabel("three");

    JTextField oneF = new JTextField(20);
    JTextField twoF = new JTextField(20);
    JTextField threeF = new JTextField(20);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("menu");

    GridBagConstraints c = new GridBagConstraints();

    public Frame1() {
        setTitle("GridBagLayout Test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        menuBar.add(menu);

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = c.REMAINDER;
        c.fill = c.HORIZONTAL;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(menuBar, c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(one, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(oneF, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(two, c);

        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(twoF, c);

        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(three, c);

        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(threeF, c);

       //setResizable(false);
       pack();
       setVisible(true);
    }

}

ps:- I am new to GUI programming, so please forgive me for any noob mistakes.

edit 1: This is the what I want to have after I am done. I know the currently it does not look anyway near what I have in mind... I am still working on itenter image description here

Thanks


Solution

  • The idea is to add empty row / columns that will grow to fill the available space:

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JTextField;
    
    public class Frame1 extends JFrame {
    
        JLabel one = new JLabel("one");
        JLabel two = new JLabel("two");
        JLabel three = new JLabel("three");
    
        JTextField oneF = new JTextField(20);
        JTextField twoF = new JTextField(20);
        JTextField threeF = new JTextField(20);
    
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("menu");
    
        public Frame1() {
    
            setTitle("GridBagLayout Test");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            GridBagLayout gridBagLayout = new GridBagLayout();
            gridBagLayout.rowHeights = new int[]{0, 0, 0, 0}; //this defines 4 rows
            //make 2 last empty row grow 
            gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0,1.0};
    
            //do the same for columns 
            gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
            gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0,1.0};
    
            getContentPane().setLayout(gridBagLayout);
    
            menuBar.add(menu);
    
            GridBagConstraints c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 0;
            c.gridwidth = 5;
            c.fill = c.HORIZONTAL;
            c.anchor = c.NORTH;
            c.insets = new Insets(5, 5, 5, 0);
            getContentPane().add(menuBar, c);
    
            //better have a new GridBagConstraints for each component added 
            GridBagConstraints c1 = new GridBagConstraints();
            c1.gridx = 0;
            c1.gridy = 1;
            c1.gridwidth = 1;
            c1.fill = c1.NONE;
            c1.anchor = c1.NORTH;
            c1.insets = new Insets(5, 5, 0, 5);
            getContentPane().add(one, c1);
    
            GridBagConstraints c2 = new GridBagConstraints();
            c2.gridx = 1;
            c2.gridy = 1;
            c2.fill = c2.NONE;
            c2.anchor = GridBagConstraints.NORTHWEST;
            c2.insets = new Insets(5, 5, 0, 5);
            getContentPane().add(oneF, c2);
    
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new Frame1();
        }
    }
    

    EDIT: in response to your edit: use the additional "growing" column for the "cover art"