JOptionPane
class. The ProgressMonitor
class can put up a dialog that shows the progress of an operation. Two other classes, JColorChooser
and JFileChooser
, also supply standard dialogs. To bring up a print dialog, you can use the Printing API. To create a custom dialog, use the JDialog
class directly.The code for simple dialogs can be minimal. For example, here is an informational dialog:
JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");
There are two most useful
showXxxDialog
methods are showMessageDialog
and showOptionDialog
. TheshowMessageDialog
method displays a simple, one-button dialog. The showOptionDialog
method displays a customized dialog — it can display a variety of buttons with customized button text, and can contain a standard text message or a collection of components.Here i have some examples, taken from
DialogDemo.java
, of using showMessageDialog
,showOptionDialog
, and the JOptionPane
constructor. For more example code, see DialogDemo.java
and the other programs listed in Examples that Use Dialogs. 1.
showMessageDialog
Displays a modal dialog with one button, which is labeled "OK" (or the localized equivalent). You can easily specify the message, icon, and title that the dialog displays. Here are some examples of using
showMessageDialog
:JOptionPane.showMessageDialog(frame,
"Eggs are not supposed to be green.",
"Message");
//custom title, warning icon
JOptionPane.showMessageDialog(frame,
"Eggs are not supposed to be green.",
"Inane warning",
JOptionPane.WARNING_MESSAGE);
//custom title, custom icon
JOptionPane.showMessageDialog(frame,
"Eggs are not supposed to be green.",
"Inane custom dialog",
JOptionPane.INFORMATION_MESSAGE,
icon);
2. showOptionDialog
Displays a modal dialog with the specified buttons, icons, message, title, and so on. With this method, you can change the text that appears on the buttons of standard dialogs. You can also perform many other kinds of customization.
//Custom button text
Object[] options = {"Yes, please",
"No, thanks",
"No eggs, no ham!"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like some green eggs to go "
+ "with that ham?",
"A Silly Question",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[2]);
3. JOptionPane
(constructor)
Creates a JOptionPane
with the specified buttons, icons, message, title, and so on. You must then 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 for details.
final JOptionPane optionPane = new JOptionPane(
"The only way to close this dialog is by\n"
+ "pressing one of the following buttons.\n"
+ "Do you understand?",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION);
I think, that's all i can give to you. i hope, this information useful to you all..
see you next time...
* Special thanks to java tutorial