JRadioButton

Creating JRadioButton

The first line create one radio button
JRadioButton r1 = new JRadioButton("My Text here"); 
r1.setBounds(270, 183, 83, 38); // Position of radio button
f1.getContentPane().add(r1); // Radio button added to JFrame f1 

Grouping radio buttons

There are two radio buttons r1 and r2.
ButtonGroup my_group = new ButtonGroup(); // New group created
my_group.add(r1);
my_group.add(r2);
You must import ButtonGroup, check that your IDE added this line at top. (error message Button cannot be resolved to a type )
import javax.swing.ButtonGroup;

Action Listener or Event with Radio Buttons

r1.addActionListener(new ActionListener() {  
	  public void actionPerformed(ActionEvent e) 
        {
           	// Action to be performed here
        }
});

ChangeListener

r1.addChangeListener(new ChangeListener() {
		public void stateChanged(ChangeEvent e) {
		}
	});

Reading the status

Our radio buttons can have only one of the two states, either Selected or Not Selected. We can get the status of the button by using isSelected(). It returns Boolean value , true if selected and false if not selected.

r1 is our JRadioButton.
r1.isSelected(); // true if selected, false otherwise

Reading and Updating Label of Radio button

We can read the text or Label associated with a radio button. Reading the text of JRadioButton r1.
String str=r1.getText();
Setting the text of r1.
r1.setText("I am selected");

Updating the status of radiobutton

We can change the staus of an JRadioButton to selected or not selected by using setSelected()
r1.setSelected(true); // r1 will be selected


Button with Textfield and Lable

swing


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer