Pages

Get JRadioButton text

Source code below will show you how to get text from JRadioButton.

***********************************************************************
COMPLETE SOURCE CODE FOR : GetJRadioButtonText.java
***********************************************************************


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

import java.awt.BorderLayout;

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

public class GetJRadioButtonText implements ActionListener
{
//Create button with text ( Click me to get JRadioButton text )
JButton button=new JButton("Click me to get JRadioButton text");

//Create JRadioButton with text ( I am a radio button )
JRadioButton radioButton=new JRadioButton("I am a radio button");

//Create a window with title ( Get JRadioButton text )
JFrame frame=new JFrame("Get JRadioButton text");

public GetJRadioButtonText()
{
//Add action listener to button
button.addActionListener(this);

//Set JFrame layout to BorderLayout
frame.setLayout(new BorderLayout());

//Add created radio button into JFrame
frame.add(radioButton,BorderLayout.CENTER);

//Add created button into JFrame
frame.add(button,BorderLayout.SOUTH);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,500);

//Make JFrame visible
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
//Method getText will get text from radio button
JOptionPane.showMessageDialog(frame,"JRadioButton text is : "+radioButton.getText());
}
}

public static void main(String[]args)
{
GetJRadioButtonText gjbt=new GetJRadioButtonText();
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************
Related Posts Plugin for WordPress, Blogger...