JRadioButton

Get selected JRadioButton from ButtonGroup

With this example we shall show you how to get the selected JRadioButton for a ButtonGroup in a Java Desktop Application. Radio buttons, in general, implement a very user friendly way ton provide input to your application, especially when the user has some specific elements to choose from.

In order to get the selected JRadioButton from a ButtonGroup, one should follow these steps:

  • Create a class that extends JFrame and implements ActionListener.
  • Create some JRadioButtons using JRadioButton constructor, with the button’s title as an argument.
  • Use setActionCommand to set a command for that button and setSelected to set the by default selected button.
  • Create a new ButtonGroup and use the add method to add the JRadioButtons to it.
  • Create a new Button and bundle it with the ActionListener
  • Override the actionPerformed of the ActionListener interface.
  • Use ButtonGroup.getSelection().getActionCommand() to get the user’s selection.

Let’s take a look a the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class GetSelectedJRadioButtonFromButtonGroup extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JRadioButton java;
	private JRadioButton c;
	private JRadioButton net;
	private JButton button;
	private ButtonGroup buttonGroup;

	public GetSelectedJRadioButtonFromButtonGroup() {

		// set flow layout for the frame
		this.getContentPane().setLayout(new FlowLayout());

		java = new JRadioButton("Java");
		java.setActionCommand("Java");

		c = new JRadioButton("C/C++");
		c.setActionCommand("c");

		net = new JRadioButton(".NET");
		net.setActionCommand("net");

		java.setSelected(true);

		button = new JButton("Check");

		button.addActionListener(this);

		buttonGroup = new ButtonGroup();

		//add radio buttons
		buttonGroup.add(java);
		buttonGroup.add(c);
		buttonGroup.add(net);

		add(java);
		add(c);
		add(net);
		add(button);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Check")) {
			System.out.println("Selected Radio Button: " + buttonGroup.getSelection().getActionCommand());
		}
	}

	private static void createAndShowGUI() {

  //Create and set up the window.

  JFrame frame = new GetSelectedJRadioButtonFromButtonGroup();

  //Display the window.

  frame.pack();

  frame.setVisible(true);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

	public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

    createAndShowGUI(); 

}

  });
    }

}

 
This was an example on how to get the selected JRadioButton from ButtonGroup.

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.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button