- Java show message dialogue
- Nested Class Summary
- Nested classes/interfaces inherited from class javax.swing.JComponent
- Nested classes/interfaces inherited from class java.awt.Container
- Nested classes/interfaces inherited from class java.awt.Component
- Field Summary
- Fields inherited from class javax.swing.JComponent
- Fields inherited from class java.awt.Component
- Fields inherited from interface java.awt.image.ImageObserver
- Constructor Summary
- Method Summary
- Java Swing – JOptionPane showMessageDialog example
- 1. Component & Object
- 2. Component, Object, String & int
- 3. Component, Object, String, int & Icon
- 4. A more advanced example
- References
Java show message dialogue
JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. For information about using JOptionPane , see How to Make Dialogs, a section in The Java Tutorial. While the JOptionPane class may appear complex because of the large number of methods, almost all uses of this class are one-line calls to one of the static showXxxDialog methods shown below:
Method Name Description showConfirmDialog Asks a confirming question, like yes/no/cancel. showInputDialog Prompt for some input. showMessageDialog Tell the user about something that has happened. showOptionDialog The Grand Unification of the above three.
Each of these methods also comes in a showInternalXXX flavor, which uses an internal frame to hold the dialog box (see JInternalFrame ). Multiple convenience methods have also been defined — overloaded versions of the basic methods that use different parameter lists. All dialogs are modal. Each showXxxDialog method blocks the caller until the user’s interaction is complete.
icon | message |
input value | |
option buttons |
The basic appearance of one of these dialog boxes is generally similar to the picture at the right, although the various look-and-feels are ultimately responsible for the final result. In particular, the look-and-feels will adjust the layout to accommodate the option pane’s ComponentOrientation property.
Parameters:
The parameters to these methods follow consistent patterns:
- ERROR_MESSAGE
- INFORMATION_MESSAGE
- WARNING_MESSAGE
- QUESTION_MESSAGE
- PLAIN_MESSAGE
- DEFAULT_OPTION
- YES_NO_OPTION
- YES_NO_CANCEL_OPTION
- OK_CANCEL_OPTION
- YES_OPTION
- NO_OPTION
- CANCEL_OPTION
- OK_OPTION
- CLOSED_OPTION
JOptionPane.showInternalMessageDialog(frame, "information", "information", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
Show an internal information dialog with the options yes/no/cancel and message ‘please choose one’ and title information:
JOptionPane.showInternalConfirmDialog(frame, "please choose one", "information", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
Show a warning dialog with the options OK, CANCEL, title ‘Warning’, and message ‘Click OK to continue’:
Object[] options = < "OK", "CANCEL" >; JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
Show a dialog asking the user to type in a String: String inputValue = JOptionPane.showInputDialog(«Please input a value»); Show a dialog asking the user to select a String:
Object[] possibleValues = < "First", "Second", "Third" >;
Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
Direct Use:
To create and use an JOptionPane directly, the standard pattern is roughly as follows:
JOptionPane pane = new JOptionPane(arguments); pane.set.Xxxx(. ); // Configure JDialog dialog = pane.createDialog(parentComponent, title); dialog.show(); Object selectedValue = pane.getValue(); if(selectedValue == null) return CLOSED_OPTION; //If there is not an array of option buttons: if(options == null) < if(selectedValue instanceof Integer) return ((Integer)selectedValue).intValue(); return CLOSED_OPTION; >//If there is an array of option buttons: for(int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) < if(options[counter].equals(selectedValue)) return counter; >return CLOSED_OPTION;
Warning: Swing is not thread safe. For more information see Swing’s Threading Policy. Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™ has been added to the java.beans package. Please see XMLEncoder .
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.JComponent
Nested classes/interfaces inherited from class java.awt.Container
Nested classes/interfaces inherited from class java.awt.Component
Field Summary
Return value from class method if user closes window without selecting anything, more than likely this should be treated as either a CANCEL_OPTION or NO_OPTION .
Type meaning Look and Feel should not supply any options — only use the options from the JOptionPane .
Fields inherited from class javax.swing.JComponent
Fields inherited from class java.awt.Component
Fields inherited from interface java.awt.image.ImageObserver
Constructor Summary
Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI.
Creates an instance of JOptionPane to display a message with the specified message type and the default options,
Creates an instance of JOptionPane to display a message with the specified message type, options, and icon.
Creates an instance of JOptionPane to display a message with the specified message type, icon, and options.
Creates an instance of JOptionPane to display a message with the specified message type, icon, and options, with the initially-selected option specified.
Method Summary
Creates and returns a new JDialog wrapping this centered on the parentComponent in the parentComponent ‘s frame.
Sets the initial value that is to be enabled — the Component that has the focus when the pane is initially displayed.
Sets the input selection values for a pane that provides the user with a list of items to choose from.
Brings up a dialog with the options Yes, No and Cancel; with the title, Select an Option.
Brings up a dialog where the number of choices is determined by the optionType parameter, where the messageType parameter determines the icon to display.
Brings up a dialog with a specified icon, where the number of choices is determined by the optionType parameter.
Shows a dialog requesting input from the user parented to parentComponent with the dialog having the title title and message type messageType .
Prompts the user for input in a blocking dialog where the initial selection, possible selections, and all other options can be specified.
Shows a question-message dialog requesting input from the user, with the input value initialized to initialSelectionValue .
Brings up an internal dialog panel with the options Yes, No and Cancel; with the title, Select an Option.
Brings up a internal dialog panel where the number of choices is determined by the optionType parameter.
Brings up an internal dialog panel where the number of choices is determined by the optionType parameter, where the messageType parameter determines the icon to display.
Brings up an internal dialog panel with a specified icon, where the number of choices is determined by the optionType parameter.
Shows an internal question-message dialog requesting input from the user parented to parentComponent .
Shows an internal dialog requesting input from the user parented to parentComponent with the dialog having the title title and message type messageType .
Prompts the user for input in a blocking internal dialog where the initial selection, possible selections, and all other options can be specified.
Brings up an internal dialog panel that displays a message using a default icon determined by the messageType parameter.
Brings up an internal dialog panel with a specified icon, where the initial choice is determined by the initialValue parameter and the number of choices is determined by the optionType parameter.
Brings up a dialog that displays a message using a default icon determined by the messageType parameter.
Brings up a dialog with a specified icon, where the initial choice is determined by the initialValue parameter and the number of choices is determined by the optionType parameter.
Java Swing – JOptionPane showMessageDialog example
This is a review of the showMessageDialog() method of JOptionPane Class. This method is a quick and easy way to tell the user about something that has happened . The showMessageDialog() can be called using the following combinations of parameters:
Component, Object Component, Object, String, int Component, Object, String, int, Icon
- Component – The first parameter is a component which determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used.
- Object – The second parameter can be any objects. (In some older versions of Java you might get a compiler error when using primitive types directly).
- String – The third parameter is a String placed as the title of the message dialog window.
- int – The int that follows the String is the MessageType . The different MessageTypes for JOptionPane , are:
- ERROR_MESSAGE
- INFORMATION_MESSAGE
- WARNING_MESSAGE
- QUESTION_MESSAGE
- PLAIN_MESSAGE
- Icon – The last parameter is an Icon that is displayed inside the dialog and overrides the default MessageType icon.
1. Component & Object
The simplest way to use the message dialog. Example with Component set to null and a String as second argument:
package com.mkyong.messageDialog; import javax.swing.JOptionPane; public class SimpleDialog1 < public static void main(String[] args)< JOptionPane.showMessageDialog(null, "Simple Information Message"); >>
2. Component, Object, String & int
Adding some more information to the message dialog. Example with Component set to null and a double as second parameter:
package com.mkyong.messageDialog; import javax.swing.JOptionPane; public class SimpleDialog2a < public static void main(String[] args)< JOptionPane.showMessageDialog(null, 8.9, "This is not an integer.", JOptionPane.PLAIN_MESSAGE); >>
Example of an Error Message ( Component set to null, String Object ):
package com.mkyong.messageDialog; import javax.swing.JOptionPane; public class SimpleDialog2b < public static void main(String[] args)< JOptionPane.showMessageDialog(null, "Uh-oh!", "Error", JOptionPane.ERROR_MESSAGE); >>
3. Component, Object, String, int & Icon
Make your message dialog “prettier”. Example with Icon retrieved from a directory:
package messageDialog; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class SimpleDialog3a < public static void main(String[] args)< ImageIcon icon = new ImageIcon("src/images/turtle64.png"); JOptionPane.showMessageDialog(null, "I like turtles.", "Customized Dialog", JOptionPane.INFORMATION_MESSAGE, icon); >>
Example with Component set to a frame :
package com.mkyong.messageDialog; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JOptionPane; import java.awt.Color; public class MessageDialogInFrame extends JFrame < public MessageDialogInFrame() < getContentPane().setBackground(Color.DARK_GRAY); setTitle("Message Dialog in Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setResizable(false); setSize(400, 300); getContentPane().setLayout(null); >public static void main(String[] args) < ImageIcon icon = new ImageIcon("src/images/turtle64.png"); JOptionPane.showMessageDialog(new MessageDialogInFrame(), "I appear as part of the frame!!", "Customized Dialog", JOptionPane.INFORMATION_MESSAGE, icon); >>
4. A more advanced example
For this example we are passing a JPanel as the Object parameter. The JPanel is customized and has a JLabel added to it. We are also manipulating the size of the OptionPane using a call to UIManager .
package com.mkyong.messageDialog; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.UIManager; public class MessageDialogPanel < public static void main(String[] args)< ImageIcon icon = new ImageIcon("src/images/turtle64.png"); JPanel panel = new JPanel(); panel.setBackground(new Color(102, 205, 170)); panel.setSize(new Dimension(200, 64)); panel.setLayout(null); JLabel label = new JLabel("Turtles are awesome. :D"); label.setBounds(0, 0, 200, 64); label.setFont(new Font("Arial", Font.BOLD, 11)); label.setHorizontalAlignment(SwingConstants.CENTER); panel.add(label); UIManager.put("OptionPane.minimumSize",new Dimension(300, 120)); JOptionPane.showMessageDialog(null, panel, "Customized Message Dialog", JOptionPane.PLAIN_MESSAGE, icon); >>
References
Marilena
Marilena Panagiotidou is a senior at University of the Aegean, in the department of Information and Communication Systems Engineering. She is passionate about programming in a wide range of languages. You can contact her at m4r1l3n4gr@gmail.com or through her LinkedIn.