Example Six: VoteDialog

 <  Day Day Up  >  

graphics/cd_icon.gif

The last example in this chapter is VoteDialog . [11] It illustrates the use of dialogs and radio buttons , as shown in Figure 13.

[11] To run VoteDialog using Java Web Start, click the VoteDialog link on the RunExamples/learn.html page on the CD. You can find the source files here: JavaTutorial/uiswing/learn/example-1dot4/index.html#VoteDialog .

Figure 13. The VoteDialog application.

graphics/02fig13.gif

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.

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 previous examples, our top-level container was always 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, Figure 14 shows an informational dialog. Here's the code that creates and shows it:

 JOptionPane.showMessageDialog(frame, "There's no \"there\" there."); 
Figure 14. A simple dialog.

graphics/02fig14.gif

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 the section How to Make Dialogs (page 187) in Chapter 7.

 <  Day Day Up  >  


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net