Updated date:

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 41

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 40 | Lesson 42 >>

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-41

Lesson 41: Working with GUI controls: Radio buttons

In the previous lesson, we learnt how to work with Check box and Text area as Java Graphical User Interface (GUI) controls and we saw how we can select items using Check box and drop them into a Text area using some codes in a Command button. In this lesson, we shall continue with GUI controls in Java and discuss Radio buttons as GUI controls.

Radio buttons controls are used to select only one item from a list of many items. Unlike Check box, users cannot select multiple items through Radio buttons. This option of allowing users to select only one item from a list of items or options is very useful when the situation allow only one item to be selected, for instance, in the case of selecting Gender option.

For this lesson, you will need to create a new project and a new form as the one we have been using in previous lessons look filled already. Click on the File >> New Project. You can call the project a name of your choice but i have called it Radio Button here. Once you have created the project, go ahead and create a new Jframe form.

How to create a Jframe form

How to create a Jframe form

We’ll first start by labeling the form with a caption “Working with Radio Buttons” so, drag a label from the Swing Controls on the Palette window and drop it on the form. Apply the necessary font and foreground properties. I have made this to have font Calibri, size 24 and bold with a blue foreground color.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-41

Radio buttons are better manipulated when they are on a panel, that way you can work with them as a group and it is easy to move them around on the form. From the Swing Containers on the Palette window, drag and drop a panel to the form and re-size appropriately.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-41
Palette window

Palette window

From the Palette window under the Swing containers, locate for the Radio button control and drag it onto the panel. We want the user to select only one method of payment from the available options. Drop five radio button controls, changing caption text in each case. The text can be edited like we did with the Check boxes in the previous lesson. Let the variable name remain as the default name i.e. jRadioButton1

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-41

The final five radio buttons with all modes of payment labeled as radio buttons’ caption text should look as shown below when you run the form. Notice that we have applied some properties on font for radio button captions and also we have added a label at the top of radio buttons to let the user know what to do with the radio buttons.You can make these changes too.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-41

One thing that you should have noticed when you run the form is that the radio buttons are not working as expected. We want the user to select only one method of payment but as of now we can select more than one option. See below:

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-41

To solve this and make the radio buttons to have a single selection only, we need to put the radio buttons in a Button Group. Button Group allows you to group radio buttons under one name where only one radio button can be selected at any given time.

To use Button Group, we need to import the Button Group class from the Swing library. Click on the source code section of the form and add the following import code just before the class code.

import javax.swing.ButtonGroup;

And then add the following method just after the class Constructor

private void groupButton( ) {

ButtonGroup bg1 = new ButtonGroup( );

bg1.add(jRadioButton1);
bg1.add(jRadioButton2);
bg1.add(jRadioButton3);

}

The groupButton() method above adds radio buttons to the ButtonGroup object through use of the add method. What we need next is to call the Button Group from the class Constructor because that way the radio buttons will be grouped when the form loads. The code segment now should look like shown below:

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-41

Discover more on Java...

Run the form again and you should be able to select only one radio button now, selecting multiple radio buttons will not be possible. To get which radio button was selected, we are going to add a Command button on the form with the caption “Complete Payment Now.”

The command button will contain some codes with IF statements that will return the caption of the selected radio button through a JOptionPane message box. To get which radio button was selected, we’ll use the isSelected() method of the radio buttons.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-41

The code in the command button should look as shown below, notice that we have added JOptionPane message box code at the end if the IF statements to output the text from the radio button that was selected.

Code in the command button ActionPerformed event

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String textradio = "";
    if(jRadioButton1.isSelected()){
        textradio = jRadioButton1.getText();
    }
    if(jRadioButton2.isSelected()){
        textradio = jRadioButton2.getText();
    }
    if(jRadioButton3.isSelected()){
        textradio = jRadioButton3.getText();
    }
    if(jRadioButton4.isSelected()){
        textradio = jRadioButton4.getText();
    }
    if(jRadioButton5.isSelected()){
        textradio = jRadioButton5.getText();
    }
   javax.swing.JOptionPane.showMessageDialog(this,"Pay with "+textradio+" ?");
}

Run the form now and you should be able to have a JOptionPane message box displaying the caption text of the selected radio button.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-41

We are using the “this” keyword in the JOptionPane message box code to denote the object of the current form rather than using null which means no window. Radio buttons are some of the commonly used GUI controls in GUI programming. In the next lesson, we shall discuss Java Menus. See you then.

<< Lesson 40 | Lesson 42 >>

Want to make money online?

Comments

Danson Wachira (author) from Nairobi, Kenya on May 13, 2013:

Hi annitakison,

I am glad that this tutorial was helpful, that motivates me to go on and on. Thanks for the visit and comment.

Anni Takison from california, USA on May 13, 2013:

Wow.. You really provides us great and informative notes in java. My queries regarding radio button are now solved by the help your hub. Thanks. :)

Danson Wachira (author) from Nairobi, Kenya on April 22, 2013:

Hi Virma,

If i offer something useful to beginners, then my mission is complete. Thanks for the visit and comment.

Virma on April 22, 2013:

Awesome tutorial on Java programming, you have crafted this nicely and i know all Java beginners will love this, in fact i guess they are coming here in drones. Well done!!!

Danson Wachira (author) from Nairobi, Kenya on January 29, 2013:

Hi kashmir56,

Due to some commitments, i have not been updating this series but luckily today i had some strength to do it and hope i will pick myself again. Thanks for the visit and comment.

Thomas Silvia from Massachusetts on January 29, 2013:

Another great hub in the series of programming in Java . Well done !

Vote up and more !!!

Danson Wachira (author) from Nairobi, Kenya on January 29, 2013:

Hi kidscrafts,

I do understand your take here, coding can take a lot of ones time and sometimes a lot of "pain" locating those bugs, luckily you have a loving friend to take care of that. Thanks for the visit and comment.

kidscrafts from Ottawa, Canada on January 29, 2013:

I used to be able to program those things when I took my multimedia course. Now, I am too busy working on the creative side of my website (add content) so my husband takes care of those aspects :-)))) Anyway...I am still working in multimedia because for my website, I use Photoshop, Flash (for animations), Cool Edit (for the sound), Avid Studio (for the video), Publisher (to create my documents) and occasionally Dreamweaver when I update the website of some of my clients. I think I have my hands full :-) No need to do radio buttons :-) But I am sure that with what you presented here so clearly, you will help a lot of people!

Voted up and useful!

Danson Wachira (author) from Nairobi, Kenya on January 29, 2013:

Hi Emman,

Thanks for the visit and comment, keep reading as i will post more.

Emman on January 29, 2013:

I don't know how you do it, but this is a useful tutorial worthy mentioning. May you continue to post more. Thanks.

Related Articles