Advertisement
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 | 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. |
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);
}
}
Advertisement
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);
}
}
}
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 -:
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement