user3718809
user3718809

Reputation:

How do i display the radiobutton and label in the same row?

private String[] gender = {"Male","Female"};

private JComboBox jco = new JComboBox();

private JRadioButton[] jrbGender;
private ButtonGroup buttonGroup = new ButtonGroup();

private JButton jbtAdd = new JButton("Create");
private JButton jbtRetrieve = new JButton("Retrieve");
private JButton jbtUpdate = new JButton("Update");
private JButton jbtDelete = new JButton("Delete");

public RegistrationForMembership(){


    JPanel jp1 = new JPanel(new GridLayout(6,2));
    JPanel jp2 = new JPanel(new FlowLayout());
    JPanel jp3 = new JPanel(new GridLayout(1,1));

    jco = new JComboBox(membership);
    jrbGender = new JRadioButton[gender.length];

    add(jp1);

    jp1.add(new JLabel("Member ID"));
    jp1.add(jtfID);
    jp1.add(new JLabel("Member Name"));
    jp1.add(jtfName);
    jp1.add(new JLabel("Member IC"));
    jp1.add(jtfIC);
    jp1.add(new JLabel("Address"));
    jp1.add(jtfAddress);
    jp1.add(new JLabel("Gender"));

    for(int i =0; i<gender.length;++i){
        jrbGender[i] = new JRadioButton(gender[i]);
        buttonGroup.add(jrbGender[i]);
      jp1.add(jrbGender[i]);

    }

    add(jp1);

The one of the radio button will go to the next line , how do i let the radio button on the same line with the label ?

Upvotes: 0

Views: 538

Answers (1)

Gabriel Negut
Gabriel Negut

Reputation: 13960

Add the JRadioButtons to a new JPanel and add that one to jp1.

JPanel radios = new JPanel();
for (int i = 0; i < gender.length; ++i){
    jrbGender[i] = new JRadioButton(gender[i]);
    buttonGroup.add(jrbGender[i]);
    radios.add(jrbGender[i]);
}
jp1.add(radios);

Also, it looks like jp1 should have 5 rows, not 6.

JPanel jp1 = new JPanel(new GridLayout(5,2));

Upvotes: 1

Related Questions