Search code examples
javaswinguser-interfacegridbaglayoutjradiobutton

Component not appearing when adding to panel


So I'm trying to create a series of radio buttons and check boxes that are displayed as follows:

          Radio Button
Check Box
          Radio Button
Check Box
          Radio Button

However, I'm still in the learning process for java and I was wondering if anyone could solve this problem. At the moment the buttons and boxes are being displayed in the correct location, however the first radio button ("Courier") is not being displayed for some reason. If you could perhaps describe the reason and a possible solution that'd be great.

Thanks

Updated Code:

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

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Question2 {
    public static void main(String[] args) {
        MyFrame f = new MyFrame("Font Chooser");
        f.init();
    }
}

class MyFrame extends JFrame {
    MyFrame(String title) {
        super(title);
    }

    private JPanel mainPanel;
    private GridBagConstraints gbc = new GridBagConstraints();
    private GridBagLayout gbLayout = new GridBagLayout();

    void init() {
        mainPanel = new JPanel();
        mainPanel.setLayout(gbLayout);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
        this.setContentPane(mainPanel);

        gbc.gridx = 0;
        gbc.gridy = 1;

        JCheckBox cb = new JCheckBox("Bold");
        gbLayout.setConstraints(cb, gbc);
        mainPanel.add(cb);
        gbc.gridy = 3;
        gbLayout.setConstraints(cb, gbc);
        cb = new JCheckBox("Italic");
        mainPanel.add(cb);

        gbc.gridx = 1;
        gbc.gridy = 0;

        JRadioButton rb = new JRadioButton("Times");
        gbLayout.setConstraints(rb, gbc);
        mainPanel.add(rb, gbc);
        gbc.gridy = 2;
        rb = new JRadioButton("Helvatica");
        mainPanel.add(rb, gbc);
        rb = new JRadioButton("Courier");
        gbc.gridy = 4; 
        mainPanel.add(rb, gbc);


        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}

Solution

  • 3 issues

    • Y Coordinate not re-assigned to different value causing last 2 radio buttons to exist at same location
    • GridBagConstraints not being used for left-hand side components
    • setConstraints erroneously being used to set constraints

    Resultant code:

    public class GoodGridBagApp {
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame frame = new JFrame("Font Chooser");
                    frame.add(getMainPanel());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setVisible(true);
                }
    
                private JPanel getMainPanel() {
    
                    JPanel mainPanel = new JPanel();
                    GridBagConstraints gbc = new GridBagConstraints();
                    mainPanel.setLayout(new GridBagLayout());
                    mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
    
                    gbc.gridx = 1;
                    gbc.gridy = 2;
    
                    JCheckBox cb = new JCheckBox("Bold");
                    mainPanel.add(cb, gbc);
                    gbc.gridy = 4;
                    cb = new JCheckBox("Italic");
                    mainPanel.add(cb, gbc);
    
                    gbc.gridx = 2;
                    gbc.gridy = 1;
    
                    JRadioButton rb = new JRadioButton("Times");
                    mainPanel.add(rb, gbc);
                    gbc.gridy = 3;
                    rb = new JRadioButton("Helvatica");
                    mainPanel.add(rb, gbc);
                    rb = new JRadioButton("Courier");
                    gbc.gridy = 5;
                    mainPanel.add(rb, gbc);
    
                    return mainPanel;
                }
            });
        }
    }
    

    Read: How to Use GridBagLayout