Java and Radio Buttons Using Netbeans 5.6 GUI Builder

To get through this tutorial, you must already be able to create a basic form with 3 or 4 radio buttons on it.  We are going to go over making and using these buttons in a functional example.  To start, I create a project selecting Java Application and then the Next button.  I rename my application to “RadioButtons”, you may name it whatever you want to, I then deselect (uncheck) the “Create Main Class” option, followed by Finish.

  1. Right Click on Source Packages and New -> Java Package, I name this radiobuttons.
    1-29-2009 5-16-59 PM
  2. Right Click radiobuttons (if you have named it the same as I have) -> New -> JFrame Form, which I name RadioButtons, and click finish.
    1-29-2009 5-21-34 PM
  3. From the Right hand side (maybe different for you), drag Button Group anywhere in your square panel and drop it (you won’t see any changes here).
    1-29-2009 5-23-53 PM
  4. Now place your radio buttons, using the Radio Button (next to your Button Group).  I place four for this example.  I then shift-click (to select all of them).
    1-29-2009 5-35-02 PM
  5. Right click your selection -> Select Properties.  This step is where you group them together.  When you look at the properties, there is an option for button Grouping (seen below).  This will group them for our purposes here, select your buttonGroup (that was created in step 3).  Note: If you don’t see a selection, you can go back to 3 now and carry out that step
    1-29-2009 5-37-13 PM
  6. At this stage, if you were to click “Play” or Run -> Main Project, you will see the radio buttons in a GUI that are grouped (allowing only 1 of the 4 to be selected at a time).
  7. Now, to put these radio buttons to use, we will add a button, with a click listener and a text area.  (Just drag and drop from the right hand group of objects).
    1-29-2009 5-47-22 PM
  8. To add the click listener to the button, Right Click the button, select Properties.  At the top of the pop up dialogue, select events -> scroll down to mouseClicked and select the … on the right hand side.  This will take you to the picture below, where I named my event and selected Ok to close out dialogues.
    1-29-2009 5-53-13 PM
  9. This will take you into the code where you can create your code to do what you want it to do.  In this example, I am simply setting the text box to the value of the radio button that is selected.  And here is the corresponding code, below:
    1-29-2009 6-47-31 PM
  10. Press Play now.  :-)

There are a few things to note.  If you are using their text in a calculation, you will need to cast it properly.  There is most likely a better way to do this, but for beginner I believe this will get you started.  Feel free to comment, as always.

About the Author: JeffAncel
http://jeffancel.com
A passionate web developer in St. Louis. Living on the edge with Rails, HTML5, CSS3 and any interesting libraries around. I love to spend time with the family, cook, develop web applications and play with our dragons. When I have nothing else to do, I socialize via twitter (@jancel) and facebook.

32 Responses to Java and Radio Buttons Using Netbeans 5.6 GUI Builder

Leave a reply

© 2012 - Jeff Ancel
Wordpress Themes
Scroll to Top