1. Introduction to JRadioButtonMenuItem
In the past example, we created and used JCheckBoxMenuItem. Here, we will create an example to learn Java Swing’s JRadioButtonMenuItem. This component class serves as a radio button which can be placed on the JMenu. A ButtonGroup object can group these radio menu items. The user can select only one radio button menu item within a group. This means, when we select an item, the formerly selected radio button goes from selected to un-selected state. Like other menu items, a Radio Button Menu also will raise ItemEvent.
2. About the examples
The below screenshot shows the example:

This example has a fruits menu and a JLabel in the JFrame. The fruit menu contains four Radio button menu items and the user can pick only one among them. In the JLabel, the example displays what menu item is picked by the user. Now, let us create this example.
3. Create JRadioButtonMenuItem
In the below code, we create Fruits JMenu and then add it to the JMenuBar. Code snippet 2 creates JMenuBar and adds it to the JFrame. Snippet 3 creates JMenu and adds it to the menu bar. The fruits menu is empty at this stage. Code snippet 4 creates four JRadioButtonMenuItem objects, and the string parameter is for the label caption, which will be displayed next to the Radio-mark.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public SwingRadioMenuItem(String title) { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 02: Create Menu Bar and add to JFrame JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); //Sample 03: Create Menu and Add it to Menu bar JMenu mnuFruits = new JMenu("Fruits"); menubar.add(mnuFruits); //Sample 04: Create Menu Items JRadioButtonMenuItem miApple = new JRadioButtonMenuItem("Apple"); JRadioButtonMenuItem miBanana = new JRadioButtonMenuItem("Banana"); JRadioButtonMenuItem miCherry = new JRadioButtonMenuItem("Cherry"); JRadioButtonMenuItem miGrapes = new JRadioButtonMenuItem("Grapes"); |
4. Group JRadioButtonMenuItem via ButtonGroup
Next, we will add all the JRadioButtonMenuItem components to the Fruits menu. But before that, we should group them so that the user can pick only one item at a time. We create a ButtonGroup object and add all the menu items to it. In the below code, snippet 05 adds the menu items to a distinct group and snippet 6 adds the menu item to the JMenu (Fruits).
1 2 3 4 5 6 7 8 9 10 11 12 |
//Sample 05: Add Menu Items to Button Group ButtonGroup bgrp = new ButtonGroup(); bgrp.add(miApple); bgrp.add(miBanana); bgrp.add(miCherry); bgrp.add(miGrapes); //Sample 06: Add Menu Items to Menu mnuFruits.add(miApple); mnuFruits.add(miBanana); mnuFruits.add(miCherry); mnuFruits.add(miGrapes); |
5. Add JLabel to show Radio Menu Item Events
Our example will show the user-selected radio button on a label. So we create a JLabel as a class member, as the handler method will pick it up.
1 2 3 4 5 |
public class SwingRadioMenuItem extends JFrame implements ItemListener { //Sample 07: Label to show selected Radio Menu Item JLabel lblStatus = new JLabel(); |
In the constructor, we add this JLabel to the JFrame window. As we do not pass any argument to the add method, the label will stay in the centre of the JFrame. Note, we give BorderLayout manager to our JFrame for dealing with the component alignment and, by default, the added control goes to the centre of the layout.
1 2 |
//Sample 08: Add Label to Frame add(lblStatus); |
6. JRadioButtonMenuItem & ItemListener
We created class SwingRadioMenuItem for our example. This JFrame extended class implements ItemListener so that it can capture the ItemEvent.
1 2 3 4 |
//Sample 09: Implement Item Listener public class SwingRadioMenuItem extends JFrame implements ItemListener { |
Next, we register all our JRadioButtonMenuItem with ItemListener via the call addItemListener. The method receives ‘this’ as an argument as our JFrame extended class implements ItemListener as well.
1 2 3 4 5 |
//Sample 10: Register all Radio Menu Item with Item Listener miApple.addItemListener(this); miBanana.addItemListener(this); miCherry.addItemListener(this); miGrapes.addItemListener(this); |
7. Notify Check State of JRadioButtonMenuItem
Since our JFrame implements ItemListener, we write the handler function: itemStateChanged. The method takes ItemEvent as an argument and from this we get the event source via the call getSource. Means, we get the JRadioButtonMenuItem which raised this ItemEvent. Next, the method call isSelected tells whether the Radio Menu item is in checked state or not. Note, when you place a check-mark in one radio menu item, previously checked menu item goes from checked to unchecked state. When the event maker is in checked state, we update the JLabel stating the clicked Radio button menu item is in selected state.
1 2 3 4 5 6 7 8 |
//Sample 11: Show Selected Radio Buttons @Override public void itemStateChanged(ItemEvent e) { JRadioButtonMenuItem EventSource = (JRadioButtonMenuItem) e.getSource(); if (EventSource.isSelected()== true) lblStatus.setText(EventSource.getText() + " Radio is Selected"); } |
8. Youtube – Demo
9. Code Reference
9.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) { SwingRadioMenuItem frame = new SwingRadioMenuItem("Radio Menu Items"); frame.setVisible(true); } } |
9.2 SwingRadioMenuItem.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 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; //Sample 09: Implement Item Listener public class SwingRadioMenuItem extends JFrame implements ItemListener { //Sample 07: Label to show selected Radio Menu Item JLabel lblStatus = new JLabel(); public SwingRadioMenuItem(String title) { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 02: Create Menu Bar and add to JFrame JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); //Sample 03: Create Menu and Add it to Menu bar JMenu mnuFruits = new JMenu("Fruits"); menubar.add(mnuFruits); //Sample 04: Create Menu Items JRadioButtonMenuItem miApple = new JRadioButtonMenuItem("Apple"); JRadioButtonMenuItem miBanana = new JRadioButtonMenuItem("Banana"); JRadioButtonMenuItem miCherry = new JRadioButtonMenuItem("Cherry"); JRadioButtonMenuItem miGrapes = new JRadioButtonMenuItem("Grapes"); //Sample 05: Add Menu Items to Button Group ButtonGroup bgrp = new ButtonGroup(); bgrp.add(miApple); bgrp.add(miBanana); bgrp.add(miCherry); bgrp.add(miGrapes); //Sample 06: Add Menu Items to Menu mnuFruits.add(miApple); mnuFruits.add(miBanana); mnuFruits.add(miCherry); mnuFruits.add(miGrapes); //Sample 08: Add Label to Frame add(lblStatus); //Sample 10: Register all Radio Menu Item with Item Listener miApple.addItemListener(this); miBanana.addItemListener(this); miCherry.addItemListener(this); miGrapes.addItemListener(this); } //Sample 11: Show Selected Radio Buttons @Override public void itemStateChanged(ItemEvent e) { JRadioButtonMenuItem EventSource = (JRadioButtonMenuItem) e.getSource(); if (EventSource.isSelected()== true) lblStatus.setText(EventSource.getText() + " Radio is Selected"); } } |
Categories: Swing
Tags: ButtonGroup, isSelected, ItemListener, JMenu, JMenuBar, JRadioButtonMenuItem