event
JRadioButton with ItemListener example
In this example we are going to see how to use JRadioButton bundled with an ItemListener. As you know, radio buttons a quite popular in Graphical Applications because they give flexibility to the end users, and it’s very natural to them to interact with radio buttons when you want to give specific values to choose from.
In short, to use JRadioButtons with ItemListener you have to:
- Create a new class that implements
ItemListenerinterface. - Override the methods that correspond to the events that you want to monitor on the radio buttons e.g
itemStateChanged. - Create a number of new
JRadioButtonscomponents. - Use the
addItemListenerto add the ItemListener to each one of the buttons.
Let’s see the code snippet that follows:
package com.javacodegeeks.snippets.desktop;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;
public class JradioButtonExample extends JFrame {
private JRadioButton s, m, l;
private JButton jbutton;
public JradioButtonExample(String str) {
super(str);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container cPane = this.getContentPane();
ButtonGroup buttonGroup = new ButtonGroup();
s = new JRadioButton("Small");
m = new JRadioButton("Medium");
l = new JRadioButton("Large");
buttonGroup.add(s);
buttonGroup.add(m);
buttonGroup.add(l);
jbutton = new JButton("Click me!");
jbutton.setBounds(100, 100, 100, 100);
JPanel jPaneCent = new JPanel();
jPaneCent.setLayout(null);
jPaneCent.add(jbutton);
cPane.add(jPaneCent, BorderLayout.CENTER);
JPanel jPanelNor = new JPanel();
jPanelNor.add(s);
jPanelNor.add(m);
jPanelNor.add(l);
cPane.add(jPanelNor, BorderLayout.NORTH);
ChangeSize chsize = new ChangeSize(jbutton);
s.addItemListener(chsize);
m.addItemListener(chsize);
l.addItemListener(chsize);
}
public static void main(String[] args) {
JFrame jFrame = new JradioButtonExample("JRadioButton example");
jFrame.setSize(300, 200);
jFrame.setVisible(true);
}
}
class ChangeSize implements ItemListener {
private Component component;
public ChangeSize(Component comp) {
component = comp;
}
@Override
public void itemStateChanged(ItemEvent event) {
String s = (String) event.getItem();
if (s.equals("Small")) {
component.setSize(60, 10);
} else if (s.equals("Medium")) {
component.setSize(90, 70);
} else if (s.equals("Large")) {
component.setSize(260,105);
}
}
}
This was an example on how to use JRadioButton with ItemListener in Java.

