Programming Examples

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

JRadioButton & ItemListener

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:

JRadioButton Example
JRadioButton Example

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.

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.

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.

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



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.