JRadioButton Component in Java
Submitted by donbermoy on Wednesday, November 19, 2014 - 22:00.
This is a tutorial in which we will going to create a program that has the JRadioButton Component using Java. The JRadioButton as in implementation of a Radio Button is used to let the user select or deselect an item and displays its state to the user. There can only be one item to choose from different options in a radio button.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of jRadioButtonComponent.java.
2. Import the following packages:
3. Initialize your variable in your Main, variable frame for JFrame class, variable button1,button2, buttob3, and button4 for JRadioButton, and variable btnGroup for ButtonGroup. Have this code below:
As you have seen the code above, we have declared the four variables for JRadioButton namely button1 labeled as "Pig", button2 labeled as "Dog", button3 labeled as "Cat", button4 labeled as "Rat". The ButtonGroup class is used to create a group of buttons in which only one button at a time can be selected. This is to merge all buttons into one as a group.
4. To merge the radio buttons to only one group that the buttons can only be selected at one time, we will use the add method of the ButtonGroup class.
5. Now, to select the item on the radio buttons after running it, use the setSelected method and return it to true.
6. Now, add the radio buttons to the frame using the getContentPane.add method and the label that will be instantiated in the frame. Have this code below:
7. Pack the frame so that the component/control will be seen directly to the frame and have it with the FlowLayout as the layout used. Also set its visibility to true and have it the close operation to exit.
Output:
Here's the full code of this tutorial:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
- import java.awt.*; // used to access the FlowLayout class
- import javax.swing.*; // used to access JFrame, JLabel, JRadioButton, and ButtonGroup class
- btnGroup.add(button1);
- btnGroup.add(button2);
- btnGroup.add(button3);
- btnGroup.add(button4);
- button1.setSelected(true);
- frame.getContentPane().add(button1);
- frame.getContentPane().add(button2);
- frame.getContentPane().add(button3);
- frame.getContentPane().add(button4);
- frame.pack();
- frame.setVisible(true);

- import java.awt.*; // used to access the FlowLayout class
- import javax.swing.*; // used to access JFrame, JLabel, JRadioButton, and ButtonGroup class
- public class jRadioButtonComponent {
- btnGroup.add(button1);
- btnGroup.add(button2);
- btnGroup.add(button3);
- btnGroup.add(button4);
- button1.setSelected(true);
- frame.getContentPane().add(button1);
- frame.getContentPane().add(button2);
- frame.getContentPane().add(button3);
- frame.getContentPane().add(button4);
- frame.pack();
- frame.setVisible(true);
- }
- }
Add new comment
- 37 views