How to jRadioButton Select One Option at a Time in Java Netbeans

Whenever you use multiple jRadioButton in java, you have to face problem that it can not work as jRadioButton select one option at a time in java when you click on that particular option.

When you select one option and then jump or select other option, then previous option also selected including current selected option. So, get rid of this problem, you have to try or use following two ways described in SKOTechLearn Tips.

  1. jRadioButton Select One Option in Java through ‘ItemStateChanged’ events 
  2. Use ButtonGroup for Select One Option at a Time in Java

So, before we start, We have to drag this control , then change its Name and Text according to your self.

 (1). jRadioButton Select One Option in java through ‘ItemStateChanged’ Events

Suppose we drag 3 jRadioButton with name MyOption1, MyOption2, MyOption3. So, we have to write code inside every RadioButton’s ‘ItemStateChanged’ events.

jRadioButton ‘ItemStateChanged’ events

If MyOption1 is selected then write following code:
 private void MyOption1ItemStateChanged(java.awt.event.ItemEvent evt) { 
        if (MyOption1.isSelected()){
            MyOption2.setSelected(false);
            MyOption3.setSelected(false);
        }
    }

If MyOption2 is selected then write following code:
 private void MyOption2ItemStateChanged(java.awt.event.ItemEvent evt) { 
        if (MyOption2.isSelected()){
            MyOption1.setSelected(false);
            MyOption3.setSelected(false);
        }
    }

If MyOption3 is selected then write following code:
 private void MyOption3ItemStateChanged(java.awt.event.ItemEvent evt) { 
        if (MyOption3.isSelected()){
            MyOption1.setSelected(false);
            MyOption2.setSelected(false);
        }
    }
jRadioButton ItemStateChanged Event in java
ItemStateChanged Event

As you can see that when write this code and run application. And when you select any option, others will be deselected.

 SetSelected(true) : Is Use For Selection
 SetSelected(false) : Is Use For Deselecting


 2. Use ButtonGroup for Select One Option at a Time in Java:

Now, first we talk about ButtonGroup, Basically ButtonGroup class is use for selecting only one RadioButton and deselects remain RadioButton.

So, this can be done by adding all related jRadioButton in ButtonGroup.

Now, drag ButtonGroup on jFrame. Then write following code inside your jFrame class or ‘formWindowOpened’ events.

Import ButtonGroup class

First, you have to Import ButtonGroup class:
import javax.swing.ButtonGroup;

Add jRadioButton on ButtonGroup Class

If you want to initialize it in jFrame Class then write code like following way:
public MyRadioButtonFrame() {
        initComponents();
        ButtonGroup MyOptiongroup = new ButtonGroup();
        MyOptiongroup.add(MyOption1);
        MyOptiongroup.add(MyOption2);
        MyOptiongroup.add(MyOption3); 
  }

Or you can initialize it in jFrame’s ‘formWindowOpened’ events like bellow:
public formWindowOpened(java.awt.event.WindowEvent evt) {
        ButtonGroup MyOptiongroup = new ButtonGroup();
        MyOptiongroup.add(MyOption1);
        MyOptiongroup.add(MyOption2);
        MyOptiongroup.add(MyOption3); 
  }

jRadioButton Selected Option Example

If you want to display Selected Text from jRadiobutton on jLabel then go to following example.
private void MyOption1ItemStateChanged(java.awt.event.ItemEvent evt) {
        if(MyOption1.isSelected()){
            MyLabel1.setText("MyOption1 is Selected");
        }
    }

private void MyOption2ItemStateChanged(java.awt.event.ItemEvent evt) {
        if(MyOption2.isSelected()){
            MyLabel1.setText("MyOption2 is Selected");
        }
    }

private void MyOption3ItemStateChanged(java.awt.event.ItemEvent evt) {
        if(MyOption3.isSelected()){
            MyLabel1.setText("MyOption3 is Selected");
        }
    }
jRadioButton Option Select Example in Java
RadioButton Option Select Example

These two point will be use as jRadioButton Select one Option at a time in java Netbeans with SKOTechLearn Tips.

1 comment: