Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Accessing the selected value from a swing radio button group

801424Feb 6 2009 — edited Feb 7 2009
I am using NetBeans to create a Swing GUI. I want to have a panel with three radio buttons and a next button. When the user clicks next I want the selected radio buttons value to be printed out by the System.out.println(); statement. It all seems to works but I can’t access the selected value from the nextP2BtnActionPerformed I am getting null. I have cut and pasted the relevant code from my program below. Thanks for any help.

buttonGroup1 = new javax.swing.ButtonGroup();

jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();

buttonGroup1.add(jRadioButton1);
jRadioButton1.setSelected(true);
jRadioButton1.setText("1. Standard");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});

buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("2. Double sided");

buttonGroup1.add(jRadioButton3);
jRadioButton3.setText("3. Triple sided");

//Then I have the action listeners below the first one is for a button nextP2Btn.
I thought that there would be an easy way to take the selected value from the buttonGroup.

private void nextP2BtnActionPerformed(java.awt.event.ActionEvent evt) {


ButtonModel b = this.buttonGroup1.getSelection();
System.out.println(b.getSelectedObjects());
}

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(evt.getActionCommand());

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 7 2009
Added on Feb 6 2009
1 comment
452 views