plot1
Anime Subreddit Admin
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
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
Download
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:
- import
java.awt.*
;
// used to access the FlowLayout class
- import
javax.swing.*
;
// used to access JFrame, JLabel, JRadioButton, and ButtonGroup class
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:
- JFrame
frame =
new
JFrame
(
"JRadioButton Component"
)
;
- JRadioButton
button1 =
new
JRadioButton
(
"Pig"
)
;
- JRadioButton
button2 =
new
JRadioButton
(
"Dog"
)
;
- JRadioButton
button3 =
new
JRadioButton
(
"Cat"
)
;
- JRadioButton
button4 =
new
JRadioButton
(
"Rat"
)
;
- ButtonGroup
btnGroup =
new
ButtonGroup
(
)
;
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.
- btnGroup.add
(
button1)
;
- btnGroup.add
(
button2)
;
- btnGroup.add
(
button3)
;
- btnGroup.add
(
button4)
;
5. Now, to select the item on the radio buttons after running it, use the setSelected method and return it to true.
- button1.setSelected
(
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:
- frame.getContentPane
(
)
.add
(
new
JLabel
(
"Animals:"
)
)
;
- frame.getContentPane
(
)
.add
(
button1)
;
- frame.getContentPane
(
)
.add
(
button2)
;
- frame.getContentPane
(
)
.add
(
button3)
;
- frame.getContentPane
(
)
.add
(
button4)
;
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.
- frame.getContentPane
(
)
.setLayout
(
new
FlowLayout
(
)
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
- frame.pack
(
)
;
- frame.setVisible
(
true
)
;
Output:

Here's the full code of this tutorial:
- import
java.awt.*
;
// used to access the FlowLayout class
- import
javax.swing.*
;
// used to access JFrame, JLabel, JRadioButton, and ButtonGroup class
- public
class
jRadioButtonComponent {
- public
static
void
main(
String
[
]
args)
{
- JFrame
frame =
new
JFrame
(
"JRadioButton Component"
)
;
- JRadioButton
button1 =
new
JRadioButton
(
"Pig"
)
;
- JRadioButton
button2 =
new
JRadioButton
(
"Dog"
)
;
- JRadioButton
button3 =
new
JRadioButton
(
"Cat"
)
;
- JRadioButton
button4 =
new
JRadioButton
(
"Rat"
)
;
- ButtonGroup
btnGroup =
new
ButtonGroup
(
)
;
- btnGroup.add
(
button1)
;
- btnGroup.add
(
button2)
;
- btnGroup.add
(
button3)
;
- btnGroup.add
(
button4)
;
- button1.setSelected
(
true
)
;
- frame.getContentPane
(
)
.add
(
new
JLabel
(
"Animals:"
)
)
;
- frame.getContentPane
(
)
.add
(
button1)
;
- frame.getContentPane
(
)
.add
(
button2)
;
- frame.getContentPane
(
)
.add
(
button3)
;
- frame.getContentPane
(
)
.add
(
button4)
;
- frame.getContentPane
(
)
.setLayout
(
new
FlowLayout
(
)
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
- frame.pack
(
)
;
- frame.setVisible
(
true
)
;
- }
- }
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
Download
You must upgrade your account or reply in the thread to view hidden text.