Search code examples
javaswingpreferredsizespringlayout

Component size in SpringLayout


I use SpringLayout on my form, But as you see, its look isn't good (large and bad size)!

public class t8 extends JFrame {

JButton okButton, cancellButton;
JTextField idTF, nameTf;
JLabel idlbl, namelbl;

public t8() {
    add(createPanel(), BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 500);
    setLocation(400, 100);
    setVisible(true);
}

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

public JPanel createPanel() {
    JPanel panel = new JPanel();
    okButton = new JButton("Ok");
    cancellButton = new JButton("Cancel");
    idTF = new JTextField(10);
    nameTf = new JTextField(10);
    idlbl = new JLabel("ID");
    namelbl = new JLabel("Name");
    panel.add(idlbl);
    panel.add(idTF);
    panel.add(namelbl);
    panel.add(nameTf);
    panel.add(okButton);
    panel.add(cancellButton);

    panel.setLayout(new SpringLayout());
    SpringUtilities.makeCompactGrid(panel, 3, 2, 20, 50, 50, 100);
    return panel;
}
}

I change makeCompactGrid numbers, But was not success!

(The width of JTextFields are large, and my button's size are different) enter image description here


Solution

  • If you don't care about the layout manager, but only care about the layout, then you should use a GridLayout, or if you don't want all component to be the same size, a GridBagLayout. Here's how with a grid layout (only the modified method is shown):

    public JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout());
        okButton = new JButton("Ok");
        cancellButton = new JButton("Cancel");
        idTF = new JTextField(10);
        nameTf = new JTextField(10);
        idlbl = new JLabel("ID");
        namelbl = new JLabel("Name");
        panel.add(idlbl);
        panel.add(idTF);
        panel.add(namelbl);
        panel.add(nameTf);
        panel.add(okButton);
        panel.add(cancellButton);
        return panel;
    }
    

    And with a GridBagLayout:

    public JPanel createPanel() {
        JPanel panel = new JPanel();
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        panel.setLayout(gb);
        okButton = new JButton("Ok");
        cancellButton = new JButton("Cancel");
        idTF = new JTextField(10);
        nameTf = new JTextField(10);
        idlbl = new JLabel("ID");
        namelbl = new JLabel("Name");
        add(panel, idlbl, 0, 0, 1, 1, gb, gbc, false);
        add(panel, idTF, 0, 1, 1, 1, gb, gbc, true);
        add(panel, namelbl, 1, 0, 1, 1, gb, gbc, false);
        add(panel, nameTf, 1, 1, 1, 1, gb, gbc, true);
        add(panel, okButton, 2, 0, 1, 1, gb, gbc, false);
        add(panel, cancellButton, 2, 1, 1, 1, gb, gbc, true);
        return panel;
    }
    
    private void add(Container outer, Component c, int x, int y, int w, int h, GridBagLayout gb, GridBagConstraints gbc, boolean wide) {
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = w;
        gbc.gridheight = h;
        if (wide) {
            gbc.weightx = 100;
        } else {
            gbc.weightx = 0;
        }
        gb.setConstraints(c, gbc);
        outer.add(c);
    }
    

    I believe that the extra GridBagLayout complexity just might be worth it.