Search code examples
javaswingradio-button

Radio Button group not centering even with Component.CENTER_ALIGNMENT and no whitespace


I am essentially making a quiz type of GUI with java swing. This is what I am going for:

https://i.sstatic.net/tqhHt.png

But this is what I'm getting:

https://i.sstatic.net/FQBMb.png

As you can see in the second image, question one's radio buttons are not centered and there is no white space. This is happening despite using Component.CENTER_ALIGNMENT. I have also used createVerticalGlue() to add white space, but it makes no difference.

This is my code:

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;

import java.awt.*;

public class quiz extends JFrame{
    public static void main(String args[]){
        window();
        
        
        
    }

    public static void window(){
        JFrame window = new JFrame("quiz");
        JPanel panel0 = new JPanel();
        BoxLayout ogBox = new BoxLayout(panel0, BoxLayout.Y_AXIS);
        panel0.setLayout(ogBox);
    

        window.getContentPane().setLayout(new BoxLayout(window.getContentPane(), BoxLayout.Y_AXIS));
        JLabel label = new JLabel("question 1.........................................?", SwingConstants.CENTER);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(label, BorderLayout.NORTH);
        JRadioButton o1q1 = new JRadioButton("5");
        o1q1.setVerticalTextPosition(JRadioButton.BOTTOM);
        o1q1.setHorizontalTextPosition(JRadioButton.CENTER);
        JRadioButton o2q1 = new JRadioButton("6");
        o2q1.setVerticalTextPosition(JRadioButton.BOTTOM);
        o2q1.setHorizontalTextPosition(JRadioButton.CENTER);
        JRadioButton o3q1 = new JRadioButton("7");
        o3q1.setVerticalTextPosition(JRadioButton.BOTTOM);
        o3q1.setHorizontalTextPosition(JRadioButton.CENTER);
        JRadioButton o4q1 = new JRadioButton("8");
        o4q1.setVerticalTextPosition(JRadioButton.BOTTOM);
        o4q1.setHorizontalTextPosition(JRadioButton.CENTER);
        JRadioButton o5q1 = new JRadioButton("9");
        o5q1.setVerticalTextPosition(JRadioButton.BOTTOM);
        o5q1.setHorizontalTextPosition(JRadioButton.CENTER);
        JRadioButton o6q1 = new JRadioButton("10");
        o6q1.setVerticalTextPosition(JRadioButton.BOTTOM);
        o6q1.setHorizontalTextPosition(JRadioButton.CENTER);
        ButtonGroup group1 = new ButtonGroup();
        group1.add(o1q1);
        group1.add(o2q1);
        group1.add(o3q1);
        group1.add(o4q1);
        group1.add(o5q1);
        group1.add(o6q1);
        JPanel radio = new JPanel();
        BoxLayout panelBox = new BoxLayout(radio, BoxLayout.X_AXIS);
        radio.setLayout(panelBox);
        radio.add(o1q1);
        radio.add(o2q1);
        radio.add(o3q1);
        radio.add(o4q1);
        radio.add(o5q1);
        radio.add(o6q1);
        radio.setAlignmentX(Component.CENTER_ALIGNMENT);
        radio.add(Box.createVerticalGlue());
        JLabel label2 = new JLabel("question 2.........................................?", SwingConstants.CENTER);
        JPanel panel2 = new JPanel(new BorderLayout());
        panel2.add(label2, BorderLayout.NORTH);
        JRadioButton o1q2 = new JRadioButton("2 things");
        o1q2.setVerticalTextPosition(JRadioButton.BOTTOM);
        o1q2.setHorizontalTextPosition(JRadioButton.CENTER);
        JRadioButton o2q2 = new JRadioButton("3 things");
        o2q2.setVerticalTextPosition(JRadioButton.BOTTOM);
        o2q2.setHorizontalTextPosition(JRadioButton.CENTER);
        JRadioButton o3q2 = new JRadioButton("4 things");
        o3q2.setVerticalTextPosition(JRadioButton.BOTTOM);
        o3q2.setHorizontalTextPosition(JRadioButton.CENTER);
        JRadioButton o4q2 = new JRadioButton("5 things");
        o4q2.setVerticalTextPosition(JRadioButton.BOTTOM);
        o4q2.setHorizontalTextPosition(JRadioButton.CENTER);
        JRadioButton o5q2 = new JRadioButton("6 things");
        o5q2.setVerticalTextPosition(JRadioButton.BOTTOM);
        o5q2.setHorizontalTextPosition(JRadioButton.CENTER);
        JRadioButton o6q2 = new JRadioButton("7 things");
        o6q2.setVerticalTextPosition(JRadioButton.BOTTOM);
        o6q2.setHorizontalTextPosition(JRadioButton.CENTER);
        ButtonGroup group2 = new ButtonGroup();
        group2.add(o1q2);
        group2.add(o2q2);
        group2.add(o3q2);
        group2.add(o4q2);
        group2.add(o5q2);
        group2.add(o6q2);
        JPanel radio2 = new JPanel();
        BoxLayout panelBox2 = new BoxLayout(radio2, BoxLayout.X_AXIS);
        radio2.setLayout(panelBox2);
        radio2.add(o1q2);
        radio2.add(o2q2);
        radio2.add(o3q2);
        radio2.add(o4q2);
        radio2.add(o5q2);
        radio2.add(o6q2);
        radio2.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel0.add(panel);
        panel0.add(panel2);
        panel.add(radio);
        panel2.add(radio2);
        window.setLayout(new FlowLayout());
        window.add(panel0);
        window.setResizable(false);
        window.setDefaultCloseOperation(EXIT_ON_CLOSE);
        window.setSize(new Dimension(900, 500));
        panel.setVisible(true);
        window.setVisible(true);
    

    }
}

Thanks for the help.


Solution

  • I'd consider using a GridBagLayout, for example...

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    
    public class Main {
        public static void main(String[] args) {
            new Main();
        }
    
        public Main() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
            public TestPane() {
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = gbc.REMAINDER;
                gbc.insets = new Insets(8, 8, 8, 8);
    
                add(makeQuestionPane("Question One .....................?", new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}), gbc);
                add(makeQuestionPane("Question Two .....................?", new String[]{"A", "B", "C", "D", "E"}), gbc);
            }
    
            protected JPanel makeQuestionPane(String question, String[] options) {
                JPanel questionPane = new JPanel(new BorderLayout());
                JLabel label = new JLabel(question, JLabel.CENTER);
                questionPane.add(label, BorderLayout.NORTH);
                ButtonGroup q1BG = new ButtonGroup();
                JPanel optionsPane = new JPanel(new GridBagLayout());
                for (String option : options) {
                    JRadioButton btn = new JRadioButton(option);
                    q1BG.add(btn);
                    optionsPane.add(btn);
                }
                questionPane.add(optionsPane);
    
                return questionPane;
            }
        }
    }
    

    But remember, you're not stuck to using a single layout. For example, the above example uses both a GridBagLayout and BorderLayout to layout each question pane.

    Take a closer look at Laying Out Components Within a Container