1. About JRadioButton
The JRadioButton is a swing class which gives the UI behaviour of the Radio Button. A radio button like Toggle Button and Checkbox can maintains two states—Selected, Unselected. Recall, a radio button can be in a group and within a group, only one radio button can be in the selected state. In java swing, we can group the radio buttons using the ButtonGroup. If you want two groups of the radio buttons, you must create two button groups and give the needed radio buttons to them. Like Java Swing JCheckBox, JRadioButton also fires the ItemEvent when state change takes place.
2. About JRadioButton Example
The Example which will create is below:

Our example shows three radio buttons in a form. A user can select any one of these radio buttons and only one radio button will be in the selected state. Above, the radio button three is in the selected state. When a radio button is in checked (selected) state, our example will flash a message box stating which radio button is in the selected state. Here in this example, we will create all these radio buttons using JRadioButton component of the Java Swing.
3. Create the Components
After setting up the flow layout to the content pane of the Java Swing Frame, we create three radio buttons using the JRadioButton swing class. The constructor takes a string, and the UI item will display the string as radio button’s caption. In our case, we set the captions as Radio 1, Radio 2 and Radio 3.
1 2 3 4 5 6 7 8 9 |
//Sample 01: Set Size and Position setBounds(100, 100, 450, 110); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Radio Buttons JRadioButton rad1 = new JRadioButton("Radio 1"); JRadioButton rad2 = new JRadioButton("Radio 2"); JRadioButton rad3 = new JRadioButton("Radio 3"); |
4. Grouping JRadioButton
The ButtonGroup class of the Java Swing is useful for grouping the control. We used this already for the JToggleButton as Radio example. Here, in the same way, we add all three of our radio buttons to the button group bg1
. This way, we have one radio button group and the user can only one button in the selected state within this bg1
group. After grouping the radio button under a group, we added those buttons to the content pane of the JFrame via
add method. Note, we add only the radio buttons and not the ButtonGroup, as it is not a component.
1 2 3 4 5 6 7 8 |
//Sample 03: Add to Button Group ButtonGroup bg1 = new ButtonGroup(); bg1.add(rad1); bg1.add(rad2); bg1.add(rad3); ControlHost.add(rad1); ControlHost.add(rad2); ControlHost.add(rad3); |
5. Handling ItemEvent
Like JCheckBox, JRadioButton also produces ItemEvent when the user alters the state of it. ItemListener’s method itemStateChanged will get this item event. In the below code, we have an anonymous handler for the first radio button. Here, we use the isSelected method of the first radio button and this method returns true when the radio button is in the selected state.
Java Swing’s JOptionPane
has static methods to display the message boxes. Here, we use the showMessageDialog
to show INFO message to the user stating the radio button is selected by the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Sample 04: Handle Item Event rad1.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (rad1.isSelected()) { JOptionPane.showMessageDialog( JFrameDemo.this, "Radio 1 option Picked", "Selected Option", JOptionPane.INFORMATION_MESSAGE); } } }); |
One can write the same handler for the other radio buttons as well. The code reference section has the full code for this example. The next part shows this example as a Youtube demo. Also note, one can also use AWT radio button to get the same result. One can create two button group to have two sets of the radio buttons.
6. Youtube Demo
7. Code Reference
7.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 07: Create Instance of JFrameDemo JavaSwingRadioButtonDemo frame = new JavaSwingRadioButtonDemo("JRadioButton Example"); frame.setVisible(true); } } |
7.2 JavaSwingRadioButtonDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
package tube.coding.examples; import java.awt.Container; import java.awt.FlowLayout; import java.awt.HeadlessException; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JRadioButton; import javax.swing.JTextField; public class JavaSwingRadioButtonDemo extends JFrame { public JavaSwingRadioButtonDemo(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 450, 110); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Radio Buttons JRadioButton rad1 = new JRadioButton("Radio 1"); JRadioButton rad2 = new JRadioButton("Radio 2"); JRadioButton rad3 = new JRadioButton("Radio 3"); //Sample 03: Add to Button Group ButtonGroup bg1 = new ButtonGroup(); bg1.add(rad1); bg1.add(rad2); bg1.add(rad3); ControlHost.add(rad1); ControlHost.add(rad2); ControlHost.add(rad3); //Sample 04: Handle Item Event rad1.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (rad1.isSelected()) { JOptionPane.showMessageDialog( JavaSwingRadioButtonDemo.this, "Radio 1 option Picked", "Selected Option", JOptionPane.INFORMATION_MESSAGE); } } }); rad2.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (rad2.isSelected()) { JOptionPane.showMessageDialog( JavaSwingRadioButtonDemo.this, "Radio 2 option Picked", "Selected Option", JOptionPane.INFORMATION_MESSAGE); } } }); rad3.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (rad3.isSelected()) { JOptionPane.showMessageDialog( JavaSwingRadioButtonDemo.this, "Radio 3 option Picked", "Selected Option", JOptionPane.INFORMATION_MESSAGE); } } }); } } |
Categories: Swing
Tags: isSelected, itemStateChanged, JOptionPane, JRadioButton, showMessageDialog