Swing Containers : Dialogues

Apart from frames, there is one other specialized window subclass, namely, Dialog. Dialogues typically provide messages within their own windows. The Swing Dialogue class,



javax.swing.JDialog is a subclass of java.awt.Dialog, which in turn, is a subclass of java.awt.Windows. This class is used to create a custom dialogue window. With Swing, a number of standard dialogue windows are available and easy to use. These standard dialogues are available through the javax.swing.JOptionPane class. Figure shows the standard dialogue windows available with JOptionPane.

Dialogues usually have an associated parent frame. Typically, this frame will contain the application, and the dialogue will be created in response to the user performing a selection
or some kind of action in the parent frame. Note that once a dialogue window appears, the user cannot perform any other action until the dialogue window has been clicked. The most
useful method in the JOptionPane class is showMessageDialog. The statement

JOptionPane.showMessageDialog(fr, ''A Message");

where fr is the parent frame, creates a default information message dialogue window with a corresponding information icon. The default title is "Message," and the message text is
the second argument, namely, ''A Message." The result is shown in Figure 8.4. Other invocations of the method are in the form

JOptionPane.showMessageDialog(fr, title, text, message_type);

where fr is the parent frame, title is the text to be displayed in the title portion of the window, text is the message displayed, and message_type determines the icon to be displayed. To illustrate this, the following statements produce the remaining dialogue windows shown in Figure.

JOptionPane.showMessageDialog(fr, "An Error Message",
"Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(fr, "An Information Message", "Information", JOptionPane.INFORMATION_MESSAGE);


Note that the preceding statement is similar to the first default dialogue, except in this case, the title text is supplied as a parameter.

JOptionPane.showMessageDialog(fr, "A Warning
Message", "Warning", JOptionPane.WARNING_MESSAGE);

JOptionPane.showMessageDialog(fr, "A Question
Message", "Question", JOptionPane.QUESTION_MESSAGE);

JOptionPane.showMessageDialog(fr, "A Plain Message",
"Plain", JOptionPane.PLAIN_MESSAGE);


Note that for the preceding statement no icon is displayed.

We should briefly mention here a few other classes that provide for specialized dialogues similar to JOptionPane. The javax.swing.ProgressMonitor class is used to show the progress of an operation. This is done by means of a dialogue window and progress bar. The javax.swing.JColorChooser class is used to manipulate and select a color. The javax.swing.JFileChooser class is used to choose a file.

No comments:

Post a Comment