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;
51 import java.beans.PropertyChangeEvent;
52 import java.beans.PropertyChangeListener;
54 import javax.accessibility.Accessible;
55 import javax.accessibility.AccessibleContext;
56 import javax.accessibility.AccessibleRole;
57 import javax.swing.plaf.OptionPaneUI;
60 * This class creates different types of JDialogs and JInternalFrames that can
61 * ask users for input or pass on information. JOptionPane can be used by
62 * calling one of the show static methods or by creating an instance of
63 * JOptionPane and calling createDialog or createInternalFrame.
65 public class JOptionPane extends JComponent implements Accessible
68 * Provides the accessibility features for the <code>JOptionPane</code>
71 protected class AccessibleJOptionPane extends JComponent.AccessibleJComponent
73 private static final long serialVersionUID = 686071432213084821L;
76 * Creates a new <code>AccessibleJOptionPane</code> instance.
78 protected AccessibleJOptionPane()
80 // Nothing to do here.
84 * Returns the accessible role of this object, which is always
85 * {@link AccessibleRole#OPTION_PANE}.
87 * @return the accessible role of this object
89 public AccessibleRole getAccessibleRole()
91 return AccessibleRole.OPTION_PANE;
95 private static final long serialVersionUID = 5231143276678566796L;
97 /** The value returned when cancel option is selected. */
98 public static final int CANCEL_OPTION = 2;
100 /** The value returned when the dialog is closed without a selection. */
101 public static final int CLOSED_OPTION = -1;
103 /** An option used in confirmation dialog methods. */
104 public static final int DEFAULT_OPTION = -1;
106 /** The value returned when the no option is selected. */
107 public static final int NO_OPTION = 1;
109 /** An option used in confirmation dialog methods. */
110 public static final int OK_CANCEL_OPTION = 2;
112 /** The value returned when the ok option is selected. */
113 public static final int OK_OPTION = 0;
115 /** An option used in confirmation dialog methods. */
116 public static final int YES_NO_CANCEL_OPTION = 1;
118 /** An option used in confirmation dialog methods. */
119 public static final int YES_NO_OPTION = 0;
121 /** The value returned when the yes option is selected. */
122 public static final int YES_OPTION = 0;
124 /** Identifier for the error message type. */
125 public static final int ERROR_MESSAGE = 0;
127 /** Identifier for the information message type. */
128 public static final int INFORMATION_MESSAGE = 1;
130 /** Identifier for the plain message type. */
131 public static final int PLAIN_MESSAGE = -1;
133 /** Identifier for the question message type. */
134 public static final int QUESTION_MESSAGE = 3;
136 /** Identifier for the warning message type. */
137 public static final int WARNING_MESSAGE = 2;
140 * The identifier for the propertyChangeEvent when the icon property
143 public static final String ICON_PROPERTY = "icon";
146 * The identifier for the propertyChangeEvent when the initialSelectionValue
149 public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
152 * The identifier for the propertyChangeEvent when the initialValue property
155 public static final String INITIAL_VALUE_PROPERTY = "initialValue";
158 * The identifier for the propertyChangeEvent when the inputValue property
161 public static final String INPUT_VALUE_PROPERTY = "inputValue";
164 * The identifier for the propertyChangeEvent when the message property
167 public static final String MESSAGE_PROPERTY = "message";
170 * The identifier for the propertyChangeEvent when the messageType property
173 public static final String MESSAGE_TYPE_PROPERTY = "messageType";
176 * The identifier for the propertyChangeEvent when the optionType property
179 public static final String OPTION_TYPE_PROPERTY = "optionType";
182 * The identifier for the propertyChangeEvent when the options property
185 public static final String OPTIONS_PROPERTY = "options";
188 * The identifier for the propertyChangeEvent when the selectionValues
191 public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
194 * The identifier for the propertyChangeEvent when the value property
197 public static final String VALUE_PROPERTY = "value";
200 * The identifier for the propertyChangeEvent when the wantsInput property
203 public static final String WANTS_INPUT_PROPERTY = "wantsInput";
205 /** The value returned when the inputValue is uninitialized. */
206 public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
208 /** The icon displayed in the dialog/internal frame. */
211 /** The initial selected value in the input component. */
212 protected Object initialSelectionValue;
214 /** The object that is initially selected for options. */
215 protected Object initialValue;
217 /** The value the user inputs. */
218 protected Object inputValue = UNINITIALIZED_VALUE;
220 /** The message displayed in the dialog/internal frame. */
221 protected Object message;
223 /** The type of message displayed. */
224 protected int messageType = PLAIN_MESSAGE;
227 * The options (usually buttons) aligned at the bottom for the user to
230 protected Object[] options;
232 /** The type of options to display. */
233 protected int optionType = DEFAULT_OPTION;
235 /** The input values the user can select. */
236 protected Object[] selectionValues;
238 /** The value returned by selecting an option. */
239 protected Object value = UNINITIALIZED_VALUE;
241 /** Whether the Dialog/InternalFrame needs input. */
242 protected boolean wantsInput;
244 /** The common frame used when no parent is provided. */
245 private static Frame privFrame = (Frame) SwingUtilities.getOwnerFrame(null);
248 * Creates a new JOptionPane object using a message of "JOptionPane
249 * message", using the PLAIN_MESSAGE type and DEFAULT_OPTION.
253 this("JOptionPane message", PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
257 * Creates a new JOptionPane object using the given message using the
258 * PLAIN_MESSAGE type and DEFAULT_OPTION.
260 * @param message The message to display.
262 public JOptionPane(Object message)
264 this(message, PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
268 * Creates a new JOptionPane object using the given message and messageType
269 * and DEFAULT_OPTION.
271 * @param message The message to display.
272 * @param messageType The type of message.
274 public JOptionPane(Object message, int messageType)
276 this(message, messageType, DEFAULT_OPTION, null, null, null);
280 * Creates a new JOptionPane object using the given message, messageType and
283 * @param message The message to display.
284 * @param messageType The type of message.
285 * @param optionType The type of options.
287 public JOptionPane(Object message, int messageType, int optionType)
289 this(message, messageType, optionType, null, null, null);
293 * Creates a new JOptionPane object using the given message, messageType,
294 * optionType and icon.
296 * @param message The message to display.
297 * @param messageType The type of message.
298 * @param optionType The type of options.
299 * @param icon The icon to display.
301 public JOptionPane(Object message, int messageType, int optionType, Icon icon)
303 this(message, messageType, optionType, icon, null, null);
307 * Creates a new JOptionPane object using the given message, messageType,
308 * optionType, icon and options.
310 * @param message The message to display.
311 * @param messageType The type of message.
312 * @param optionType The type of options.
313 * @param icon The icon to display.
314 * @param options The options given.
316 public JOptionPane(Object message, int messageType, int optionType,
317 Icon icon, Object[] options)
319 this(message, messageType, optionType, icon, options, null);
323 * Creates a new JOptionPane object using the given message, messageType,
324 * optionType, icon, options and initialValue. The initialValue will be
327 * @param message The message to display.
328 * @param messageType The type of message.
329 * @param optionType The type of options.
330 * @param icon The icon to display.
331 * @param options The options given.
332 * @param initialValue The component to focus on initially.
334 * @throws IllegalArgumentException If the messageType or optionType are not
337 public JOptionPane(Object message, int messageType, int optionType,
338 Icon icon, Object[] options, Object initialValue)
340 this.message = message;
341 if (! validMessageType(messageType))
342 throw new IllegalArgumentException("Message Type not legal value.");
343 this.messageType = messageType;
344 if (! validOptionType(optionType))
345 throw new IllegalArgumentException("Option Type not legal value.");
346 this.optionType = optionType;
348 this.options = options;
349 this.initialValue = initialValue;
351 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
357 * This method creates a new JDialog that is either centered around the
358 * parent's frame or centered on the screen (if the parent is null). The
359 * JDialog will not be resizable and will be modal. Once the JDialog is
360 * disposed, the inputValue and value properties will be set by the
363 * @param parentComponent The parent of the Dialog.
364 * @param title The title in the bar of the JDialog.
366 * @return A new JDialog based on the JOptionPane configuration.
368 public JDialog createDialog(Component parentComponent, String title)
370 Frame toUse = getFrameForComponent(parentComponent);
372 toUse = getRootFrame();
374 JDialog dialog = new JDialog(toUse, title);
375 inputValue = UNINITIALIZED_VALUE;
376 value = UNINITIALIZED_VALUE;
378 dialog.getContentPane().add(this);
379 dialog.setModal(true);
380 dialog.setResizable(false);
382 dialog.setLocationRelativeTo(parentComponent);
384 addPropertyChangeListener(new ValuePropertyHandler(dialog));
389 * Handles changes of the value property. Whenever this property changes,
390 * the JOptionPane dialog should be closed.
392 private static class ValuePropertyHandler
393 implements PropertyChangeListener
396 * The dialog to close.
401 * Creates a new instance.
403 * @param d the dialog to be closed
405 ValuePropertyHandler(JDialog d)
411 * Receives notification when any of the properties change.
413 public void propertyChange(PropertyChangeEvent p)
415 String prop = p.getPropertyName();
416 Object val = p.getNewValue();
417 if (prop.equals(VALUE_PROPERTY) && val != null
418 && val != UNINITIALIZED_VALUE)
420 dialog.setVisible(false);
426 * This method creates a new JInternalFrame that is in the JLayeredPane
427 * which contains the parentComponent given. If no suitable JLayeredPane
428 * can be found from the parentComponent given, a RuntimeException will be
431 * @param parentComponent The parent to find a JDesktopPane from.
432 * @param title The title of the JInternalFrame.
434 * @return A new JInternalFrame based on the JOptionPane configuration.
436 * @throws RuntimeException If no suitable JDesktopPane is found.
438 * @specnote The specification says that the internal frame is placed
439 * in the nearest <code>JDesktopPane</code> that is found in
440 * <code>parent</code>'s ancestors. The behaviour of the JDK
441 * is that it actually looks up the nearest
442 * <code>JLayeredPane</code> in <code>parent</code>'s ancestors.
445 public JInternalFrame createInternalFrame(Component parentComponent,
447 throws RuntimeException
449 // Try to find a JDesktopPane.
450 JLayeredPane toUse = getDesktopPaneForComponent(parentComponent);
451 // If we don't have a JDesktopPane, we try to find a JLayeredPane.
453 toUse = JLayeredPane.getLayeredPaneAbove(parentComponent);
454 // If this still fails, we throw a RuntimeException.
456 throw new RuntimeException
457 ("parentComponent does not have a valid parent");
459 JInternalFrame frame = new JInternalFrame(title);
461 inputValue = UNINITIALIZED_VALUE;
462 value = UNINITIALIZED_VALUE;
464 frame.setContentPane(this);
465 frame.setClosable(true);
468 frame.setLayer(JLayeredPane.MODAL_LAYER);
471 frame.setVisible(true);
477 * Returns the object that provides accessibility features for this
478 * <code>JOptionPane</code> component.
480 * @return The accessible context (an instance of
481 * {@link AccessibleJOptionPane}).
483 public AccessibleContext getAccessibleContext()
485 if (accessibleContext == null)
486 accessibleContext = new AccessibleJOptionPane();
487 return accessibleContext;
491 * This method returns the JDesktopPane for the given parentComponent or
492 * null if none can be found.
494 * @param parentComponent The component to look in.
496 * @return The JDesktopPane for the given component or null if none can be
499 public static JDesktopPane getDesktopPaneForComponent(Component parentComponent)
501 return (JDesktopPane) SwingUtilities.getAncestorOfClass(JDesktopPane.class,
506 * This method returns the Frame for the given parentComponent or null if
509 * @param parentComponent The component to look in.
511 * @return The Frame for the given component or null if none can be found.
513 public static Frame getFrameForComponent(Component parentComponent)
515 return (Frame) SwingUtilities.getAncestorOfClass(Frame.class,
520 * This method returns the icon displayed.
522 * @return The icon displayed.
524 public Icon getIcon()
530 * This method returns the value initially selected from the list of values
531 * the user can input.
533 * @return The initial selection value.
535 public Object getInitialSelectionValue()
537 return initialSelectionValue;
541 * This method returns the value that is focused from the list of options.
543 * @return The initial value from options.
545 public Object getInitialValue()
551 * This method returns the value that the user input.
553 * @return The user's input value.
555 public Object getInputValue()
557 if (getValue().equals(new Integer(CANCEL_OPTION)))
563 * This method returns the maximum characters per line. By default, this is
566 * @return The maximum characters per line.
568 public int getMaxCharactersPerLineCount()
570 return Integer.MAX_VALUE;
574 * This method returns the message displayed.
576 * @return The message displayed.
578 public Object getMessage()
584 * This method returns the message type.
586 * @return The message type.
588 public int getMessageType()
594 * This method returns the options.
596 * @return The options.
598 public Object[] getOptions()
604 * This method returns the option type.
606 * @return The option type.
608 public int getOptionType()
614 * This method returns the Frame used by JOptionPane dialog's that have no
617 * @return The Frame used by dialogs that have no parent.
619 public static Frame getRootFrame()
625 * This method returns the selection values.
627 * @return The selection values.
629 public Object[] getSelectionValues()
631 return selectionValues;
635 * This method returns the UI used by the JOptionPane.
637 * @return The UI used by the JOptionPane.
639 public OptionPaneUI getUI()
641 return (OptionPaneUI) ui;
645 * This method returns an identifier to determine which UI class will act as
648 * @return The UI identifier.
650 public String getUIClassID()
652 return "OptionPaneUI";
656 * This method returns the value that the user selected out of options.
658 * @return The value that the user selected out of options.
660 public Object getValue()
666 * This method returns whether this JOptionPane wants input.
668 * @return Whether this JOptionPane wants input.
670 public boolean getWantsInput()
676 * This method returns a String that describes this JOptionPane.
678 * @return A String that describes this JOptionPane.
680 protected String paramString()
682 return "JOptionPane";
686 * This method requests focus for the initial value.
688 public void selectInitialValue()
691 ((OptionPaneUI) ui).selectInitialValue(this);
695 * This method changes the icon property.
697 * @param newIcon The new icon to use.
699 public void setIcon(Icon newIcon)
705 firePropertyChange(ICON_PROPERTY, old, icon);
710 * This method changes the initial selection property.
712 * @param newValue The new initial selection.
714 public void setInitialSelectionValue(Object newValue)
716 if (initialSelectionValue != newValue)
718 Object old = initialSelectionValue;
719 initialSelectionValue = newValue;
720 firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, old,
721 initialSelectionValue);
726 * This method changes the initial value property.
728 * @param newValue The new initial value.
730 public void setInitialValue(Object newValue)
732 if (initialValue != newValue)
734 Object old = initialValue;
735 initialValue = newValue;
736 firePropertyChange(INITIAL_VALUE_PROPERTY, old, initialValue);
741 * This method changes the inputValue property.
743 * @param newValue The new inputValue.
745 public void setInputValue(Object newValue)
747 if (inputValue != newValue)
749 Object old = inputValue;
750 inputValue = newValue;
751 firePropertyChange(INPUT_VALUE_PROPERTY, old, inputValue);
756 * This method changes the message property.
758 * @param newMessage The new message.
760 public void setMessage(Object newMessage)
762 if (message != newMessage)
764 Object old = message;
765 message = newMessage;
766 firePropertyChange(MESSAGE_PROPERTY, old, message);
771 * This method changes the messageType property.
773 * @param newType The new messageType.
775 * @throws IllegalArgumentException If the messageType is not valid.
777 public void setMessageType(int newType)
779 if (! validMessageType(newType))
780 throw new IllegalArgumentException("Message Type not legal value.");
781 if (newType != messageType)
783 int old = messageType;
784 messageType = newType;
785 firePropertyChange(MESSAGE_TYPE_PROPERTY, old, messageType);
790 * This method changes the options property.
792 * @param newOptions The new options.
794 public void setOptions(Object[] newOptions)
796 if (options != newOptions)
798 Object[] old = options;
799 options = newOptions;
800 firePropertyChange(OPTIONS_PROPERTY, old, options);
805 * This method changes the optionType property.
807 * @param newType The new optionType.
809 * @throws IllegalArgumentException If the optionType is not valid.
811 public void setOptionType(int newType)
813 if (! validOptionType(newType))
814 throw new IllegalArgumentException("Option Type not legal value.");
815 if (newType != optionType)
817 int old = optionType;
818 optionType = newType;
819 firePropertyChange(OPTION_TYPE_PROPERTY, old, optionType);
824 * This method changes the Frame used for JOptionPane dialogs that have no
827 * @param newRootFrame The Frame to use for dialogs that have no parent.
829 public static void setRootFrame(Frame newRootFrame)
831 privFrame = newRootFrame;
835 * This method changes the selectionValues property.
837 * @param newValues The new selectionValues.
839 public void setSelectionValues(Object[] newValues)
841 if (newValues != selectionValues)
843 if (newValues != null)
845 Object[] old = selectionValues;
846 selectionValues = newValues;
847 firePropertyChange(SELECTION_VALUES_PROPERTY, old, selectionValues);
852 * This method sets the UI used with the JOptionPane.
854 * @param ui The UI used with the JOptionPane.
856 public void setUI(OptionPaneUI ui)
862 * This method sets the value has been selected out of options.
864 * @param newValue The value that has been selected out of options.
866 public void setValue(Object newValue)
868 if (value != newValue)
872 firePropertyChange(VALUE_PROPERTY, old, value);
877 * This method changes the wantsInput property.
879 * @param newValue Whether this JOptionPane requires input.
881 public void setWantsInput(boolean newValue)
883 if (wantsInput != newValue)
885 boolean old = wantsInput;
886 wantsInput = newValue;
887 firePropertyChange(WANTS_INPUT_PROPERTY, old, wantsInput);
892 * This method shows a confirmation dialog with the title "Select an Option"
893 * and displays the given message. The parent frame will be the same as the
894 * parent frame of the given parentComponent. This method returns the
895 * option chosen by the user.
897 * @param parentComponent The parentComponent to find a frame in.
898 * @param message The message to display.
900 * @return The option that was selected.
902 public static int showConfirmDialog(Component parentComponent, Object message)
904 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
905 JDialog dialog = pane.createDialog(parentComponent, "Select an Option");
908 if (pane.getValue() instanceof Integer)
909 return ((Integer) pane.getValue()).intValue();
914 * This method shows a confirmation dialog with the given message,
915 * optionType and title. The frame that owns the dialog will be the same
916 * frame that holds the given parentComponent. This method returns the
917 * option that was chosen.
919 * @param parentComponent The component to find a frame in.
920 * @param message The message displayed.
921 * @param title The title of the dialog.
922 * @param optionType The optionType.
924 * @return The option that was chosen.
926 public static int showConfirmDialog(Component parentComponent,
927 Object message, String title,
930 JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
931 JDialog dialog = pane.createDialog(parentComponent, title);
934 if (pane.getValue() instanceof Integer)
935 return ((Integer) pane.getValue()).intValue();
940 * This method shows a confirmation dialog with the given message, title,
941 * messageType and optionType. The frame owner will be the same frame as
942 * the one that holds the given parentComponent. This method returns the
943 * option selected by the user.
945 * @param parentComponent The component to find a frame in.
946 * @param message The message displayed.
947 * @param title The title of the dialog.
948 * @param optionType The optionType.
949 * @param messageType The messageType.
951 * @return The selected option.
953 public static int showConfirmDialog(Component parentComponent,
954 Object message, String title,
955 int optionType, int messageType)
957 JOptionPane pane = new JOptionPane(message, messageType, optionType);
958 JDialog dialog = pane.createDialog(parentComponent, title);
961 if (pane.getValue() instanceof Integer)
962 return ((Integer) pane.getValue()).intValue();
967 * This method shows a confirmation dialog with the given message, title,
968 * optionType, messageType and icon. The frame owner will be the same as
969 * the one that holds the given parentComponent. This method returns the
970 * option selected by the user.
972 * @param parentComponent The component to find a frame in.
973 * @param message The message displayed.
974 * @param title The title of the dialog.
975 * @param optionType The optionType.
976 * @param messageType The messsageType.
977 * @param icon The icon displayed.
979 * @return The selected option.
981 public static int showConfirmDialog(Component parentComponent,
982 Object message, String title,
983 int optionType, int messageType,
986 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
987 JDialog dialog = pane.createDialog(parentComponent, title);
990 if (pane.getValue() instanceof Integer)
991 return ((Integer) pane.getValue()).intValue();
996 * This method will show a QUESTION_MESSAGE input dialog with the given
997 * message. No selectionValues is set so the Look and Feel will usually
998 * give the user a TextField to fill out. The frame owner will be the same
999 * frame that holds the given parentComponent. This method will return the
1000 * value entered by the user.
1002 * @param parentComponent The component to find a frame in.
1003 * @param message The message displayed.
1005 * @return The value entered by the user.
1007 public static String showInputDialog(Component parentComponent,
1010 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1011 pane.setWantsInput(true);
1012 JDialog dialog = pane.createDialog(parentComponent, null);
1015 return (String) pane.getInputValue();
1019 * This method will show a QUESTION_MESSAGE type input dialog with the given
1020 * message and initialSelectionValue. Since there is no selectionValues
1021 * set, the Look and Feel will usually give a TextField to fill out. The
1022 * frame owner will be the same as the one that holds the given
1023 * parentComponent. This method will return the value entered by the user.
1025 * @param parentComponent The component to find a frame in.
1026 * @param message The message to display.
1027 * @param initialSelectionValue The initially selected value.
1029 * @return The value the user input.
1031 public static String showInputDialog(Component parentComponent,
1033 Object initialSelectionValue)
1035 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1036 pane.setInitialSelectionValue(initialSelectionValue);
1037 pane.setWantsInput(true);
1038 JDialog dialog = pane.createDialog(parentComponent, null);
1041 return (String) pane.getInputValue();
1045 * This method displays a new input dialog with the given message, title and
1046 * messageType. Since no selectionValues value is given, the Look and Feel
1047 * will usually give the user a TextField to input data to. This method
1048 * returns the value the user inputs.
1050 * @param parentComponent The component to find a frame in.
1051 * @param message The message to display.
1052 * @param title The title of the dialog.
1053 * @param messageType The messageType.
1055 * @return The value the user input.
1057 public static String showInputDialog(Component parentComponent,
1058 Object message, String title,
1061 JOptionPane pane = new JOptionPane(message, messageType);
1062 pane.setWantsInput(true);
1063 JDialog dialog = pane.createDialog(parentComponent, title);
1066 return (String) pane.getInputValue();
1070 * This method shows an input dialog with the given message, title,
1071 * messageType, icon, selectionValues, and initialSelectionValue. This
1072 * method returns the value that the user selects.
1074 * @param parentComponent The component to find a frame in.
1075 * @param message The message displayed.
1076 * @param title The title of the dialog.
1077 * @param messageType The messageType.
1078 * @param icon The icon displayed.
1079 * @param selectionValues The list of values to select from.
1080 * @param initialSelectionValue The initially selected value.
1082 * @return The user selected value.
1084 public static Object showInputDialog(Component parentComponent,
1085 Object message, String title,
1086 int messageType, Icon icon,
1087 Object[] selectionValues,
1088 Object initialSelectionValue)
1090 JOptionPane pane = new JOptionPane(message, messageType);
1091 pane.setWantsInput(true);
1093 pane.setSelectionValues(selectionValues);
1094 pane.setInitialSelectionValue(initialSelectionValue);
1095 JDialog dialog = pane.createDialog(parentComponent, title);
1098 return pane.getInputValue();
1102 * This method shows a QUESTION_MESSAGE type input dialog. Since no
1103 * selectionValues is set, the Look and Feel will usually give the user a
1104 * TextField to input data to. This method returns the value the user
1107 * @param message The message to display.
1109 * @return The user selected value.
1111 public static String showInputDialog(Object message)
1113 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1114 pane.setWantsInput(true);
1115 JDialog dialog = pane.createDialog(null, null);
1118 return (String) pane.getInputValue();
1122 * This method shows a QUESTION_MESSAGE type input dialog. Since no
1123 * selectionValues is set, the Look and Feel will usually give the user a
1124 * TextField to input data to. The input component will be initialized with
1125 * the initialSelectionValue. This method returns the value the user
1128 * @param message The message to display.
1129 * @param initialSelectionValue The initialSelectionValue.
1131 * @return The user selected value.
1133 public static String showInputDialog(Object message,
1134 Object initialSelectionValue)
1136 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1137 pane.setWantsInput(true);
1138 pane.setInitialSelectionValue(initialSelectionValue);
1139 JDialog dialog = pane.createDialog(null, null);
1142 return (String) pane.getInputValue();
1146 * This method shows an internal confirmation dialog with the given message.
1147 * The internal frame dialog will be placed in the first JDesktopPane
1148 * ancestor of the given parentComponent. This method will return the value
1151 * @param parentComponent The parent to find a JDesktopPane in.
1152 * @param message The message to display.
1154 * @return The value selected.
1156 public static int showInternalConfirmDialog(Component parentComponent,
1159 JOptionPane pane = new JOptionPane(message);
1160 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1164 if (pane.getValue() instanceof Integer)
1165 return ((Integer) pane.getValue()).intValue();
1170 * This method shows an internal confirmation dialog with the given message,
1171 * optionType and title. The internal frame dialog will be placed in the
1172 * first JDesktopPane ancestor of the given parentComponent. This method
1173 * will return the selected value.
1175 * @param parentComponent The parent to find a JDesktopPane in.
1176 * @param message The message to display.
1177 * @param title The title to display.
1178 * @param optionType The option type.
1180 * @return The selected value.
1182 public static int showInternalConfirmDialog(Component parentComponent,
1183 Object message, String title,
1186 JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
1187 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1191 if (pane.getValue() instanceof Integer)
1192 return ((Integer) pane.getValue()).intValue();
1197 * This method shows an internal confirmation dialog with the given message,
1198 * title, optionTypes and icon for the given message type. The internal
1199 * confirmation dialog will be placed in the first instance of
1200 * JDesktopPane ancestor of the given parentComponent.
1202 * @param parentComponent The component to find a JDesktopPane in.
1203 * @param message The message to display.
1204 * @param title The title of the dialog.
1205 * @param optionType The option type.
1206 * @param messageType The message type.
1208 * @return The selected value.
1210 public static int showInternalConfirmDialog(Component parentComponent,
1211 Object message, String title,
1212 int optionType, int messageType)
1214 JOptionPane pane = new JOptionPane(message, messageType, optionType);
1215 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1219 if (pane.getValue() instanceof Integer)
1220 return ((Integer) pane.getValue()).intValue();
1225 * This method shows an internal confirmation dialog with the given message,
1226 * title, option type, message type, and icon. The internal frame dialog
1227 * will be placed in the first JDesktopPane ancestor that is found in the
1228 * given parentComponent. This method returns the selected value.
1230 * @param parentComponent The parent to find a JDesktopPane in.
1231 * @param message The message to display.
1232 * @param title The title to display.
1233 * @param optionType The option type.
1234 * @param messageType The message type.
1235 * @param icon The icon to display.
1237 * @return The selected value.
1239 public static int showInternalConfirmDialog(Component parentComponent,
1240 Object message, String title,
1241 int optionType, int messageType,
1244 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
1245 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1249 if (pane.getValue() instanceof Integer)
1250 return ((Integer) pane.getValue()).intValue();
1255 * This method shows an internal input dialog with the given message. The
1256 * internal frame dialog will be placed in the first JDesktopPane ancestor
1257 * of the given parent component. This method returns the value input by
1260 * @param parentComponent The parent to find a JDesktopPane in.
1261 * @param message The message to display.
1263 * @return The user selected value.
1265 public static String showInternalInputDialog(Component parentComponent,
1268 JOptionPane pane = new JOptionPane(message);
1269 pane.setWantsInput(true);
1270 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1274 return (String) pane.getInputValue();
1278 * This method shows an internal input dialog with the given message, title
1279 * and message type. The internal input dialog will be placed in the first
1280 * JDesktopPane ancestor found in the given parent component. This method
1281 * will return the input value given by the user.
1283 * @param parentComponent The component to find a JDesktopPane in.
1284 * @param message The message to display.
1285 * @param title The title to display.
1286 * @param messageType The message type.
1288 * @return The user input value.
1290 public static String showInternalInputDialog(Component parentComponent,
1291 Object message, String title,
1294 JOptionPane pane = new JOptionPane(message, messageType);
1295 pane.setWantsInput(true);
1296 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1300 return (String) pane.getInputValue();
1304 * This method shows an internal input dialog with the given message, title
1305 * message type, icon, selection value list and initial selection value.
1306 * The internal frame dialog will be placed in the first JDesktopPane
1307 * ancestor found in the given parent component. This method returns the
1308 * input value from the user.
1310 * @param parentComponent The parent to find a JDesktopPane in.
1311 * @param message The message to display.
1312 * @param title The title to display.
1313 * @param messageType The message type.
1314 * @param icon The icon to display.
1315 * @param selectionValues The selection value list.
1316 * @param initialSelectionValue The initial selection value.
1318 * @return The user input value.
1320 public static Object showInternalInputDialog(Component parentComponent,
1321 Object message, String title,
1322 int messageType, Icon icon,
1323 Object[] selectionValues,
1324 Object initialSelectionValue)
1326 JOptionPane pane = new JOptionPane(message, messageType);
1327 pane.setWantsInput(true);
1329 pane.setSelectionValues(selectionValues);
1330 pane.setInitialSelectionValue(initialSelectionValue);
1331 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1335 return pane.getInputValue();
1339 * This method shows an internal message dialog with the given message. The
1340 * internal frame dialog will be placed in the first JDesktopPane ancestor
1341 * found in the given parent component.
1343 * @param parentComponent The component to find a JDesktopPane in.
1344 * @param message The message to display.
1346 public static void showInternalMessageDialog(Component parentComponent,
1349 JOptionPane pane = new JOptionPane(message);
1350 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1356 * This method shows an internal message dialog with the given message,
1357 * title and message type. The internal message dialog is placed in the
1358 * first JDesktopPane ancestor found in the given parent component.
1360 * @param parentComponent The parent component to find a JDesktopPane in.
1361 * @param message The message to display.
1362 * @param title The title to display.
1363 * @param messageType The message type.
1365 public static void showInternalMessageDialog(Component parentComponent,
1366 Object message, String title,
1369 JOptionPane pane = new JOptionPane(message, messageType);
1370 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1376 * This method shows an internal message dialog with the given message,
1377 * title, message type and icon. The internal message dialog is placed in
1378 * the first JDesktopPane ancestor found in the given parent component.
1380 * @param parentComponent The component to find a JDesktopPane in.
1381 * @param message The message to display.
1382 * @param title The title to display.
1383 * @param messageType The message type.
1384 * @param icon The icon to display.
1386 public static void showInternalMessageDialog(Component parentComponent,
1387 Object message, String title,
1388 int messageType, Icon icon)
1390 JOptionPane pane = new JOptionPane(message, messageType);
1392 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1398 * This method displays an internal option dialog with the given message,
1399 * title, option type, message type, icon, option list, and initial option
1400 * value. The internal option dialog is placed in the first JDesktopPane
1401 * ancestor found in the parent component. This method returns the option
1404 * @param parentComponent The parent to find a JDesktopPane in.
1405 * @param message The message displayed.
1406 * @param title The title displayed.
1407 * @param optionType The option type.
1408 * @param messageType The message type.
1409 * @param icon The icon to display.
1410 * @param options The array of options.
1411 * @param initialValue The initial value selected.
1413 * @return The option that was selected.
1415 public static int showInternalOptionDialog(Component parentComponent,
1416 Object message, String title,
1417 int optionType, int messageType,
1418 Icon icon, Object[] options,
1419 Object initialValue)
1421 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1422 options, initialValue);
1424 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1428 if (pane.getValue() instanceof Integer)
1429 return ((Integer) pane.getValue()).intValue();
1434 * This method shows an INFORMATION_MESSAGE type message dialog.
1436 * @param parentComponent The component to find a frame in.
1437 * @param message The message displayed.
1439 public static void showMessageDialog(Component parentComponent,
1442 JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE);
1443 JDialog dialog = pane.createDialog(parentComponent, null);
1448 * This method shows a message dialog with the given message, title and
1451 * @param parentComponent The component to find a frame in.
1452 * @param message The message displayed.
1453 * @param title The title of the dialog.
1454 * @param messageType The messageType.
1456 public static void showMessageDialog(Component parentComponent,
1457 Object message, String title,
1460 JOptionPane pane = new JOptionPane(message, messageType);
1461 JDialog dialog = pane.createDialog(parentComponent, title);
1466 * This method shows a message dialog with the given message, title,
1467 * messageType and icon.
1469 * @param parentComponent The component to find a frame in.
1470 * @param message The message displayed.
1471 * @param title The title of the dialog.
1472 * @param messageType The messageType.
1473 * @param icon The icon displayed.
1475 public static void showMessageDialog(Component parentComponent,
1476 Object message, String title,
1477 int messageType, Icon icon)
1479 JOptionPane pane = new JOptionPane(message, messageType);
1481 JDialog dialog = pane.createDialog(parentComponent, title);
1486 * This method shows an option dialog with the given message, title,
1487 * optionType, messageType, icon, options and initialValue. This method
1488 * returns the option that was selected.
1490 * @param parentComponent The component to find a frame in.
1491 * @param message The message displayed.
1492 * @param title The title of the dialog.
1493 * @param optionType The optionType.
1494 * @param messageType The messageType.
1495 * @param icon The icon displayed.
1496 * @param options The options to choose from.
1497 * @param initialValue The initial value.
1499 * @return The selected option.
1501 public static int showOptionDialog(Component parentComponent,
1502 Object message, String title,
1503 int optionType, int messageType,
1504 Icon icon, Object[] options,
1505 Object initialValue)
1507 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1508 options, initialValue);
1510 JDialog dialog = pane.createDialog(parentComponent, title);
1513 if (pane.getValue() instanceof Integer)
1514 return ((Integer) pane.getValue()).intValue();
1519 * This method resets the UI to the Look and Feel default.
1521 public void updateUI()
1523 setUI((OptionPaneUI) UIManager.getUI(this));
1527 * This method returns true if the key is a valid messageType.
1529 * @param key The key to check.
1531 * @return True if key is valid.
1533 private boolean validMessageType(int key)
1538 case INFORMATION_MESSAGE:
1540 case QUESTION_MESSAGE:
1541 case WARNING_MESSAGE:
1548 * This method returns true if the key is a valid optionType.
1550 * @param key The key to check.
1552 * @return True if key is valid.
1554 private boolean validOptionType(int key)
1558 case DEFAULT_OPTION:
1559 case OK_CANCEL_OPTION:
1560 case YES_NO_CANCEL_OPTION:
1568 * This helper method makes the JInternalFrame wait until it is notified by
1569 * an InternalFrameClosing event. This method also adds the given
1570 * JOptionPane to the JInternalFrame and sizes it according to the
1571 * JInternalFrame's preferred size.
1573 * @param f The JInternalFrame to make modal.
1575 private static void startModal(JInternalFrame f)
1577 // We need to add an additional glasspane-like component directly
1578 // below the frame, which intercepts all mouse events that are not
1579 // directed at the frame itself.
1580 JPanel modalInterceptor = new JPanel();
1581 modalInterceptor.setOpaque(false);
1582 JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f);
1583 lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue());
1584 modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight());
1585 modalInterceptor.addMouseListener(new MouseAdapter(){});
1586 modalInterceptor.addMouseMotionListener(new MouseMotionAdapter(){});
1587 lp.add(modalInterceptor);
1590 // We need to explicitly dispatch events when we are blocking the event
1592 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
1595 while (! f.isClosed())
1597 if (EventQueue.isDispatchThread())
1599 // The getNextEventMethod() issues wait() when no
1600 // event is available, so we don't need do explicitly wait().
1601 AWTEvent ev = queue.getNextEvent();
1602 // This mimics EventQueue.dispatchEvent(). We can't use
1603 // EventQueue.dispatchEvent() directly, because it is
1604 // protected, unfortunately.
1605 if (ev instanceof ActiveEvent)
1606 ((ActiveEvent) ev).dispatch();
1607 else if (ev.getSource() instanceof Component)
1608 ((Component) ev.getSource()).dispatchEvent(ev);
1609 else if (ev.getSource() instanceof MenuComponent)
1610 ((MenuComponent) ev.getSource()).dispatchEvent(ev);
1611 // Other events are ignored as per spec in
1612 // EventQueue.dispatchEvent
1616 // Give other threads a chance to become active.
1621 catch (InterruptedException ex)
1623 // If we get interrupted, then leave the modal state.
1627 // Clean up the modal interceptor.
1628 lp.remove(modalInterceptor);
1630 // Remove the internal frame from its parent, so it is no longer
1631 // lurking around and clogging memory.
1632 Container parent = f.getParent();