GridLayout SpringLayout This section shows example GUIs that use these layout managers and tells you where to find the how-to section for each one. You can run each example using Java Web Start by following the instructions in the how-to sections. BorderLayout Every content pane is initialized to use BorderLayout. All extra space is placed in the center area see Figure 1. Figure 1.

Author:Kagar Meztira
Language:English (Spanish)
Published (Last):12 May 2012
PDF File Size:20.75 Mb
ePub File Size:5.22 Mb
Price:Free* [*Free Regsitration Required]

To create simple, standard dialogs, you use the JOptionPane class. The code for simple dialogs can be minimal. Figure 11, for example, shows an informational dialog. Figure A simple dialog. When the frame is destroyed , so are its dialogs.

When the frame is iconified , its dialogs disappear from the screen. When the frame is deiconified, its dialogs return to the screen.

The AWT automatically provides this behavior. A dialog can be modal. When a modal dialog is visible, it blocks user input to all other windows in the program. The dialogs that JOptionPane provides are modal. To create a nonmodal dialog, you must use the JDialog class directly. Dialog class. It adds to Dialog a root pane and support for a default close operation. The DialogDemo Example Figure 12 is a picture of an application that displays dialogs.

The DialogDemo application. Click the Show it! A modal dialog will appear. Until you close it, the application will be unresponsive , although it will repaint itself if necessary. Iconify the DialogDemo window while a dialog is showing. The dialog will disappear from the screen until you deiconify the DialogDemo window. In the More Dialogs pane, click the bottom radio button and then the Show it! A nonmodal dialog will appear. Note that the DialogDemo window remains fully functional while the nonmodal dialog is up.

Other features allow you to customize the components the dialog displays and specify where the dialog should appear onscreen. You can even specify that an option pane put itself into an internal frame JInternalFrame instead of a JDialog. Note: The internal frames that JOptionPane creates currently behave differently from modal dialogs.

When you create a JOptionPane , look-and-feel-specific code adds components to it and determines their layout. You can use a custom icon, no icon at all, or any one of four standard JOptionPane icons question, information, warning, and error.

Each look and feel has its own versions of the four standard icons. Figure 13 shows the icons used in the Java look and feel. Icons provided by JOptionPane Java look and feel shown. If your dialog should be an internal frame, add Internal after show for example, showMessageDialog changes to showInternalMessageDialog. Then invoke setVisible true on the JDialog to make it appear.

The showMessageDialog method displays a simple, one-button dialog. The show-OptionDialog method displays a customized dialog that can display a variety of buttons with customized button text and can contain a standard text message or a collection of components. The other two show Xxx Dialog methods are used less often. A fourth method, showInputDialog , is designed to display a modal dialog that gets a string from the user, using either a text field or an uneditable combo box.

The following are some examples, taken from DialogDemo. For more example code, see DialogDemo. You can easily specify the message, icon, and title the dialog displays. Table 12 shows some examples. Table With this method, you can change the text that appears on the buttons of standard dialogs and perform many other kinds of customization. See Table You then have to add the option pane to a JDialog , register a property-change listener on the option pane, and show the dialog.

See Stopping Automatic Dialog Closing page for details. The following list describes each argument. Component parentComponent Always the first argument to each show Xxx Dialog method, it must be a frame, a component inside a frame, or null.

If you specify a frame, the dialog will appear over its center and depend on it. Object message A required argument, this specifies what the dialog should display in its main area. Generally, you specify a string, which results in the dialog displaying a label with the specified text. Determines the icon displayed in the dialog. Icon icon The icon to display in the dialog. Object[] options Further specifies the option buttons to appear at the bottom of the dialog.

Generally, you specify an array of strings for the buttons. See Customizing Button Text page for more information. Object initialValue Specifies the default value to be selected. You can either let the default icon be used or specify the icon using the message type or icon argument. By default, a dialog created with showMessageDialog displays the information icon, and a dialog created with showConfirmDialog or showInputDialog displays the question icon.

An option pane created with a JOptionPane constructor displays no icon by default. To specify that the dialog display a standard icon or no icon, specify the message type. To specify a custom icon, use the icon argument. The icon argument takes precedence over the message type; as long as the icon argument has a non-null value, the dialog displays the specified icon. Customizing Button Text When you use JOptionPane to create a dialog, you can choose either to use the standard button text which might vary by look and feel or to specify different text.

The code shown in Table 15, taken from DialogDemo. The first is implemented with showConfirmDialog , which uses the look-and-feel wording for the two buttons. The second uses showOptionDialog so that it can customize the wording. With the exception of wording changes, the dialogs are identical. Even if you change the strings that the standard dialog buttons display, the return value is still one of the predefined integers.

For example, the dialog implemented in CustomDialog. DialogDemo contains two dialogs that implement a property-change listener.

One of these is a custom modal dialog, implemented in CustomDialog , that uses JOptionPane both to get the standard icon and to get layout assistance. Though this dialog is useless as written, its code is simple enough that you can use it as a template for more complex dialogs.

The arguments specify in order parent component, message, title, message type, and icon for the dialog. See Creating and Showing Simple Dialogs page for a discussion of the arguments and their effects. The arguments specify in order parent component, message, title, option type, message type, icon, options, and initial value for the dialog. See also Creating and Showing Simple Dialogs page The arguments specify in order parent component, message, title, option type, message type, and icon for the dialog.

The single-argument version specifies just the message, with the parent component assumed to be null. The arguments for the other versions specify in order parent component, message, title, message type, icon, options, and initial value for the dialog. Implement a standard dialog as an internal frame. See Table 17 page for the exact list of arguments. The Frame argument, if any, is the frame usually a JFrame object that the dialog depends on. Make the boolean argument true to specify a modal dialog, false or absent to specify a nonmodal dialog.

You can also specify the title of the dialog using a string argument. The constructors taking the java. GraphicsConfiguration argument were introduced in 1.

See Using Top-Level Containers page 46 for more information. See Responding to Window-Closing Events page for more information. Note that this is only a hint, as some look and feels do not support this feature. Introduced in 1.


JFC Swing Tutorial, The: A Guide to Constructing GUIs, 2nd Edition



The JFC Swing Tutorial: A Guide to Constructing GUIs / Edition 2



The JFC Swing Tutorial Second Edition


Related Articles