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 ItemListener interface.
  • 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 JRadioButtons components.
  • Use the addItemListener to 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) {



  Container cPane = this.getContentPane();

  ButtonGroup buttonGroup = new ButtonGroup();

  s = new JRadioButton("Small");

  m = new JRadioButton("Medium");

  l = new JRadioButton("Large");




  jbutton = new JButton("Click me!");

  jbutton.setBounds(100, 100, 100, 100);

  JPanel jPaneCent = new JPanel();



  cPane.add(jPaneCent, BorderLayout.CENTER);

  JPanel jPanelNor = new JPanel();




  cPane.add(jPanelNor, BorderLayout.NORTH);

  ChangeSize chsize = new ChangeSize(jbutton);




    public static void main(String[] args) {

  JFrame jFrame = new JradioButtonExample("JRadioButton example");

  jFrame.setSize(300, 200);

class ChangeSize implements ItemListener {

    private Component component;

    public ChangeSize(Component comp) {

  component = comp;

    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")) {



This was an example on how to use JRadioButton with ItemListener in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

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

Inline Feedbacks
View all comments
Back to top button