< Prev
Next >



JRadioButton class




JRadioButton class is used to create a radio button control, which contains a circle that can be selected or unselected by clicking on it. JRadioButton is a component which extends JComponent class and it can be added to the container like JFrame or a component like JPanel.




Constructors of JRadioButton


Constructor Description
public JRadioButton() Creates an unselected radio button.
public JRadioButton(String text) Creates an unselected radio button with specified text.
public JRadioButton(String text, boolean b) Creates a radio button with specified text, which is selected or unselected on the basis of boolean value.
public JRadioButton(String text, Icon image, boolean b) Creates a radio button with specified text and icon, which is selected or unselected depending on boolean value.





Methods of JRadioButton class


Methods Description
public void setName(String text) Sets a name on the JRadioButton, this name will not be displayed.
public String getName() Gets a String message of JRadioButton, this name will not be displayed.
public void setIcon(Icon icon) Sets an icon or image over the JRadioButton.
public Icon getIcon() Gets the icon or image of the JRadioButton.





An example variants of JRadioButton


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RadioBt1
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class A


class A //implements ActionListener
{
JFrame jf;

JRadioButton radio1, radio2, radio3, radio4;

A()
{
jf= new JFrame("Variants of JCheckbox");




radio1 = new JRadioButton();		//JRadioButton()
radio2 = new JRadioButton("Water",true); //JRadioButton(String, boolean)
radio3 = new JRadioButton("Tea");	//JRadioButton(String)
radio4 = new JRadioButton("Coffee", new ImageIcon("Coffee.png"), true);//JRadioButton(String, Icon, boolean)


jf.add(radio1);
jf.add(radio2);
jf.add(radio3);
jf.add(radio4);


jf.setLayout(new FlowLayout());
jf.setSize(400,200);
jf.setVisible(true);
}

}


When you run the code, you are presented a window shown below -:

Figure 1


Remember the last JCheckbox(ch4) will not be visible because its icon has covered it, but it is still a checkbox, just covered by its icon.




Handling JRadioButton events when a JRadioButton is checked or unchecked.


In the upcoming code, we are going to handle events when a JRadioButton is checked or unchecked. In the next code, we have also combined a JRadioButton with a JLabel to add an icon/image next to a radio button.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RadioBt2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class A

class A implements ActionListener
{
JFrame jf;
JRadioButton radio1, radio2;
JLabel label1;

A()
{
jf= new JFrame("JRadioButton");
radio1 = new JRadioButton("Summer");
radio2 = new JRadioButton("Winter");
label1 = new JLabel();

jf.add(radio1);
jf.add(radio2);

radio1.addActionListener(this);
radio2.addActionListener(this);

jf.setLayout(new FlowLayout());
jf.setSize(200,150);
jf.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
JRadioButton rb = (JRadioButton)ae.getSource();

if(rb.isSelected()==true)
{
label1.setText(ae.getActionCommand()+ " is checked");
jf.add(label1);
jf.setVisible(true);
}
else
{
label1.setText(ae.getActionCommand()+ " is unchecked");
jf.add(label1);
jf.setVisible(true);
}
}


}


When you run the code, you are presented a window shown in the Figure2 below -:

Figure 2


When you check a checkbox an ActionEvent is fired and you are presented a message to display which checkbox is last checked/unchecked. For example, when you check the checkbox with label- summer, you are notified like -

Figure 3


when you uncheck this checkbox, you are notified with a message that you've unchecked the checkbox(including its its name).

Figure 4





JRadioButton with images


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;t

public class RadioBt3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class A

class A implements ActionListener
{
JFrame jf;
JRadioButton radio1, radio2, radio3;
JLabel label1, label2, label3, label4;


A()
{
jf= new JFrame("JRadioButton with icons");

label1 = new JLabel(new ImageIcon("Grapes1.png"));
radio1 = new JRadioButton("");
radio1.setName("Grapes");

label2 = new JLabel(new ImageIcon("Mango.jpg"));
radio2 = new JRadioButton("");
radio2.setName("Mango");

label3 = new JLabel(new ImageIcon("Apple.jpg"));
radio3 = new JRadioButton("");
radio3.setName("Apple");

label4= new JLabel();

jf.add(radio1);
jf.add(label1);
jf.add(radio2);
jf.add(label2);
jf.add(radio3);
jf.add(label3);

radio1.addActionListener(this);
radio2.addActionListener(this);
radio3.addActionListener(this);

jf.setLayout(new FlowLayout());
jf.setSize(400,200);
jf.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
JRadioButton rd = (JRadioButton)ae.getSource();

if(rd.isSelected()==true)
{
label4.setText(rd.getName()+ " is checked");
jf.add(label4);
jf.setVisible(true);
}
else
{
label4.setText(rd.getName()+ " is unchecked");
jf.add(label4);
jf.setVisible(true);
}
}

}
When you run the code, you are presented a window shown in the Figure5 below -:

Figure 5


When you check a JRadioButton an ActionEvent is fired and you are presented a message to display which image of fruit is last checked/unchecked. For example, when you check the JRadioButton with the image of an Apple, you are notified like -

Figure6


when you uncheck this checkbox, you are notified with a message that you've unchecked the Apple's JRadioButton.

Figure 7




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< JComboBox
JTable >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2