Java: Standard Dialog Boxes (swing)

Contributor Icon Contributed by William_Wilson  
Tag Icon Tagged: Java programming  

Displaying The Standard Dialog Boxes in Java


*NOTE*This recipe simply shows how to use the code, to display the dialog boxes, if you require a fully coded example including buttons, and JFrame, please E-mail the address below.

There are many different dialog boxes that come standard with Java, including:
* General
* Warning
* Error
* Information
* Question/Confirmation
* Input
* Option (many variations on this one)

-the option of text, icon and which button options to choose is all for you to decide, based on the type of dialog you choose.

In each exmaple below, the option this can be left as is, or -as is recommended- replaced with the name of the class/object that called it.

The First String will be the text displayed in the window, and the second String will be the title of the dialog. The main difference in the dialog types are the button options, and the displayed icon.

General:
JOptionPane.showMessageDialog(this, “General msg”, “General”, JOptionPane.PLAIN_MESSAGE);

Warning:
JOptionPane.showMessageDialog(this, “A Warning msg.”, “Warning”, JOptionPane.WARNING_MESSAGE);

Error:
JOptionPane.showMessageDialog(this, “An Error msg!”, “Error”, OptionPane.ERROR_MESSAGE);

Information:
JOptionPane.showMessageDialog(this, “An Information msg”, “Information”, JOptionPane.INFORMATION_MESSAGE);

Question:
int result = JOptionPane.showConfirmDialog(this, “A Question?”, “Question”, JOptionPane.YES_NO_OPTION);
if (result == 0)
System.out.println(“YES”);
else
System.out.println(“NO”);
}

Input:
String inputValue = JOptionPane.showInputDialog(“What to input”);

Option: (buttons)
Object[] opt = { “Outstanding”, “Excellent”, “Good”, “Fair”, “Poor” };
int result = JOptionPane.showOptionDialog(this, “What do you think of this recipe?”, “Option”, JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, opt, options[0]);

Option: (list)
Object[] opt = { “One”, “Two”, “Three”, “Four” };
Object selectedValue = JOptionPane.showInputDialog(this, “choose a value”, “Option”, JOptionPane.INFORMATION_MESSAGE, null, opt, options[1]);

Have fun with dialog boxes!!!

Questions and Comments: william_a_wilson@hotmail.com
-William. ยง (marvin_gohan)

 

No Comments -


No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment -