Java JRadioButton

Java JRadioButton

Java JRadioButton Example
JRadioButton is used to create radio button.JRadioButtons are used to select one option from several options unlike JCheckBox that you can select multiple options.
A group of radio buttons should be added to one ButtonGroup in order to select one.

How to Declare JRadioButton class

This is how you declare javax.swing.JRadioButton.
[public class JRadioButton extends JToggleButton implements Accessible]

Constructors used in JRadioButton.

  • JRadioButton()-creates radio button that is unselected.
  • JRadioButton(String s)-creates radio button with a certain text.
  • JRadioButton(String s,boolean selected)-creates radio button that is selected and with a certain text.

JRadioButtons commonly used methods.

  • void setText(String s)-used to specify radio button text.
  • String getText()-returns radio button text.
  • void setEnabled(boolean value)-used to enable or disable the button.
  • void  setIcon(Icon icon)-used to add icon to the radio button.
  • Icon getIcon()-returns icon used in the radio button.
  • void setMnemonic(int a)-use to set radio button mnemonic.
  • void addActionListener(ActionListener l)-used to add actionlistener. 

Java JRadioButton Example

[
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
public class RadioButtonExample extends JFrame {

    RadioButtonExample()
    {
        super("JRadioButton Example");

        JRadioButton button1=new JRadioButton("A) Male");
        JRadioButton button2=new JRadioButton("B) Female");
        button1.setBounds(75,50,100,30);
        button2.setBounds(75,100,100,30);
        ButtonGroup bg=new ButtonGroup();
        bg.add(button1);bg.add(button2);
        add(button1);
        add(button2);

        setLocationRelativeTo(null);
        setSize(400,400);
        setLayout(null);
        setVisible(true);
    }
    public static void main(String args[])
    {
        new RadioButtonExample();
    }

}
]

Output:

Java JRadioButton


Java JRadioButton Example with ActionListener

[
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class RadioButtonExample extends JFrame implements ActionListener{
    public JRadioButton button1,button2;
    public JButton save;

    RadioButtonExample()
    {
        super("JRadioButton Example");
        button1=new JRadioButton("A) Male");
        button2=new JRadioButton("B) Female");
        button1.setBounds(75,50,100,30);
        button2.setBounds(75,100,100,30);
        ButtonGroup bg=new ButtonGroup();
        bg.add(button1);bg.add(button2);
        add(button1);
        add(button2);

        save=new JButton("Save");
        save.setBounds(75,150,100,30);
        add(save);
        save.addActionListener(this);

        setLocationRelativeTo(null);
        setSize(400,400);
        setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent event)
    {
        if(button1.isSelected())
        {
            JOptionPane.showMessageDialog(this, "You Gender is Male",
                    "JRadioButton Example",JOptionPane.INFORMATION_MESSAGE);
        }
        if(button2.isSelected())
        {
            JOptionPane.showMessageDialog(this, "You Gender is Female",
                    "JRadioButton Example",JOptionPane.INFORMATION_MESSAGE);
        }
    }
    public static void main(String args[])
    {
        new RadioButtonExample();
    }
}
]

Output




Code Explanation:

Above program introduces several java.swing class components These are :-
The program extends class JFrame and implements ActionListener interface.
Note:Once you extends a class,you can be able to access all the class methods.
JFrame methods include setSize,setLayout,setDefaultCloseOperation etc.
If a class is not abstract and it implements a certain interface,you are required to override all interface abstract methods.
Code [button1.isSelected()] checks if the radio button is selected.

Next Topic:Java JComboBox

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java JRadioButton
Java JRadioButton
Java JRadioButton
https://1.bp.blogspot.com/-QU_htWoBqQk/WL_zsOKSkMI/AAAAAAAAHFU/cur_d1AukroBEytgOeipKOQ5l9wpBSESACLcB/s200/java-JRadioButton-example-min.PNG
https://1.bp.blogspot.com/-QU_htWoBqQk/WL_zsOKSkMI/AAAAAAAAHFU/cur_d1AukroBEytgOeipKOQ5l9wpBSESACLcB/s72-c/java-JRadioButton-example-min.PNG
Java Codes
http://www.download-all.net/2017/03/java-jradiobutton.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/java-jradiobutton.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy