2 Copyright (C) 2004 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.Component;
42 import java.awt.Dimension;
43 import java.awt.Frame;
45 import javax.accessibility.Accessible;
46 import javax.accessibility.AccessibleContext;
47 import javax.accessibility.AccessibleRole;
48 import javax.swing.event.InternalFrameAdapter;
49 import javax.swing.event.InternalFrameEvent;
50 import javax.swing.plaf.OptionPaneUI;
53 * This class creates different types of JDialogs and JInternalFrames that can
54 * ask users for input or pass on information. JOptionPane can be used by
55 * calling one of the show static methods or by creating an instance of
56 * JOptionPane and calling createDialog or createInternalFrame.
58 public class JOptionPane extends JComponent implements Accessible
63 protected class AccessibleJOptionPane extends JComponent.AccessibleJComponent
66 private static final long serialVersionUID = 686071432213084821L;
69 * Creates a new AccessibleJOptionPane object.
71 protected AccessibleJOptionPane()
78 * @return DOCUMENT ME!
80 public AccessibleRole getAccessibleRole()
87 private static final long serialVersionUID = 5231143276678566796L;
89 /** The value returned when cancel option is selected. */
90 public static final int CANCEL_OPTION = 2;
92 /** The value returned when the dialog is closed without a selection. */
93 public static final int CLOSED_OPTION = -1;
95 /** An option used in confirmation dialog methods. */
96 public static final int DEFAULT_OPTION = -1;
98 /** The value returned when the no option is selected. */
99 public static final int NO_OPTION = 1;
101 /** An option used in confirmation dialog methods. */
102 public static final int OK_CANCEL_OPTION = 2;
104 /** The value returned when the ok option is selected. */
105 public static final int OK_OPTION = 0;
107 /** An option used in confirmation dialog methods. */
108 public static final int YES_NO_CANCEL_OPTION = 1;
110 /** An option used in confirmation dialog methods. */
111 public static final int YES_NO_OPTION = 0;
113 /** The value returned when the yes option is selected. */
114 public static final int YES_OPTION = 0;
116 /** Identifier for the error message type. */
117 public static final int ERROR_MESSAGE = 0;
119 /** Identifier for the information message type. */
120 public static final int INFORMATION_MESSAGE = 1;
122 /** Identifier for the plain message type. */
123 public static final int PLAIN_MESSAGE = -1;
125 /** Identifier for the question message type. */
126 public static final int QUESTION_MESSAGE = 3;
128 /** Identifier for the warning message type. */
129 public static final int WARNING_MESSAGE = 2;
132 * The identifier for the propertyChangeEvent when the icon property
135 public static final String ICON_PROPERTY = "icon";
138 * The identifier for the propertyChangeEvent when the initialSelectionValue
141 public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
144 * The identifier for the propertyChangeEvent when the initialValue property
147 public static final String INITIAL_VALUE_PROPERTY = "initialValue";
150 * The identifier for the propertyChangeEvent when the inputValue property
153 public static final String INPUT_VALUE_PROPERTY = "inputValue";
156 * The identifier for the propertyChangeEvent when the message property
159 public static final String MESSAGE_PROPERTY = "message";
162 * The identifier for the propertyChangeEvent when the messageType property
165 public static final String MESSAGE_TYPE_PROPERTY = "messageType";
168 * The identifier for the propertyChangeEvent when the optionType property
171 public static final String OPTION_TYPE_PROPERTY = "optionType";
174 * The identifier for the propertyChangeEvent when the options property
177 public static final String OPTIONS_PROPERTY = "options";
180 * The identifier for the propertyChangeEvent when the selectionValues
183 public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
186 * The identifier for the propertyChangeEvent when the value property
189 public static final String VALUE_PROPERTY = "value";
192 * The identifier for the propertyChangeEvent when the wantsInput property
195 public static final String WANTS_INPUT_PROPERTY = "wantsInput";
197 /** The value returned when the inputValue is uninitialized. */
198 public static Object UNINITIALIZED_VALUE = "uninitializedValue";
200 /** The icon displayed in the dialog/internal frame. */
203 /** The initial selected value in the input component. */
204 protected Object initialSelectionValue;
206 /** The object that is initially selected for options. */
207 protected Object initialValue;
209 /** The value the user inputs. */
210 protected Object inputValue = UNINITIALIZED_VALUE;
212 /** The message displayed in the dialog/internal frame. */
213 protected Object message;
215 /** The type of message displayed. */
216 protected int messageType = PLAIN_MESSAGE;
219 * The options (usually buttons) aligned at the bottom for the user to
222 protected Object[] options;
224 /** The type of options to display. */
225 protected int optionType = DEFAULT_OPTION;
227 /** The input values the user can select. */
228 protected Object[] selectionValues;
230 /** The value returned by selecting an option. */
231 protected Object value = UNINITIALIZED_VALUE;
233 /** Whether the Dialog/InternalFrame needs input. */
234 protected boolean wantsInput;
236 /** The common frame used when no parent is provided. */
237 private static Frame privFrame = SwingUtilities.getOwnerFrame();
240 * Creates a new JOptionPane object using a message of "JOptionPane
241 * message", using the PLAIN_MESSAGE type and DEFAULT_OPTION.
245 this("JOptionPane message", PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
249 * Creates a new JOptionPane object using the given message using the
250 * PLAIN_MESSAGE type and DEFAULT_OPTION.
252 * @param message The message to display.
254 public JOptionPane(Object message)
256 this(message, PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
260 * Creates a new JOptionPane object using the given message and messageType
261 * and DEFAULT_OPTION.
263 * @param message The message to display.
264 * @param messageType The type of message.
266 public JOptionPane(Object message, int messageType)
268 this(message, messageType, DEFAULT_OPTION, null, null, null);
272 * Creates a new JOptionPane object using the given message, messageType and
275 * @param message The message to display.
276 * @param messageType The type of message.
277 * @param optionType The type of options.
279 public JOptionPane(Object message, int messageType, int optionType)
281 this(message, messageType, optionType, null, null, null);
285 * Creates a new JOptionPane object using the given message, messageType,
286 * optionType and icon.
288 * @param message The message to display.
289 * @param messageType The type of message.
290 * @param optionType The type of options.
291 * @param icon The icon to display.
293 public JOptionPane(Object message, int messageType, int optionType, Icon icon)
295 this(message, messageType, optionType, icon, null, null);
299 * Creates a new JOptionPane object using the given message, messageType,
300 * optionType, icon and options.
302 * @param message The message to display.
303 * @param messageType The type of message.
304 * @param optionType The type of options.
305 * @param icon The icon to display.
306 * @param options The options given.
308 public JOptionPane(Object message, int messageType, int optionType,
309 Icon icon, Object[] options)
311 this(message, messageType, optionType, icon, options, null);
315 * Creates a new JOptionPane object using the given message, messageType,
316 * optionType, icon, options and initialValue. The initialValue will be
319 * @param message The message to display.
320 * @param messageType The type of message.
321 * @param optionType The type of options.
322 * @param icon The icon to display.
323 * @param options The options given.
324 * @param initialValue The component to focus on initially.
326 * @throws IllegalArgumentException If the messageType or optionType are not
329 public JOptionPane(Object message, int messageType, int optionType,
330 Icon icon, Object[] options, Object initialValue)
332 this.message = message;
333 if (! validMessageType(messageType))
334 throw new IllegalArgumentException("Message Type not legal value.");
335 this.messageType = messageType;
336 if (! validOptionType(optionType))
337 throw new IllegalArgumentException("Option Type not legal value.");
338 this.optionType = optionType;
340 this.options = options;
341 this.initialValue = initialValue;
343 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
351 * This method creates a new JDialog that is either centered around the
352 * parent's frame or centered on the screen (if the parent is null). The
353 * JDialog will not be resizable and will be modal. Once the JDialog is
354 * disposed, the inputValue and value properties will be set by the
357 * @param parentComponent The parent of the Dialog.
358 * @param title The title in the bar of the JDialog.
360 * @return A new JDialog based on the JOptionPane configuration.
362 public JDialog createDialog(Component parentComponent, String title)
364 Frame toUse = getFrameForComponent(parentComponent);
366 toUse = getRootFrame();
368 JDialog dialog = new JDialog(toUse, title);
369 inputValue = UNINITIALIZED_VALUE;
370 value = UNINITIALIZED_VALUE;
372 // FIXME: This dialog should be centered on the parent
373 // or at the center of the screen (if the parent is null)
374 // Need getGraphicsConfiguration to return non-null in
375 // order for that to work so we know how large the
377 dialog.getContentPane().add(this);
378 dialog.setModal(true);
379 dialog.setResizable(false);
387 * This method creates a new JInternalFrame that is in the JLayeredPane
388 * which contains the parentComponent given. If no suitable JLayeredPane
389 * can be found from the parentComponent given, a RuntimeException will be
392 * @param parentComponent The parent to find a JDesktopPane from.
393 * @param title The title of the JInternalFrame.
395 * @return A new JInternalFrame based on the JOptionPane configuration.
397 * @throws RuntimeException If no suitable JDesktopPane is found.
399 * @specnote The specification says that the internal frame is placed
400 * in the nearest <code>JDesktopPane</code> that is found in
401 * <code>parent</code>'s ancestors. The behaviour of the JDK
402 * is that it actually looks up the nearest
403 * <code>JLayeredPane</code> in <code>parent</code>'s ancestors.
406 public JInternalFrame createInternalFrame(Component parentComponent,
408 throws RuntimeException
410 // Try to find a JDesktopPane.
411 JLayeredPane toUse = getDesktopPaneForComponent(parentComponent);
412 // If we don't have a JDesktopPane, we try to find a JLayeredPane.
414 toUse = JLayeredPane.getLayeredPaneAbove(parentComponent);
415 // If this still fails, we throw a RuntimeException.
417 throw new RuntimeException
418 ("parentComponent does not have a valid parent");
420 JInternalFrame frame = new JInternalFrame(title);
422 inputValue = UNINITIALIZED_VALUE;
423 value = UNINITIALIZED_VALUE;
425 frame.setContentPane(this);
426 frame.setClosable(true);
429 frame.setLayer(JLayeredPane.MODAL_LAYER);
432 frame.setVisible(true);
440 * @return DOCUMENT ME!
442 public AccessibleContext getAccessibleContext()
444 if (accessibleContext == null)
445 accessibleContext = new AccessibleJOptionPane();
446 return accessibleContext;
450 * This method returns the JDesktopPane for the given parentComponent or
451 * null if none can be found.
453 * @param parentComponent The component to look in.
455 * @return The JDesktopPane for the given component or null if none can be
458 public static JDesktopPane getDesktopPaneForComponent(Component parentComponent)
460 return (JDesktopPane) SwingUtilities.getAncestorOfClass(JDesktopPane.class,
465 * This method returns the Frame for the given parentComponent or null if
468 * @param parentComponent The component to look in.
470 * @return The Frame for the given component or null if none can be found.
472 public static Frame getFrameForComponent(Component parentComponent)
474 return (Frame) SwingUtilities.getAncestorOfClass(Frame.class,
479 * This method returns the icon displayed.
481 * @return The icon displayed.
483 public Icon getIcon()
489 * This method returns the value initially selected from the list of values
490 * the user can input.
492 * @return The initial selection value.
494 public Object getInitialSelectionValue()
496 return initialSelectionValue;
500 * This method returns the value that is focused from the list of options.
502 * @return The initial value from options.
504 public Object getInitialValue()
510 * This method returns the value that the user input.
512 * @return The user's input value.
514 public Object getInputValue()
520 * This method returns the maximum characters per line. By default, this is
523 * @return The maximum characters per line.
525 public int getMaxCharactersPerLineCount()
527 return Integer.MAX_VALUE;
531 * This method returns the message displayed.
533 * @return The message displayed.
535 public Object getMessage()
541 * This method returns the message type.
543 * @return The message type.
545 public int getMessageType()
551 * This method returns the options.
553 * @return The options.
555 public Object[] getOptions()
561 * This method returns the option type.
563 * @return The option type.
565 public int getOptionType()
571 * This method returns the Frame used by JOptionPane dialog's that have no
574 * @return The Frame used by dialogs that have no parent.
576 public static Frame getRootFrame()
582 * This method returns the selection values.
584 * @return The selection values.
586 public Object[] getSelectionValues()
588 return selectionValues;
592 * This method returns the UI used by the JOptionPane.
594 * @return The UI used by the JOptionPane.
596 public OptionPaneUI getUI()
598 return (OptionPaneUI) ui;
602 * This method returns an identifier to determine which UI class will act as
605 * @return The UI identifier.
607 public String getUIClassID()
609 return "OptionPaneUI";
613 * This method returns the value that the user selected out of options.
615 * @return The value that the user selected out of options.
617 public Object getValue()
623 * This method returns whether this JOptionPane wants input.
625 * @return Whether this JOptionPane wants input.
627 public boolean getWantsInput()
633 * This method returns a String that describes this JOptionPane.
635 * @return A String that describes this JOptionPane.
637 protected String paramString()
639 return "JOptionPane";
643 * This method requests focus for the initial value.
645 public void selectInitialValue()
648 ((OptionPaneUI) ui).selectInitialValue(this);
652 * This method changes the icon property.
654 * @param newIcon The new icon to use.
656 public void setIcon(Icon newIcon)
662 firePropertyChange(ICON_PROPERTY, old, icon);
667 * This method changes the initial selection property.
669 * @param newValue The new initial selection.
671 public void setInitialSelectionValue(Object newValue)
673 if (initialSelectionValue != newValue)
675 Object old = initialSelectionValue;
676 initialSelectionValue = newValue;
677 firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, old,
678 initialSelectionValue);
683 * This method changes the initial value property.
685 * @param newValue The new initial value.
687 public void setInitialValue(Object newValue)
689 if (initialValue != newValue)
691 Object old = initialValue;
692 initialValue = newValue;
693 firePropertyChange(INITIAL_VALUE_PROPERTY, old, initialValue);
698 * This method changes the inputValue property.
700 * @param newValue The new inputValue.
702 public void setInputValue(Object newValue)
704 if (inputValue != newValue)
706 Object old = inputValue;
707 inputValue = newValue;
708 firePropertyChange(INPUT_VALUE_PROPERTY, old, inputValue);
713 * This method changes the message property.
715 * @param newMessage The new message.
717 public void setMessage(Object newMessage)
719 if (message != newMessage)
721 Object old = message;
722 message = newMessage;
723 firePropertyChange(MESSAGE_PROPERTY, old, message);
728 * This method changes the messageType property.
730 * @param newType The new messageType.
732 * @throws IllegalArgumentException If the messageType is not valid.
734 public void setMessageType(int newType)
736 if (! validMessageType(newType))
737 throw new IllegalArgumentException("Message Type not legal value.");
738 if (newType != messageType)
740 int old = messageType;
741 messageType = newType;
742 firePropertyChange(MESSAGE_TYPE_PROPERTY, old, messageType);
747 * This method changes the options property.
749 * @param newOptions The new options.
751 public void setOptions(Object[] newOptions)
753 if (options != newOptions)
755 Object[] old = options;
756 options = newOptions;
757 firePropertyChange(OPTIONS_PROPERTY, old, options);
762 * This method changes the optionType property.
764 * @param newType The new optionType.
766 * @throws IllegalArgumentException If the optionType is not valid.
768 public void setOptionType(int newType)
770 if (! validOptionType(newType))
771 throw new IllegalArgumentException("Option Type not legal value.");
772 if (newType != optionType)
774 int old = optionType;
775 optionType = newType;
776 firePropertyChange(OPTION_TYPE_PROPERTY, old, optionType);
781 * This method changes the Frame used for JOptionPane dialogs that have no
784 * @param newRootFrame The Frame to use for dialogs that have no parent.
786 public static void setRootFrame(Frame newRootFrame)
788 privFrame = newRootFrame;
792 * This method changes the selectionValues property.
794 * @param newValues The new selectionValues.
796 public void setSelectionValues(Object[] newValues)
798 if (newValues != selectionValues)
800 if (newValues != null)
802 Object[] old = selectionValues;
803 selectionValues = newValues;
804 firePropertyChange(SELECTION_VALUES_PROPERTY, old, selectionValues);
809 * This method sets the UI used with the JOptionPane.
811 * @param ui The UI used with the JOptionPane.
813 public void setUI(OptionPaneUI ui)
819 * This method sets the value has been selected out of options.
821 * @param newValue The value that has been selected out of options.
823 public void setValue(Object newValue)
825 if (value != newValue)
829 firePropertyChange(VALUE_PROPERTY, old, value);
834 * This method changes the wantsInput property.
836 * @param newValue Whether this JOptionPane requires input.
838 public void setWantsInput(boolean newValue)
840 if (wantsInput != newValue)
842 boolean old = wantsInput;
843 wantsInput = newValue;
844 firePropertyChange(WANTS_INPUT_PROPERTY, old, wantsInput);
849 * This method shows a confirmation dialog with the title "Select an Option"
850 * and displays the given message. The parent frame will be the same as the
851 * parent frame of the given parentComponent. This method returns the
852 * option chosen by the user.
854 * @param parentComponent The parentComponent to find a frame in.
855 * @param message The message to display.
857 * @return The option that was selected.
859 public static int showConfirmDialog(Component parentComponent, Object message)
861 JOptionPane pane = new JOptionPane(message);
862 JDialog dialog = pane.createDialog(parentComponent, "Select an Option");
867 return ((Integer) pane.getValue()).intValue();
871 * This method shows a confirmation dialog with the given message,
872 * optionType and title. The frame that owns the dialog will be the same
873 * frame that holds the given parentComponent. This method returns the
874 * option that was chosen.
876 * @param parentComponent The component to find a frame in.
877 * @param message The message displayed.
878 * @param title The title of the dialog.
879 * @param optionType The optionType.
881 * @return The option that was chosen.
883 public static int showConfirmDialog(Component parentComponent,
884 Object message, String title,
887 JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
888 JDialog dialog = pane.createDialog(parentComponent, title);
892 return ((Integer) pane.getValue()).intValue();
896 * This method shows a confirmation dialog with the given message, title,
897 * messageType and optionType. The frame owner will be the same frame as
898 * the one that holds the given parentComponent. This method returns the
899 * option selected by the user.
901 * @param parentComponent The component to find a frame in.
902 * @param message The message displayed.
903 * @param title The title of the dialog.
904 * @param optionType The optionType.
905 * @param messageType The messageType.
907 * @return The selected option.
909 public static int showConfirmDialog(Component parentComponent,
910 Object message, String title,
911 int optionType, int messageType)
913 JOptionPane pane = new JOptionPane(message, messageType, optionType);
914 JDialog dialog = pane.createDialog(parentComponent, title);
918 return ((Integer) pane.getValue()).intValue();
922 * This method shows a confirmation dialog with the given message, title,
923 * optionType, messageType and icon. The frame owner will be the same as
924 * the one that holds the given parentComponent. This method returns the
925 * option selected by the user.
927 * @param parentComponent The component to find a frame in.
928 * @param message The message displayed.
929 * @param title The title of the dialog.
930 * @param optionType The optionType.
931 * @param messageType The messsageType.
932 * @param icon The icon displayed.
934 * @return The selected option.
936 public static int showConfirmDialog(Component parentComponent,
937 Object message, String title,
938 int optionType, int messageType,
941 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
942 JDialog dialog = pane.createDialog(parentComponent, title);
946 return ((Integer) pane.getValue()).intValue();
950 * This method will show a QUESTION_MESSAGE input dialog with the given
951 * message. No selectionValues is set so the Look and Feel will usually
952 * give the user a TextField to fill out. The frame owner will be the same
953 * frame that holds the given parentComponent. This method will return the
954 * value entered by the user.
956 * @param parentComponent The component to find a frame in.
957 * @param message The message displayed.
959 * @return The value entered by the user.
961 public static String showInputDialog(Component parentComponent,
964 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
965 pane.setWantsInput(true);
966 JDialog dialog = pane.createDialog(parentComponent, null);
970 return (String) pane.getInputValue();
974 * This method will show a QUESTION_MESSAGE type input dialog with the given
975 * message and initialSelectionValue. Since there is no selectionValues
976 * set, the Look and Feel will usually give a TextField to fill out. The
977 * frame owner will be the same as the one that holds the given
978 * parentComponent. This method will return the value entered by the user.
980 * @param parentComponent The component to find a frame in.
981 * @param message The message to display.
982 * @param initialSelectionValue The initially selected value.
984 * @return The value the user input.
986 public static String showInputDialog(Component parentComponent,
988 Object initialSelectionValue)
990 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
991 pane.setInitialSelectionValue(initialSelectionValue);
992 pane.setWantsInput(true);
993 JDialog dialog = pane.createDialog(parentComponent, null);
997 return (String) pane.getInputValue();
1001 * This method displays a new input dialog with the given message, title and
1002 * messageType. Since no selectionValues value is given, the Look and Feel
1003 * will usually give the user a TextField to input data to. This method
1004 * returns the value the user inputs.
1006 * @param parentComponent The component to find a frame in.
1007 * @param message The message to display.
1008 * @param title The title of the dialog.
1009 * @param messageType The messageType.
1011 * @return The value the user input.
1013 public static String showInputDialog(Component parentComponent,
1014 Object message, String title,
1017 JOptionPane pane = new JOptionPane(message, messageType);
1018 pane.setWantsInput(true);
1019 JDialog dialog = pane.createDialog(parentComponent, title);
1023 return (String) pane.getInputValue();
1027 * This method shows an input dialog with the given message, title,
1028 * messageType, icon, selectionValues, and initialSelectionValue. This
1029 * method returns the value that the user selects.
1031 * @param parentComponent The component to find a frame in.
1032 * @param message The message displayed.
1033 * @param title The title of the dialog.
1034 * @param messageType The messageType.
1035 * @param icon The icon displayed.
1036 * @param selectionValues The list of values to select from.
1037 * @param initialSelectionValue The initially selected value.
1039 * @return The user selected value.
1041 public static Object showInputDialog(Component parentComponent,
1042 Object message, String title,
1043 int messageType, Icon icon,
1044 Object[] selectionValues,
1045 Object initialSelectionValue)
1047 JOptionPane pane = new JOptionPane(message, messageType);
1048 pane.setWantsInput(true);
1050 pane.setSelectionValues(selectionValues);
1051 pane.setInitialSelectionValue(initialSelectionValue);
1052 JDialog dialog = pane.createDialog(parentComponent, title);
1056 return (String) pane.getInputValue();
1060 * This method shows a QUESTION_MESSAGE type input dialog. Since no
1061 * selectionValues is set, the Look and Feel will usually give the user a
1062 * TextField to input data to. This method returns the value the user
1065 * @param message The message to display.
1067 * @return The user selected value.
1069 public static String showInputDialog(Object message)
1071 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1072 pane.setWantsInput(true);
1073 JDialog dialog = pane.createDialog(null, null);
1077 return (String) pane.getInputValue();
1081 * This method shows a QUESTION_MESSAGE type input dialog. Since no
1082 * selectionValues is set, the Look and Feel will usually give the user a
1083 * TextField to input data to. The input component will be initialized with
1084 * the initialSelectionValue. This method returns the value the user
1087 * @param message The message to display.
1088 * @param initialSelectionValue The initialSelectionValue.
1090 * @return The user selected value.
1092 public static String showInputDialog(Object message,
1093 Object initialSelectionValue)
1095 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1096 pane.setWantsInput(true);
1097 pane.setInitialSelectionValue(initialSelectionValue);
1098 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 return ((Integer) pane.getValue()).intValue();
1128 * This method shows an internal confirmation dialog with the given message,
1129 * optionType and title. The internal frame dialog will be placed in the
1130 * first JDesktopPane ancestor of the given parentComponent. This method
1131 * will return the selected value.
1133 * @param parentComponent The parent to find a JDesktopPane in.
1134 * @param message The message to display.
1135 * @param title The title to display.
1136 * @param optionType The option type.
1138 * @return The selected value.
1140 public static int showInternalConfirmDialog(Component parentComponent,
1141 Object message, String title,
1144 JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
1145 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1149 return ((Integer) pane.getValue()).intValue();
1153 * This method shows an internal confirmation dialog with the given message,
1154 * title, optionTypes and icon for the given message type. The internal
1155 * confirmation dialog will be placed in the first instance of
1156 * JDesktopPane ancestor of the given parentComponent.
1158 * @param parentComponent The component to find a JDesktopPane in.
1159 * @param message The message to display.
1160 * @param title The title of the dialog.
1161 * @param optionType The option type.
1162 * @param messageType The message type.
1164 * @return The selected value.
1166 public static int showInternalConfirmDialog(Component parentComponent,
1167 Object message, String title,
1168 int optionType, int messageType)
1170 JOptionPane pane = new JOptionPane(message, messageType, optionType);
1171 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1175 return ((Integer) pane.getValue()).intValue();
1179 * This method shows an internal confirmation dialog with the given message,
1180 * title, option type, message type, and icon. The internal frame dialog
1181 * will be placed in the first JDesktopPane ancestor that is found in the
1182 * given parentComponent. This method returns the selected value.
1184 * @param parentComponent The parent to find a JDesktopPane in.
1185 * @param message The message to display.
1186 * @param title The title to display.
1187 * @param optionType The option type.
1188 * @param messageType The message type.
1189 * @param icon The icon to display.
1191 * @return The selected value.
1193 public static int showInternalConfirmDialog(Component parentComponent,
1194 Object message, String title,
1195 int optionType, int messageType,
1198 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
1199 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1203 return ((Integer) pane.getValue()).intValue();
1207 * This method shows an internal input dialog with the given message. The
1208 * internal frame dialog will be placed in the first JDesktopPane ancestor
1209 * of the given parent component. This method returns the value input by
1212 * @param parentComponent The parent to find a JDesktopPane in.
1213 * @param message The message to display.
1215 * @return The user selected value.
1217 public static String showInternalInputDialog(Component parentComponent,
1220 JOptionPane pane = new JOptionPane(message);
1221 pane.setWantsInput(true);
1222 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1226 return (String) pane.getInputValue();
1230 * This method shows an internal input dialog with the given message, title
1231 * and message type. The internal input dialog will be placed in the first
1232 * JDesktopPane ancestor found in the given parent component. This method
1233 * will return the input value given by the user.
1235 * @param parentComponent The component to find a JDesktopPane in.
1236 * @param message The message to display.
1237 * @param title The title to display.
1238 * @param messageType The message type.
1240 * @return The user input value.
1242 public static String showInternalInputDialog(Component parentComponent,
1243 Object message, String title,
1246 JOptionPane pane = new JOptionPane(message, messageType);
1247 pane.setWantsInput(true);
1248 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1252 return (String) pane.getInputValue();
1256 * This method shows an internal input dialog with the given message, title
1257 * message type, icon, selection value list and initial selection value.
1258 * The internal frame dialog will be placed in the first JDesktopPane
1259 * ancestor found in the given parent component. This method returns the
1260 * input value from the user.
1262 * @param parentComponent The parent to find a JDesktopPane in.
1263 * @param message The message to display.
1264 * @param title The title to display.
1265 * @param messageType The message type.
1266 * @param icon The icon to display.
1267 * @param selectionValues The selection value list.
1268 * @param initialSelectionValue The initial selection value.
1270 * @return The user input value.
1272 public static Object showInternalInputDialog(Component parentComponent,
1273 Object message, String title,
1274 int messageType, Icon icon,
1275 Object[] selectionValues,
1276 Object initialSelectionValue)
1278 JOptionPane pane = new JOptionPane(message, messageType);
1279 pane.setWantsInput(true);
1281 pane.setSelectionValues(selectionValues);
1282 pane.setInitialSelectionValue(initialSelectionValue);
1283 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1287 return (String) pane.getInputValue();
1291 * This method shows an internal message dialog with the given message. The
1292 * internal frame dialog will be placed in the first JDesktopPane ancestor
1293 * found in the given parent component.
1295 * @param parentComponent The component to find a JDesktopPane in.
1296 * @param message The message to display.
1298 public static void showInternalMessageDialog(Component parentComponent,
1301 JOptionPane pane = new JOptionPane(message);
1302 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1308 * This method shows an internal message dialog with the given message,
1309 * title and message type. The internal message dialog is placed in the
1310 * first JDesktopPane ancestor found in the given parent component.
1312 * @param parentComponent The parent component to find a JDesktopPane in.
1313 * @param message The message to display.
1314 * @param title The title to display.
1315 * @param messageType The message type.
1317 public static void showInternalMessageDialog(Component parentComponent,
1318 Object message, String title,
1321 JOptionPane pane = new JOptionPane(message, messageType);
1322 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1328 * This method shows an internal message dialog with the given message,
1329 * title, message type and icon. The internal message dialog is placed in
1330 * the first JDesktopPane ancestor found in the given parent component.
1332 * @param parentComponent The component to find a JDesktopPane in.
1333 * @param message The message to display.
1334 * @param title The title to display.
1335 * @param messageType The message type.
1336 * @param icon The icon to display.
1338 public static void showInternalMessageDialog(Component parentComponent,
1339 Object message, String title,
1340 int messageType, Icon icon)
1342 JOptionPane pane = new JOptionPane(message, messageType);
1344 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1350 * This method displays an internal option dialog with the given message,
1351 * title, option type, message type, icon, option list, and initial option
1352 * value. The internal option dialog is placed in the first JDesktopPane
1353 * ancestor found in the parent component. This method returns the option
1356 * @param parentComponent The parent to find a JDesktopPane in.
1357 * @param message The message displayed.
1358 * @param title The title displayed.
1359 * @param optionType The option type.
1360 * @param messageType The message type.
1361 * @param icon The icon to display.
1362 * @param options The array of options.
1363 * @param initialValue The initial value selected.
1365 * @return The option that was selected.
1367 public static int showInternalOptionDialog(Component parentComponent,
1368 Object message, String title,
1369 int optionType, int messageType,
1370 Icon icon, Object[] options,
1371 Object initialValue)
1373 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1374 options, initialValue);
1376 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1380 return ((Integer) pane.getValue()).intValue();
1384 * This method shows an INFORMATION_MESSAGE type message dialog.
1386 * @param parentComponent The component to find a frame in.
1387 * @param message The message displayed.
1389 public static void showMessageDialog(Component parentComponent,
1392 JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE);
1393 JDialog dialog = pane.createDialog(parentComponent, null);
1399 * This method shows a message dialog with the given message, title and
1402 * @param parentComponent The component to find a frame in.
1403 * @param message The message displayed.
1404 * @param title The title of the dialog.
1405 * @param messageType The messageType.
1407 public static void showMessageDialog(Component parentComponent,
1408 Object message, String title,
1411 JOptionPane pane = new JOptionPane(message, messageType);
1412 JDialog dialog = pane.createDialog(parentComponent, title);
1418 * This method shows a message dialog with the given message, title,
1419 * messageType and icon.
1421 * @param parentComponent The component to find a frame in.
1422 * @param message The message displayed.
1423 * @param title The title of the dialog.
1424 * @param messageType The messageType.
1425 * @param icon The icon displayed.
1427 public static void showMessageDialog(Component parentComponent,
1428 Object message, String title,
1429 int messageType, Icon icon)
1431 JOptionPane pane = new JOptionPane(message, messageType);
1433 JDialog dialog = pane.createDialog(parentComponent, title);
1439 * This method shows an option dialog with the given message, title,
1440 * optionType, messageType, icon, options and initialValue. This method
1441 * returns the option that was selected.
1443 * @param parentComponent The component to find a frame in.
1444 * @param message The message displayed.
1445 * @param title The title of the dialog.
1446 * @param optionType The optionType.
1447 * @param messageType The messageType.
1448 * @param icon The icon displayed.
1449 * @param options The options to choose from.
1450 * @param initialValue The initial value.
1452 * @return The selected option.
1454 public static int showOptionDialog(Component parentComponent,
1455 Object message, String title,
1456 int optionType, int messageType,
1457 Icon icon, Object[] options,
1458 Object initialValue)
1460 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1461 options, initialValue);
1463 JDialog dialog = pane.createDialog(parentComponent, title);
1467 return ((Integer) pane.getValue()).intValue();
1471 * This method resets the UI to the Look and Feel default.
1473 public void updateUI()
1475 setUI((OptionPaneUI) UIManager.getUI(this));
1480 * This method returns true if the key is a valid messageType.
1482 * @param key The key to check.
1484 * @return True if key is valid.
1486 private boolean validMessageType(int key)
1491 case INFORMATION_MESSAGE:
1493 case QUESTION_MESSAGE:
1494 case WARNING_MESSAGE:
1501 * This method returns true if the key is a valid optionType.
1503 * @param key The key to check.
1505 * @return True if key is valid.
1507 private boolean validOptionType(int key)
1511 case DEFAULT_OPTION:
1512 case OK_CANCEL_OPTION:
1513 case YES_NO_CANCEL_OPTION:
1521 * This helper method makes the JInternalFrame wait until it is notified by
1522 * an InternalFrameClosing event. This method also adds the given
1523 * JOptionPane to the JInternalFrame and sizes it according to the
1524 * JInternalFrame's preferred size.
1526 * @param f The JInternalFrame to make modal.
1527 * @param pane The JOptionPane to add to the JInternalFrame.
1529 private static void startModal(JInternalFrame f)
1533 final JInternalFrame tmp = f;
1536 f.addInternalFrameListener(new InternalFrameAdapter()
1538 public void internalFrameClosed(InternalFrameEvent e)
1542 tmp.removeInternalFrameListener(this);
1549 while (! f.isClosed())
1552 catch (InterruptedException ignored)