2 Copyright (C) 2004, 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.AWTEvent;
42 import java.awt.ActiveEvent;
43 import java.awt.Component;
44 import java.awt.Container;
45 import java.awt.EventQueue;
46 import java.awt.Frame;
47 import java.awt.MenuComponent;
48 import java.awt.Toolkit;
49 import java.awt.event.MouseAdapter;
50 import java.awt.event.MouseMotionAdapter;
52 import javax.accessibility.Accessible;
53 import javax.accessibility.AccessibleContext;
54 import javax.accessibility.AccessibleRole;
55 import javax.swing.plaf.OptionPaneUI;
58 * This class creates different types of JDialogs and JInternalFrames that can
59 * ask users for input or pass on information. JOptionPane can be used by
60 * calling one of the show static methods or by creating an instance of
61 * JOptionPane and calling createDialog or createInternalFrame.
63 public class JOptionPane extends JComponent implements Accessible
66 * Provides the accessibility features for the <code>JOptionPane</code>
69 protected class AccessibleJOptionPane extends JComponent.AccessibleJComponent
71 private static final long serialVersionUID = 686071432213084821L;
74 * Creates a new <code>AccessibleJOptionPane</code> instance.
76 protected AccessibleJOptionPane()
78 // Nothing to do here.
82 * Returns the accessible role of this object, which is always
83 * {@link AccessibleRole#OPTION_PANE}.
85 * @return the accessible role of this object
87 public AccessibleRole getAccessibleRole()
89 return AccessibleRole.OPTION_PANE;
93 private static final long serialVersionUID = 5231143276678566796L;
95 /** The value returned when cancel option is selected. */
96 public static final int CANCEL_OPTION = 2;
98 /** The value returned when the dialog is closed without a selection. */
99 public static final int CLOSED_OPTION = -1;
101 /** An option used in confirmation dialog methods. */
102 public static final int DEFAULT_OPTION = -1;
104 /** The value returned when the no option is selected. */
105 public static final int NO_OPTION = 1;
107 /** An option used in confirmation dialog methods. */
108 public static final int OK_CANCEL_OPTION = 2;
110 /** The value returned when the ok option is selected. */
111 public static final int OK_OPTION = 0;
113 /** An option used in confirmation dialog methods. */
114 public static final int YES_NO_CANCEL_OPTION = 1;
116 /** An option used in confirmation dialog methods. */
117 public static final int YES_NO_OPTION = 0;
119 /** The value returned when the yes option is selected. */
120 public static final int YES_OPTION = 0;
122 /** Identifier for the error message type. */
123 public static final int ERROR_MESSAGE = 0;
125 /** Identifier for the information message type. */
126 public static final int INFORMATION_MESSAGE = 1;
128 /** Identifier for the plain message type. */
129 public static final int PLAIN_MESSAGE = -1;
131 /** Identifier for the question message type. */
132 public static final int QUESTION_MESSAGE = 3;
134 /** Identifier for the warning message type. */
135 public static final int WARNING_MESSAGE = 2;
138 * The identifier for the propertyChangeEvent when the icon property
141 public static final String ICON_PROPERTY = "icon";
144 * The identifier for the propertyChangeEvent when the initialSelectionValue
147 public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
150 * The identifier for the propertyChangeEvent when the initialValue property
153 public static final String INITIAL_VALUE_PROPERTY = "initialValue";
156 * The identifier for the propertyChangeEvent when the inputValue property
159 public static final String INPUT_VALUE_PROPERTY = "inputValue";
162 * The identifier for the propertyChangeEvent when the message property
165 public static final String MESSAGE_PROPERTY = "message";
168 * The identifier for the propertyChangeEvent when the messageType property
171 public static final String MESSAGE_TYPE_PROPERTY = "messageType";
174 * The identifier for the propertyChangeEvent when the optionType property
177 public static final String OPTION_TYPE_PROPERTY = "optionType";
180 * The identifier for the propertyChangeEvent when the options property
183 public static final String OPTIONS_PROPERTY = "options";
186 * The identifier for the propertyChangeEvent when the selectionValues
189 public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
192 * The identifier for the propertyChangeEvent when the value property
195 public static final String VALUE_PROPERTY = "value";
198 * The identifier for the propertyChangeEvent when the wantsInput property
201 public static final String WANTS_INPUT_PROPERTY = "wantsInput";
203 /** The value returned when the inputValue is uninitialized. */
204 public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
206 /** The icon displayed in the dialog/internal frame. */
209 /** The initial selected value in the input component. */
210 protected Object initialSelectionValue;
212 /** The object that is initially selected for options. */
213 protected Object initialValue;
215 /** The value the user inputs. */
216 protected Object inputValue = UNINITIALIZED_VALUE;
218 /** The message displayed in the dialog/internal frame. */
219 protected Object message;
221 /** The type of message displayed. */
222 protected int messageType = PLAIN_MESSAGE;
225 * The options (usually buttons) aligned at the bottom for the user to
228 protected Object[] options;
230 /** The type of options to display. */
231 protected int optionType = DEFAULT_OPTION;
233 /** The input values the user can select. */
234 protected Object[] selectionValues;
236 /** The value returned by selecting an option. */
237 protected Object value = UNINITIALIZED_VALUE;
239 /** Whether the Dialog/InternalFrame needs input. */
240 protected boolean wantsInput;
242 /** The common frame used when no parent is provided. */
243 private static Frame privFrame = (Frame) SwingUtilities.getOwnerFrame(null);
246 * Creates a new JOptionPane object using a message of "JOptionPane
247 * message", using the PLAIN_MESSAGE type and DEFAULT_OPTION.
251 this("JOptionPane message", PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
255 * Creates a new JOptionPane object using the given message using the
256 * PLAIN_MESSAGE type and DEFAULT_OPTION.
258 * @param message The message to display.
260 public JOptionPane(Object message)
262 this(message, PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
266 * Creates a new JOptionPane object using the given message and messageType
267 * and DEFAULT_OPTION.
269 * @param message The message to display.
270 * @param messageType The type of message.
272 public JOptionPane(Object message, int messageType)
274 this(message, messageType, DEFAULT_OPTION, null, null, null);
278 * Creates a new JOptionPane object using the given message, messageType and
281 * @param message The message to display.
282 * @param messageType The type of message.
283 * @param optionType The type of options.
285 public JOptionPane(Object message, int messageType, int optionType)
287 this(message, messageType, optionType, null, null, null);
291 * Creates a new JOptionPane object using the given message, messageType,
292 * optionType and icon.
294 * @param message The message to display.
295 * @param messageType The type of message.
296 * @param optionType The type of options.
297 * @param icon The icon to display.
299 public JOptionPane(Object message, int messageType, int optionType, Icon icon)
301 this(message, messageType, optionType, icon, null, null);
305 * Creates a new JOptionPane object using the given message, messageType,
306 * optionType, icon and options.
308 * @param message The message to display.
309 * @param messageType The type of message.
310 * @param optionType The type of options.
311 * @param icon The icon to display.
312 * @param options The options given.
314 public JOptionPane(Object message, int messageType, int optionType,
315 Icon icon, Object[] options)
317 this(message, messageType, optionType, icon, options, null);
321 * Creates a new JOptionPane object using the given message, messageType,
322 * optionType, icon, options and initialValue. The initialValue will be
325 * @param message The message to display.
326 * @param messageType The type of message.
327 * @param optionType The type of options.
328 * @param icon The icon to display.
329 * @param options The options given.
330 * @param initialValue The component to focus on initially.
332 * @throws IllegalArgumentException If the messageType or optionType are not
335 public JOptionPane(Object message, int messageType, int optionType,
336 Icon icon, Object[] options, Object initialValue)
338 this.message = message;
339 if (! validMessageType(messageType))
340 throw new IllegalArgumentException("Message Type not legal value.");
341 this.messageType = messageType;
342 if (! validOptionType(optionType))
343 throw new IllegalArgumentException("Option Type not legal value.");
344 this.optionType = optionType;
346 this.options = options;
347 this.initialValue = initialValue;
349 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
355 * This method creates a new JDialog that is either centered around the
356 * parent's frame or centered on the screen (if the parent is null). The
357 * JDialog will not be resizable and will be modal. Once the JDialog is
358 * disposed, the inputValue and value properties will be set by the
361 * @param parentComponent The parent of the Dialog.
362 * @param title The title in the bar of the JDialog.
364 * @return A new JDialog based on the JOptionPane configuration.
366 public JDialog createDialog(Component parentComponent, String title)
368 Frame toUse = getFrameForComponent(parentComponent);
370 toUse = getRootFrame();
372 JDialog dialog = new JDialog(toUse, title);
373 inputValue = UNINITIALIZED_VALUE;
374 value = UNINITIALIZED_VALUE;
376 dialog.getContentPane().add(this);
377 dialog.setModal(true);
378 dialog.setResizable(false);
380 dialog.setLocationRelativeTo(parentComponent);
386 * This method creates a new JInternalFrame that is in the JLayeredPane
387 * which contains the parentComponent given. If no suitable JLayeredPane
388 * can be found from the parentComponent given, a RuntimeException will be
391 * @param parentComponent The parent to find a JDesktopPane from.
392 * @param title The title of the JInternalFrame.
394 * @return A new JInternalFrame based on the JOptionPane configuration.
396 * @throws RuntimeException If no suitable JDesktopPane is found.
398 * @specnote The specification says that the internal frame is placed
399 * in the nearest <code>JDesktopPane</code> that is found in
400 * <code>parent</code>'s ancestors. The behaviour of the JDK
401 * is that it actually looks up the nearest
402 * <code>JLayeredPane</code> in <code>parent</code>'s ancestors.
405 public JInternalFrame createInternalFrame(Component parentComponent,
407 throws RuntimeException
409 // Try to find a JDesktopPane.
410 JLayeredPane toUse = getDesktopPaneForComponent(parentComponent);
411 // If we don't have a JDesktopPane, we try to find a JLayeredPane.
413 toUse = JLayeredPane.getLayeredPaneAbove(parentComponent);
414 // If this still fails, we throw a RuntimeException.
416 throw new RuntimeException
417 ("parentComponent does not have a valid parent");
419 JInternalFrame frame = new JInternalFrame(title);
421 inputValue = UNINITIALIZED_VALUE;
422 value = UNINITIALIZED_VALUE;
424 frame.setContentPane(this);
425 frame.setClosable(true);
428 frame.setLayer(JLayeredPane.MODAL_LAYER);
431 frame.setVisible(true);
437 * Returns the object that provides accessibility features for this
438 * <code>JOptionPane</code> component.
440 * @return The accessible context (an instance of
441 * {@link AccessibleJOptionPane}).
443 public AccessibleContext getAccessibleContext()
445 if (accessibleContext == null)
446 accessibleContext = new AccessibleJOptionPane();
447 return accessibleContext;
451 * This method returns the JDesktopPane for the given parentComponent or
452 * null if none can be found.
454 * @param parentComponent The component to look in.
456 * @return The JDesktopPane for the given component or null if none can be
459 public static JDesktopPane getDesktopPaneForComponent(Component parentComponent)
461 return (JDesktopPane) SwingUtilities.getAncestorOfClass(JDesktopPane.class,
466 * This method returns the Frame for the given parentComponent or null if
469 * @param parentComponent The component to look in.
471 * @return The Frame for the given component or null if none can be found.
473 public static Frame getFrameForComponent(Component parentComponent)
475 return (Frame) SwingUtilities.getAncestorOfClass(Frame.class,
480 * This method returns the icon displayed.
482 * @return The icon displayed.
484 public Icon getIcon()
490 * This method returns the value initially selected from the list of values
491 * the user can input.
493 * @return The initial selection value.
495 public Object getInitialSelectionValue()
497 return initialSelectionValue;
501 * This method returns the value that is focused from the list of options.
503 * @return The initial value from options.
505 public Object getInitialValue()
511 * This method returns the value that the user input.
513 * @return The user's input value.
515 public Object getInputValue()
517 if (getValue().equals(new Integer(CANCEL_OPTION)))
523 * This method returns the maximum characters per line. By default, this is
526 * @return The maximum characters per line.
528 public int getMaxCharactersPerLineCount()
530 return Integer.MAX_VALUE;
534 * This method returns the message displayed.
536 * @return The message displayed.
538 public Object getMessage()
544 * This method returns the message type.
546 * @return The message type.
548 public int getMessageType()
554 * This method returns the options.
556 * @return The options.
558 public Object[] getOptions()
564 * This method returns the option type.
566 * @return The option type.
568 public int getOptionType()
574 * This method returns the Frame used by JOptionPane dialog's that have no
577 * @return The Frame used by dialogs that have no parent.
579 public static Frame getRootFrame()
585 * This method returns the selection values.
587 * @return The selection values.
589 public Object[] getSelectionValues()
591 return selectionValues;
595 * This method returns the UI used by the JOptionPane.
597 * @return The UI used by the JOptionPane.
599 public OptionPaneUI getUI()
601 return (OptionPaneUI) ui;
605 * This method returns an identifier to determine which UI class will act as
608 * @return The UI identifier.
610 public String getUIClassID()
612 return "OptionPaneUI";
616 * This method returns the value that the user selected out of options.
618 * @return The value that the user selected out of options.
620 public Object getValue()
626 * This method returns whether this JOptionPane wants input.
628 * @return Whether this JOptionPane wants input.
630 public boolean getWantsInput()
636 * This method returns a String that describes this JOptionPane.
638 * @return A String that describes this JOptionPane.
640 protected String paramString()
642 return "JOptionPane";
646 * This method requests focus for the initial value.
648 public void selectInitialValue()
651 ((OptionPaneUI) ui).selectInitialValue(this);
655 * This method changes the icon property.
657 * @param newIcon The new icon to use.
659 public void setIcon(Icon newIcon)
665 firePropertyChange(ICON_PROPERTY, old, icon);
670 * This method changes the initial selection property.
672 * @param newValue The new initial selection.
674 public void setInitialSelectionValue(Object newValue)
676 if (initialSelectionValue != newValue)
678 Object old = initialSelectionValue;
679 initialSelectionValue = newValue;
680 firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, old,
681 initialSelectionValue);
686 * This method changes the initial value property.
688 * @param newValue The new initial value.
690 public void setInitialValue(Object newValue)
692 if (initialValue != newValue)
694 Object old = initialValue;
695 initialValue = newValue;
696 firePropertyChange(INITIAL_VALUE_PROPERTY, old, initialValue);
701 * This method changes the inputValue property.
703 * @param newValue The new inputValue.
705 public void setInputValue(Object newValue)
707 if (inputValue != newValue)
709 Object old = inputValue;
710 inputValue = newValue;
711 firePropertyChange(INPUT_VALUE_PROPERTY, old, inputValue);
716 * This method changes the message property.
718 * @param newMessage The new message.
720 public void setMessage(Object newMessage)
722 if (message != newMessage)
724 Object old = message;
725 message = newMessage;
726 firePropertyChange(MESSAGE_PROPERTY, old, message);
731 * This method changes the messageType property.
733 * @param newType The new messageType.
735 * @throws IllegalArgumentException If the messageType is not valid.
737 public void setMessageType(int newType)
739 if (! validMessageType(newType))
740 throw new IllegalArgumentException("Message Type not legal value.");
741 if (newType != messageType)
743 int old = messageType;
744 messageType = newType;
745 firePropertyChange(MESSAGE_TYPE_PROPERTY, old, messageType);
750 * This method changes the options property.
752 * @param newOptions The new options.
754 public void setOptions(Object[] newOptions)
756 if (options != newOptions)
758 Object[] old = options;
759 options = newOptions;
760 firePropertyChange(OPTIONS_PROPERTY, old, options);
765 * This method changes the optionType property.
767 * @param newType The new optionType.
769 * @throws IllegalArgumentException If the optionType is not valid.
771 public void setOptionType(int newType)
773 if (! validOptionType(newType))
774 throw new IllegalArgumentException("Option Type not legal value.");
775 if (newType != optionType)
777 int old = optionType;
778 optionType = newType;
779 firePropertyChange(OPTION_TYPE_PROPERTY, old, optionType);
784 * This method changes the Frame used for JOptionPane dialogs that have no
787 * @param newRootFrame The Frame to use for dialogs that have no parent.
789 public static void setRootFrame(Frame newRootFrame)
791 privFrame = newRootFrame;
795 * This method changes the selectionValues property.
797 * @param newValues The new selectionValues.
799 public void setSelectionValues(Object[] newValues)
801 if (newValues != selectionValues)
803 if (newValues != null)
805 Object[] old = selectionValues;
806 selectionValues = newValues;
807 firePropertyChange(SELECTION_VALUES_PROPERTY, old, selectionValues);
812 * This method sets the UI used with the JOptionPane.
814 * @param ui The UI used with the JOptionPane.
816 public void setUI(OptionPaneUI ui)
822 * This method sets the value has been selected out of options.
824 * @param newValue The value that has been selected out of options.
826 public void setValue(Object newValue)
828 if (value != newValue)
832 firePropertyChange(VALUE_PROPERTY, old, value);
837 * This method changes the wantsInput property.
839 * @param newValue Whether this JOptionPane requires input.
841 public void setWantsInput(boolean newValue)
843 if (wantsInput != newValue)
845 boolean old = wantsInput;
846 wantsInput = newValue;
847 firePropertyChange(WANTS_INPUT_PROPERTY, old, wantsInput);
852 * This method shows a confirmation dialog with the title "Select an Option"
853 * and displays the given message. The parent frame will be the same as the
854 * parent frame of the given parentComponent. This method returns the
855 * option chosen by the user.
857 * @param parentComponent The parentComponent to find a frame in.
858 * @param message The message to display.
860 * @return The option that was selected.
862 public static int showConfirmDialog(Component parentComponent, Object message)
864 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
865 JDialog dialog = pane.createDialog(parentComponent, "Select an Option");
868 if (pane.getValue() instanceof Integer)
869 return ((Integer) pane.getValue()).intValue();
874 * This method shows a confirmation dialog with the given message,
875 * optionType and title. The frame that owns the dialog will be the same
876 * frame that holds the given parentComponent. This method returns the
877 * option that was chosen.
879 * @param parentComponent The component to find a frame in.
880 * @param message The message displayed.
881 * @param title The title of the dialog.
882 * @param optionType The optionType.
884 * @return The option that was chosen.
886 public static int showConfirmDialog(Component parentComponent,
887 Object message, String title,
890 JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
891 JDialog dialog = pane.createDialog(parentComponent, title);
894 if (pane.getValue() instanceof Integer)
895 return ((Integer) pane.getValue()).intValue();
900 * This method shows a confirmation dialog with the given message, title,
901 * messageType and optionType. The frame owner will be the same frame as
902 * the one that holds the given parentComponent. This method returns the
903 * option selected by the user.
905 * @param parentComponent The component to find a frame in.
906 * @param message The message displayed.
907 * @param title The title of the dialog.
908 * @param optionType The optionType.
909 * @param messageType The messageType.
911 * @return The selected option.
913 public static int showConfirmDialog(Component parentComponent,
914 Object message, String title,
915 int optionType, int messageType)
917 JOptionPane pane = new JOptionPane(message, messageType, optionType);
918 JDialog dialog = pane.createDialog(parentComponent, title);
921 if (pane.getValue() instanceof Integer)
922 return ((Integer) pane.getValue()).intValue();
927 * This method shows a confirmation dialog with the given message, title,
928 * optionType, messageType and icon. The frame owner will be the same as
929 * the one that holds the given parentComponent. This method returns the
930 * option selected by the user.
932 * @param parentComponent The component to find a frame in.
933 * @param message The message displayed.
934 * @param title The title of the dialog.
935 * @param optionType The optionType.
936 * @param messageType The messsageType.
937 * @param icon The icon displayed.
939 * @return The selected option.
941 public static int showConfirmDialog(Component parentComponent,
942 Object message, String title,
943 int optionType, int messageType,
946 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
947 JDialog dialog = pane.createDialog(parentComponent, title);
950 if (pane.getValue() instanceof Integer)
951 return ((Integer) pane.getValue()).intValue();
956 * This method will show a QUESTION_MESSAGE input dialog with the given
957 * message. No selectionValues is set so the Look and Feel will usually
958 * give the user a TextField to fill out. The frame owner will be the same
959 * frame that holds the given parentComponent. This method will return the
960 * value entered by the user.
962 * @param parentComponent The component to find a frame in.
963 * @param message The message displayed.
965 * @return The value entered by the user.
967 public static String showInputDialog(Component parentComponent,
970 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
971 pane.setWantsInput(true);
972 JDialog dialog = pane.createDialog(parentComponent, null);
975 return (String) pane.getInputValue();
979 * This method will show a QUESTION_MESSAGE type input dialog with the given
980 * message and initialSelectionValue. Since there is no selectionValues
981 * set, the Look and Feel will usually give a TextField to fill out. The
982 * frame owner will be the same as the one that holds the given
983 * parentComponent. This method will return the value entered by the user.
985 * @param parentComponent The component to find a frame in.
986 * @param message The message to display.
987 * @param initialSelectionValue The initially selected value.
989 * @return The value the user input.
991 public static String showInputDialog(Component parentComponent,
993 Object initialSelectionValue)
995 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
996 pane.setInitialSelectionValue(initialSelectionValue);
997 pane.setWantsInput(true);
998 JDialog dialog = pane.createDialog(parentComponent, null);
1001 return (String) pane.getInputValue();
1005 * This method displays a new input dialog with the given message, title and
1006 * messageType. Since no selectionValues value is given, the Look and Feel
1007 * will usually give the user a TextField to input data to. This method
1008 * returns the value the user inputs.
1010 * @param parentComponent The component to find a frame in.
1011 * @param message The message to display.
1012 * @param title The title of the dialog.
1013 * @param messageType The messageType.
1015 * @return The value the user input.
1017 public static String showInputDialog(Component parentComponent,
1018 Object message, String title,
1021 JOptionPane pane = new JOptionPane(message, messageType);
1022 pane.setWantsInput(true);
1023 JDialog dialog = pane.createDialog(parentComponent, title);
1026 return (String) pane.getInputValue();
1030 * This method shows an input dialog with the given message, title,
1031 * messageType, icon, selectionValues, and initialSelectionValue. This
1032 * method returns the value that the user selects.
1034 * @param parentComponent The component to find a frame in.
1035 * @param message The message displayed.
1036 * @param title The title of the dialog.
1037 * @param messageType The messageType.
1038 * @param icon The icon displayed.
1039 * @param selectionValues The list of values to select from.
1040 * @param initialSelectionValue The initially selected value.
1042 * @return The user selected value.
1044 public static Object showInputDialog(Component parentComponent,
1045 Object message, String title,
1046 int messageType, Icon icon,
1047 Object[] selectionValues,
1048 Object initialSelectionValue)
1050 JOptionPane pane = new JOptionPane(message, messageType);
1051 pane.setWantsInput(true);
1053 pane.setSelectionValues(selectionValues);
1054 pane.setInitialSelectionValue(initialSelectionValue);
1055 JDialog dialog = pane.createDialog(parentComponent, title);
1058 return pane.getInputValue();
1062 * This method shows a QUESTION_MESSAGE type input dialog. Since no
1063 * selectionValues is set, the Look and Feel will usually give the user a
1064 * TextField to input data to. This method returns the value the user
1067 * @param message The message to display.
1069 * @return The user selected value.
1071 public static String showInputDialog(Object message)
1073 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1074 pane.setWantsInput(true);
1075 JDialog dialog = pane.createDialog(null, null);
1078 return (String) pane.getInputValue();
1082 * This method shows a QUESTION_MESSAGE type input dialog. Since no
1083 * selectionValues is set, the Look and Feel will usually give the user a
1084 * TextField to input data to. The input component will be initialized with
1085 * the initialSelectionValue. This method returns the value the user
1088 * @param message The message to display.
1089 * @param initialSelectionValue The initialSelectionValue.
1091 * @return The user selected value.
1093 public static String showInputDialog(Object message,
1094 Object initialSelectionValue)
1096 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1097 pane.setWantsInput(true);
1098 pane.setInitialSelectionValue(initialSelectionValue);
1099 JDialog dialog = pane.createDialog(null, null);
1102 return (String) pane.getInputValue();
1106 * This method shows an internal confirmation dialog with the given message.
1107 * The internal frame dialog will be placed in the first JDesktopPane
1108 * ancestor of the given parentComponent. This method will return the value
1111 * @param parentComponent The parent to find a JDesktopPane in.
1112 * @param message The message to display.
1114 * @return The value selected.
1116 public static int showInternalConfirmDialog(Component parentComponent,
1119 JOptionPane pane = new JOptionPane(message);
1120 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1124 if (pane.getValue() instanceof Integer)
1125 return ((Integer) pane.getValue()).intValue();
1130 * This method shows an internal confirmation dialog with the given message,
1131 * optionType and title. The internal frame dialog will be placed in the
1132 * first JDesktopPane ancestor of the given parentComponent. This method
1133 * will return the selected value.
1135 * @param parentComponent The parent to find a JDesktopPane in.
1136 * @param message The message to display.
1137 * @param title The title to display.
1138 * @param optionType The option type.
1140 * @return The selected value.
1142 public static int showInternalConfirmDialog(Component parentComponent,
1143 Object message, String title,
1146 JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
1147 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1151 if (pane.getValue() instanceof Integer)
1152 return ((Integer) pane.getValue()).intValue();
1157 * This method shows an internal confirmation dialog with the given message,
1158 * title, optionTypes and icon for the given message type. The internal
1159 * confirmation dialog will be placed in the first instance of
1160 * JDesktopPane ancestor of the given parentComponent.
1162 * @param parentComponent The component to find a JDesktopPane in.
1163 * @param message The message to display.
1164 * @param title The title of the dialog.
1165 * @param optionType The option type.
1166 * @param messageType The message type.
1168 * @return The selected value.
1170 public static int showInternalConfirmDialog(Component parentComponent,
1171 Object message, String title,
1172 int optionType, int messageType)
1174 JOptionPane pane = new JOptionPane(message, messageType, optionType);
1175 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1179 if (pane.getValue() instanceof Integer)
1180 return ((Integer) pane.getValue()).intValue();
1185 * This method shows an internal confirmation dialog with the given message,
1186 * title, option type, message type, and icon. The internal frame dialog
1187 * will be placed in the first JDesktopPane ancestor that is found in the
1188 * given parentComponent. This method returns the selected value.
1190 * @param parentComponent The parent to find a JDesktopPane in.
1191 * @param message The message to display.
1192 * @param title The title to display.
1193 * @param optionType The option type.
1194 * @param messageType The message type.
1195 * @param icon The icon to display.
1197 * @return The selected value.
1199 public static int showInternalConfirmDialog(Component parentComponent,
1200 Object message, String title,
1201 int optionType, int messageType,
1204 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
1205 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1209 if (pane.getValue() instanceof Integer)
1210 return ((Integer) pane.getValue()).intValue();
1215 * This method shows an internal input dialog with the given message. The
1216 * internal frame dialog will be placed in the first JDesktopPane ancestor
1217 * of the given parent component. This method returns the value input by
1220 * @param parentComponent The parent to find a JDesktopPane in.
1221 * @param message The message to display.
1223 * @return The user selected value.
1225 public static String showInternalInputDialog(Component parentComponent,
1228 JOptionPane pane = new JOptionPane(message);
1229 pane.setWantsInput(true);
1230 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1234 return (String) pane.getInputValue();
1238 * This method shows an internal input dialog with the given message, title
1239 * and message type. The internal input dialog will be placed in the first
1240 * JDesktopPane ancestor found in the given parent component. This method
1241 * will return the input value given by the user.
1243 * @param parentComponent The component to find a JDesktopPane in.
1244 * @param message The message to display.
1245 * @param title The title to display.
1246 * @param messageType The message type.
1248 * @return The user input value.
1250 public static String showInternalInputDialog(Component parentComponent,
1251 Object message, String title,
1254 JOptionPane pane = new JOptionPane(message, messageType);
1255 pane.setWantsInput(true);
1256 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1260 return (String) pane.getInputValue();
1264 * This method shows an internal input dialog with the given message, title
1265 * message type, icon, selection value list and initial selection value.
1266 * The internal frame dialog will be placed in the first JDesktopPane
1267 * ancestor found in the given parent component. This method returns the
1268 * input value from the user.
1270 * @param parentComponent The parent to find a JDesktopPane in.
1271 * @param message The message to display.
1272 * @param title The title to display.
1273 * @param messageType The message type.
1274 * @param icon The icon to display.
1275 * @param selectionValues The selection value list.
1276 * @param initialSelectionValue The initial selection value.
1278 * @return The user input value.
1280 public static Object showInternalInputDialog(Component parentComponent,
1281 Object message, String title,
1282 int messageType, Icon icon,
1283 Object[] selectionValues,
1284 Object initialSelectionValue)
1286 JOptionPane pane = new JOptionPane(message, messageType);
1287 pane.setWantsInput(true);
1289 pane.setSelectionValues(selectionValues);
1290 pane.setInitialSelectionValue(initialSelectionValue);
1291 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1295 return pane.getInputValue();
1299 * This method shows an internal message dialog with the given message. The
1300 * internal frame dialog will be placed in the first JDesktopPane ancestor
1301 * found in the given parent component.
1303 * @param parentComponent The component to find a JDesktopPane in.
1304 * @param message The message to display.
1306 public static void showInternalMessageDialog(Component parentComponent,
1309 JOptionPane pane = new JOptionPane(message);
1310 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1316 * This method shows an internal message dialog with the given message,
1317 * title and message type. The internal message dialog is placed in the
1318 * first JDesktopPane ancestor found in the given parent component.
1320 * @param parentComponent The parent component to find a JDesktopPane in.
1321 * @param message The message to display.
1322 * @param title The title to display.
1323 * @param messageType The message type.
1325 public static void showInternalMessageDialog(Component parentComponent,
1326 Object message, String title,
1329 JOptionPane pane = new JOptionPane(message, messageType);
1330 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1336 * This method shows an internal message dialog with the given message,
1337 * title, message type and icon. The internal message dialog is placed in
1338 * the first JDesktopPane ancestor found in the given parent component.
1340 * @param parentComponent The component to find a JDesktopPane in.
1341 * @param message The message to display.
1342 * @param title The title to display.
1343 * @param messageType The message type.
1344 * @param icon The icon to display.
1346 public static void showInternalMessageDialog(Component parentComponent,
1347 Object message, String title,
1348 int messageType, Icon icon)
1350 JOptionPane pane = new JOptionPane(message, messageType);
1352 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1358 * This method displays an internal option dialog with the given message,
1359 * title, option type, message type, icon, option list, and initial option
1360 * value. The internal option dialog is placed in the first JDesktopPane
1361 * ancestor found in the parent component. This method returns the option
1364 * @param parentComponent The parent to find a JDesktopPane in.
1365 * @param message The message displayed.
1366 * @param title The title displayed.
1367 * @param optionType The option type.
1368 * @param messageType The message type.
1369 * @param icon The icon to display.
1370 * @param options The array of options.
1371 * @param initialValue The initial value selected.
1373 * @return The option that was selected.
1375 public static int showInternalOptionDialog(Component parentComponent,
1376 Object message, String title,
1377 int optionType, int messageType,
1378 Icon icon, Object[] options,
1379 Object initialValue)
1381 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1382 options, initialValue);
1384 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1388 if (pane.getValue() instanceof Integer)
1389 return ((Integer) pane.getValue()).intValue();
1394 * This method shows an INFORMATION_MESSAGE type message dialog.
1396 * @param parentComponent The component to find a frame in.
1397 * @param message The message displayed.
1399 public static void showMessageDialog(Component parentComponent,
1402 JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE);
1403 JDialog dialog = pane.createDialog(parentComponent, null);
1408 * This method shows a message dialog with the given message, title and
1411 * @param parentComponent The component to find a frame in.
1412 * @param message The message displayed.
1413 * @param title The title of the dialog.
1414 * @param messageType The messageType.
1416 public static void showMessageDialog(Component parentComponent,
1417 Object message, String title,
1420 JOptionPane pane = new JOptionPane(message, messageType);
1421 JDialog dialog = pane.createDialog(parentComponent, title);
1426 * This method shows a message dialog with the given message, title,
1427 * messageType and icon.
1429 * @param parentComponent The component to find a frame in.
1430 * @param message The message displayed.
1431 * @param title The title of the dialog.
1432 * @param messageType The messageType.
1433 * @param icon The icon displayed.
1435 public static void showMessageDialog(Component parentComponent,
1436 Object message, String title,
1437 int messageType, Icon icon)
1439 JOptionPane pane = new JOptionPane(message, messageType);
1441 JDialog dialog = pane.createDialog(parentComponent, title);
1446 * This method shows an option dialog with the given message, title,
1447 * optionType, messageType, icon, options and initialValue. This method
1448 * returns the option that was selected.
1450 * @param parentComponent The component to find a frame in.
1451 * @param message The message displayed.
1452 * @param title The title of the dialog.
1453 * @param optionType The optionType.
1454 * @param messageType The messageType.
1455 * @param icon The icon displayed.
1456 * @param options The options to choose from.
1457 * @param initialValue The initial value.
1459 * @return The selected option.
1461 public static int showOptionDialog(Component parentComponent,
1462 Object message, String title,
1463 int optionType, int messageType,
1464 Icon icon, Object[] options,
1465 Object initialValue)
1467 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1468 options, initialValue);
1470 JDialog dialog = pane.createDialog(parentComponent, title);
1473 if (pane.getValue() instanceof Integer)
1474 return ((Integer) pane.getValue()).intValue();
1479 * This method resets the UI to the Look and Feel default.
1481 public void updateUI()
1483 setUI((OptionPaneUI) UIManager.getUI(this));
1487 * This method returns true if the key is a valid messageType.
1489 * @param key The key to check.
1491 * @return True if key is valid.
1493 private boolean validMessageType(int key)
1498 case INFORMATION_MESSAGE:
1500 case QUESTION_MESSAGE:
1501 case WARNING_MESSAGE:
1508 * This method returns true if the key is a valid optionType.
1510 * @param key The key to check.
1512 * @return True if key is valid.
1514 private boolean validOptionType(int key)
1518 case DEFAULT_OPTION:
1519 case OK_CANCEL_OPTION:
1520 case YES_NO_CANCEL_OPTION:
1528 * This helper method makes the JInternalFrame wait until it is notified by
1529 * an InternalFrameClosing event. This method also adds the given
1530 * JOptionPane to the JInternalFrame and sizes it according to the
1531 * JInternalFrame's preferred size.
1533 * @param f The JInternalFrame to make modal.
1535 private static void startModal(JInternalFrame f)
1537 // We need to add an additional glasspane-like component directly
1538 // below the frame, which intercepts all mouse events that are not
1539 // directed at the frame itself.
1540 JPanel modalInterceptor = new JPanel();
1541 modalInterceptor.setOpaque(false);
1542 JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
1543 lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
1544 modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
1545 modalInterceptor.addMouseListener(new MouseAdapter(){});
1546 modalInterceptor.addMouseMotionListener(new MouseMotionAdapter(){});
1547 lp.add(modalInterceptor);
1550 // We need to explicitly dispatch events when we are blocking the event
1552 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
1555 while (! f.isClosed())
1557 if (EventQueue.isDispatchThread())
1559 // The getNextEventMethod() issues wait() when no
1560 // event is available, so we don't need do explicitly wait().
1561 AWTEvent ev = queue.getNextEvent();
1562 // This mimics EventQueue.dispatchEvent(). We can't use
1563 // EventQueue.dispatchEvent() directly, because it is
1564 // protected, unfortunately.
1565 if (ev instanceof ActiveEvent)
1566 ((ActiveEvent) ev).dispatch();
1567 else if (ev.getSource() instanceof Component)
1568 ((Component) ev.getSource()).dispatchEvent(ev);
1569 else if (ev.getSource() instanceof MenuComponent)
1570 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1571 // Other events are ignored as per spec in
1572 // EventQueue.dispatchEvent
1576 // Give other threads a chance to become active.
1581 catch (InterruptedException ex)
1583 // If we get interrupted, then leave the modal state.
1587 // Clean up the modal interceptor.
1588 lp.remove(modalInterceptor);
1590 // Remove the internal frame from its parent, so it is no longer
1591 // lurking around and clogging memory.
1592 Container parent = f.getParent();