Download the JDK
Search the Tutorials
Hide the TOC
Trail: Creating a GUI with JFC/Swing
Lesson: Learning Swing by Example
Learning Swing by Example
Example One: Your First Swing Program
Example Two: `SwingApplication`
Example Three: `CelsiusConverter`
Example Four: An Improved `CelsiusConverter`
Example Five: `LunarPhases`
Example Six: `VoteDialog`
Summary
Home Page > Creating a GUI with JFC/Swing > Learning Swing by Example
« Previous • Trail • Next »
Example Six: `VoteDialog`

> Topics covered in this example include:
> - Radio Buttons
> - Dialogs
> The last example in this lesson is `VoteDialog`. It illustrates the use of dialogs and radio buttons.

> In `VoteDialog`, the user casts a vote by selecting a radio button and clicking the Vote button. After the button is clicked, a dialog appears with an informational message or a follow-up question.

> Here's a picture of the `VoteDialog` application:

### Radio Buttons

> The `VoteDialog` application has one action listener that listens to clicks on the top-level container’s button. Each time the action listener receives an event, the application determines which radio button was selected and displays the appropriate dialog.

> For each group of radio buttons, you need to create a `ButtonGroup` instance and add each radio button to it. `ButtonGroup` takes care of unselecting the previously selected button when the user selects another one in the group. You should generally initialize a group of radio buttons so that one is selected. However, the API doesn’t enforce this rule: A group of radio buttons can have no initial selection. Once the user has made a selection, exactly one button is selected from then on.

> Here’s the code from `VoteDialog.java` that creates the radio buttons and the `ButtonGroup` instance that controls them. The `setActionCommand` method associates a specific dialog with each radio button item. We use the `setSelected` method to specify the default selected radio button.

> >     final int numButtons = 4;
> >     JRadioButton[] radioButtons = new JRadioButton[numButtons];

> >     final ButtonGroup group = new ButtonGroup();
> >     ...

> >     final String defaultMessageCommand = "default";
> >     final String yesNoCommand = "yesno";
> >     final String yeahNahCommand = "yeahnah";
> >     final String yncCommand = "ync";

> >     radioButtons[0] = new JRadioButton("<html>Candidate 1:
> >           <font color=red>Sparky the Dog</font></html>");
> >     radioButtons[0].setActionCommand(defaultMessageCommand);

> >     radioButtons[1] = new JRadioButton("<html>Candidate 2:
> >           <font color=green>Shady Sadie</font></html>");
> >     radioButtons[1].setActionCommand(yesNoCommand);

> >     radioButtons[2] = new JRadioButton("<html>Candidate 3:
> >           <font color=blue>R.I.P. McDaniels</font></html>");
> >     radioButtons[2].setActionCommand(yeahNahCommand);

> >     radioButtons[3] = new JRadioButton("<html>Candidate 4:
> >           <font color=maroon>Duke the Java<font size=-2><sup>TM</sup>
> >           </font size> Platform Mascot</font></html>");
> >     radioButtons[3].setActionCommand(yncCommand);

> >     for (int i = 0; i < numButtons; i++) {
> >         group.add(radioButtons[i]);
> >     }

> >     //Select the first button by default.
> >     radioButtons[0].setSelected(true);

> Note the use of HTML code on the radio buttons, which lets us specify multiple text colors within each button.

### Dialogs

> In our previous examples, our top-level container has always been a `JFrame`. Another kind of top-level container is a dialog--a window that is more limited than a frame. To create simple, standard dialogs, you use the `JOptionPane` class. The dialogs that `JOptionPane` provides are modal. When a modal dialog is visible, it blocks user input to all other windows in the program.

> The code for simple dialogs can be minimal. For example, the following figure shows an informational dialog. Here’s the code that creates and shows it:

> >     JOptionPane.showMessageDialog(frame, "There's no \"there\" there.");

> Every dialog is dependent on a frame. When that frame is destroyed, so are its dependent dialogs. When the frame is iconified, its dependent dialogs disappear from the screen. When the frame is deiconified, its dependent dialogs return to the screen. The AWT automatically provides this behavior. You can get more information on dialogs in How to Make Dialogs.

« Previous • Trail • Next »
Previous page: Example Five: `LunarPhases`
Next page: Summary