Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JRadioButtonMenuItem & Check Selected State

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:

Java Swing JRadioButtonMenuItem Example
Java Swing JRadioButtonMenuItem 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.

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).

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.

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.

6. JRadioButtonMenuItem & ItemListener

We created class SwingRadioMenuItem for our example. This JFrame extended class implements ItemListener so that it can capture the ItemEvent.

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.

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.

8. Youtube – Demo

9. Code Reference

9.1 MainEntry.java

9.2 SwingRadioMenuItem.java

Categories: Swing

Tags: , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.