Site Meter

Thursday, November 18, 2010

How to Make Dialogs in Java

Hai all T-readers, finally i finish my assignment and this time to give you my experience. now, i want to share to you about making dialog in java program. if this sound familiar to you?? If you're a programmer, i think this is a common. but now, i want to share you about this subject. i'm really sorry about my bad english language. because, this is my first english article. hehe..

A Dialog window is an independent subwindow meant to carry temporary notice apart from the main Swing Application Window. Most Dialogs present an error message or warning to a user, but Dialogs can present images, directory trees, or just about anything compatible with the main Swing Application that manages them.For convenience, several Swing component classes can directly instantiate and display dialogs. To create simple, standard dialogs, you use the 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:




Here is the code that creates and shows it:

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.javaand 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 usingshowMessageDialog:

 //default title and icon

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

Related Posts Plugin for WordPress, Blogger...
 
MMORPG Games - MMORPG List - Video Game Music