• We just launched and are currently in beta. Join us as we build and grow the community.

JRadioButton Component in Java

plot1

Anime Subreddit Admin
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
126
Likes
180
Bits
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:
  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. JRadioButton

    button1 =

    new

    JRadioButton

    (

    "Pig"

    )

    ;
  3. JRadioButton

    button2 =

    new

    JRadioButton

    (

    "Dog"

    )

    ;
  4. JRadioButton

    button3 =

    new

    JRadioButton

    (

    "Cat"

    )

    ;
  5. JRadioButton

    button4 =

    new

    JRadioButton

    (

    "Rat"

    )

    ;

  6. 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:

jradio.png


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. public

    class

    jRadioButtonComponent {

  4. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  5. JFrame

    frame =

    new

    JFrame

    (

    "JRadioButton Component"

    )

    ;



  6. JRadioButton

    button1 =

    new

    JRadioButton

    (

    "Pig"

    )

    ;
  7. JRadioButton

    button2 =

    new

    JRadioButton

    (

    "Dog"

    )

    ;
  8. JRadioButton

    button3 =

    new

    JRadioButton

    (

    "Cat"

    )

    ;
  9. JRadioButton

    button4 =

    new

    JRadioButton

    (

    "Rat"

    )

    ;

  10. ButtonGroup

    btnGroup =

    new

    ButtonGroup

    (

    )

    ;


  11. btnGroup.add

    (

    button1)

    ;
  12. btnGroup.add

    (

    button2)

    ;
  13. btnGroup.add

    (

    button3)

    ;
  14. btnGroup.add

    (

    button4)

    ;


  15. button1.setSelected

    (

    true

    )

    ;

  16. frame.getContentPane

    (

    )

    .add

    (

    new

    JLabel

    (

    "Animals:"

    )

    )

    ;
  17. frame.getContentPane

    (

    )

    .add

    (

    button1)

    ;
  18. frame.getContentPane

    (

    )

    .add

    (

    button2)

    ;
  19. frame.getContentPane

    (

    )

    .add

    (

    button3)

    ;
  20. frame.getContentPane

    (

    )

    .add

    (

    button4)

    ;

  21. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    FlowLayout

    (

    )

    )

    ;
  22. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  23. frame.pack

    (

    )

    ;
  24. frame.setVisible

    (

    true

    )

    ;
  25. }
  26. }

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.
 

452,292

323,348

323,357

Top