JRadioButton Component in Java

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:
  1. import java.awt.*; // used to access the FlowLayout class
  2. 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:
  1.     JFrame frame = new JFrame("JRadioButton Component");
  2.    
  3.     JRadioButton button1 = new JRadioButton("Pig");
  4.     JRadioButton button2 = new JRadioButton("Dog");
  5.     JRadioButton button3 = new JRadioButton("Cat");
  6.     JRadioButton button4 = new JRadioButton("Rat");
  7.    
  8.     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.
  1.     btnGroup.add(button1);
  2.     btnGroup.add(button2);
  3.     btnGroup.add(button3);
  4.     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.
  1.  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:
  1.     frame.getContentPane().add(new JLabel("Animals:"));
  2.     frame.getContentPane().add(button1);
  3.     frame.getContentPane().add(button2);
  4.     frame.getContentPane().add(button3);
  5.     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.
  1.     frame.getContentPane().setLayout(new FlowLayout());
  2.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3.     frame.pack();
  4.     frame.setVisible(true);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; // used to access the FlowLayout class
  2. import javax.swing.*; // used to access JFrame, JLabel, JRadioButton, and ButtonGroup class
  3.  
  4.  
  5. public class jRadioButtonComponent {
  6.        
  7.   public static void main(String[] args) {
  8.     JFrame frame = new JFrame("JRadioButton Component");
  9.    
  10.    
  11.    
  12.     JRadioButton button1 = new JRadioButton("Pig");
  13.     JRadioButton button2 = new JRadioButton("Dog");
  14.     JRadioButton button3 = new JRadioButton("Cat");
  15.     JRadioButton button4 = new JRadioButton("Rat");
  16.    
  17.     ButtonGroup btnGroup = new ButtonGroup();
  18.    
  19.    
  20.     btnGroup.add(button1);
  21.     btnGroup.add(button2);
  22.     btnGroup.add(button3);
  23.     btnGroup.add(button4);
  24.    
  25.    
  26.     button1.setSelected(true);
  27.    
  28.     frame.getContentPane().add(new JLabel("Animals:"));
  29.     frame.getContentPane().add(button1);
  30.     frame.getContentPane().add(button2);
  31.     frame.getContentPane().add(button3);
  32.     frame.getContentPane().add(button4);
  33.    
  34.     frame.getContentPane().setLayout(new FlowLayout());
  35.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.     frame.pack();
  37.     frame.setVisible(true);
  38.   }
  39. }
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

Add new comment