Question

I want to create a JFrame by hand and use spring layout to do this. But, my finally output is not good. The space between my rows is so much, and between my radio buttons too:

example output

My code:

public final class NewUserFrame1 extends JFrame {

public NewUserFrame1() {
    add(rowComponent(), BorderLayout.CENTER);
    setLocation(200, 40);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
    pack();
}

public JPanel rowComponent() {

    JPanel panel = new JPanel();
    JLabel fnamelbl = new JLabel("First name");
    JLabel lnamelbl = new JLabel("Last Name");
    JLabel fntemp = new JLabel();
    JLabel lntemp = new JLabel();
    JTextField fntf = new JTextField(10);
    JTextField lntf = new JTextField(10);
    JLabel gndlnl = new JLabel("Gender");
    JRadioButton malerb = new JRadioButton("Male");
    JRadioButton femalerb = new JRadioButton("Female");
    ButtonGroup bgroup = new ButtonGroup();
    bgroup.add(malerb);
    bgroup.add(femalerb);
    JLabel registnm = new JLabel("Registration ID is:");
    JLabel showreglbl = new JLabel();
    JLabel regtemp = new JLabel();

    panel.add(fnamelbl);
    panel.add(fntf);
    panel.add(fntemp);
    panel.add(lnamelbl);
    panel.add(lntf);
    panel.add(lntemp);
    panel.add(gndlnl);
    panel.add(malerb);
    panel.add(femalerb);
    panel.add(registnm);
    panel.add(showreglbl);
    panel.add(regtemp);

    panel.setLayout(new SpringLayout());
    SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 4);
    return panel;
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            NewUserFrame1 newUserFrame1 = new NewUserFrame1();
        }
    });
}
}

Now: enter image description here

Was it helpful?

Solution

Instead of calling setSize call pack on JFrame within your NewUserFrame1 constructor.

public NewUserFrame1() {
    add(rowComponent(), BorderLayout.CENTER);
    setLocation(200, 40);
    //setSize(800, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
    pack();
}

Also change the parameters of SpringUtilities.makeCompactGrid method in following way:

SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 4);//change yPad to 4 instead of 100. It sets the vertical height between two rows

OTHER TIPS

  1. Your code is not compilable (missing imports).
  2. You wrote:

    SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 100);

The last argument is yPad. Change this to 10 (or lower value if you want), for example:

SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 10);

But still - label will be to high etc., but it's a different issue. Keep playing with panel's size and your component's size.

In case of radio buttons - change

panel.add(malerb);
panel.add(femalerb);

To something like:

JPanel radioPanel = new JPanel();
radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
radioPanel.add(malerb);
radioPanel.add(femalerb);
panel.add(radioPanel);
panel.add(new JLabel());

The last line is needed because you declared your layout to have 3 columns.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top