Home » Desktop Java » awt » event » JRadioButton with ItemListener example

About Ilias Tsagklis

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

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

  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.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

 

Leave a Reply

avatar
  Subscribe  
Notify of