Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / JOptionPane.java
blob0a00729b5cce37f7b001df67ef02f862ec9f19a0
1 /* JOptionPane.java
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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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
24 combination.
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. */
39 package javax.swing;
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;
52 /**
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
60 /**
61 * DOCUMENT ME!
63 protected class AccessibleJOptionPane extends JComponent.AccessibleJComponent
65 /** DOCUMENT ME! */
66 private static final long serialVersionUID = 686071432213084821L;
68 /**
69 * Creates a new AccessibleJOptionPane object.
71 protected AccessibleJOptionPane()
75 /**
76 * DOCUMENT ME!
78 * @return DOCUMENT ME!
80 public AccessibleRole getAccessibleRole()
82 return null;
86 /** DOCUMENT ME! */
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
133 * changes.
135 public static final String ICON_PROPERTY = "icon";
138 * The identifier for the propertyChangeEvent when the initialSelectionValue
139 * property changes.
141 public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
144 * The identifier for the propertyChangeEvent when the initialValue property
145 * changes.
147 public static final String INITIAL_VALUE_PROPERTY = "initialValue";
150 * The identifier for the propertyChangeEvent when the inputValue property
151 * changes.
153 public static final String INPUT_VALUE_PROPERTY = "inputValue";
156 * The identifier for the propertyChangeEvent when the message property
157 * changes.
159 public static final String MESSAGE_PROPERTY = "message";
162 * The identifier for the propertyChangeEvent when the messageType property
163 * changes.
165 public static final String MESSAGE_TYPE_PROPERTY = "messageType";
168 * The identifier for the propertyChangeEvent when the optionType property
169 * changes.
171 public static final String OPTION_TYPE_PROPERTY = "optionType";
174 * The identifier for the propertyChangeEvent when the options property
175 * changes.
177 public static final String OPTIONS_PROPERTY = "options";
180 * The identifier for the propertyChangeEvent when the selectionValues
181 * property changes.
183 public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
186 * The identifier for the propertyChangeEvent when the value property
187 * changes.
189 public static final String VALUE_PROPERTY = "value";
192 * The identifier for the propertyChangeEvent when the wantsInput property
193 * changes.
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. */
201 protected Icon icon;
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
220 * select.
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.
243 public JOptionPane()
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
273 * optionType.
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
317 * focused initially.
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
327 * legal values.
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;
339 this.icon = icon;
340 this.options = options;
341 this.initialValue = initialValue;
343 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
345 updateUI();
346 invalidate();
347 repaint();
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
355 * optionPane.
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);
365 if (toUse == null)
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
376 // screen is.
377 dialog.getContentPane().add(this);
378 dialog.setModal(true);
379 dialog.setResizable(false);
380 dialog.invalidate();
381 dialog.repaint();
383 return dialog;
387 * This method creates a new JInternalFrame that is in the JDesktopPane
388 * which contains the parentComponent given. If no suitable JDesktopPane
389 * can be found from the parentComponent given, a RuntimeException will be
390 * thrown.
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 public JInternalFrame createInternalFrame(Component parentComponent,
400 String title)
401 throws RuntimeException
403 JDesktopPane toUse = getDesktopPaneForComponent(parentComponent);
404 if (toUse == null)
405 throw new RuntimeException("parentComponent does not have a valid parent");
407 JInternalFrame frame = new JInternalFrame(title);
409 inputValue = UNINITIALIZED_VALUE;
410 value = UNINITIALIZED_VALUE;
412 frame.setClosable(true);
413 toUse.add(frame);
415 // FIXME: JLayeredPane broken? See bug # 16576
416 // frame.setLayer(JLayeredPane.MODAL_LAYER);
417 return frame;
421 * DOCUMENT ME!
423 * @return DOCUMENT ME!
425 public AccessibleContext getAccessibleContext()
427 if (accessibleContext == null)
428 accessibleContext = new AccessibleJOptionPane();
429 return accessibleContext;
433 * This method returns the JDesktopPane for the given parentComponent or
434 * null if none can be found.
436 * @param parentComponent The component to look in.
438 * @return The JDesktopPane for the given component or null if none can be
439 * found.
441 public static JDesktopPane getDesktopPaneForComponent(Component parentComponent)
443 return (JDesktopPane) SwingUtilities.getAncestorOfClass(JDesktopPane.class,
444 parentComponent);
448 * This method returns the Frame for the given parentComponent or null if
449 * none can be found.
451 * @param parentComponent The component to look in.
453 * @return The Frame for the given component or null if none can be found.
455 public static Frame getFrameForComponent(Component parentComponent)
457 return (Frame) SwingUtilities.getAncestorOfClass(Frame.class,
458 parentComponent);
462 * This method returns the icon displayed.
464 * @return The icon displayed.
466 public Icon getIcon()
468 return icon;
472 * This method returns the value initially selected from the list of values
473 * the user can input.
475 * @return The initial selection value.
477 public Object getInitialSelectionValue()
479 return initialSelectionValue;
483 * This method returns the value that is focused from the list of options.
485 * @return The initial value from options.
487 public Object getInitialValue()
489 return initialValue;
493 * This method returns the value that the user input.
495 * @return The user's input value.
497 public Object getInputValue()
499 return inputValue;
503 * This method returns the maximum characters per line. By default, this is
504 * Integer.MAX_VALUE.
506 * @return The maximum characters per line.
508 public int getMaxCharactersPerLineCount()
510 return Integer.MAX_VALUE;
514 * This method returns the message displayed.
516 * @return The message displayed.
518 public Object getMessage()
520 return message;
524 * This method returns the message type.
526 * @return The message type.
528 public int getMessageType()
530 return messageType;
534 * This method returns the options.
536 * @return The options.
538 public Object[] getOptions()
540 return options;
544 * This method returns the option type.
546 * @return The option type.
548 public int getOptionType()
550 return optionType;
554 * This method returns the Frame used by JOptionPane dialog's that have no
555 * parent.
557 * @return The Frame used by dialogs that have no parent.
559 public static Frame getRootFrame()
561 return privFrame;
565 * This method returns the selection values.
567 * @return The selection values.
569 public Object[] getSelectionValues()
571 return selectionValues;
575 * This method returns the UI used by the JOptionPane.
577 * @return The UI used by the JOptionPane.
579 public OptionPaneUI getUI()
581 return (OptionPaneUI) ui;
585 * This method returns an identifier to determine which UI class will act as
586 * the UI.
588 * @return The UI identifier.
590 public String getUIClassID()
592 return "OptionPaneUI";
596 * This method returns the value that the user selected out of options.
598 * @return The value that the user selected out of options.
600 public Object getValue()
602 return value;
606 * This method returns whether this JOptionPane wants input.
608 * @return Whether this JOptionPane wants input.
610 public boolean getWantsInput()
612 return wantsInput;
616 * This method returns a String that describes this JOptionPane.
618 * @return A String that describes this JOptionPane.
620 protected String paramString()
622 return "JOptionPane";
626 * This method requests focus for the initial value.
628 public void selectInitialValue()
630 if (ui != null)
631 ((OptionPaneUI) ui).selectInitialValue(this);
635 * This method changes the icon property.
637 * @param newIcon The new icon to use.
639 public void setIcon(Icon newIcon)
641 if (icon != newIcon)
643 Icon old = icon;
644 icon = newIcon;
645 firePropertyChange(ICON_PROPERTY, old, icon);
650 * This method changes the initial selection property.
652 * @param newValue The new initial selection.
654 public void setInitialSelectionValue(Object newValue)
656 if (initialSelectionValue != newValue)
658 Object old = initialSelectionValue;
659 initialSelectionValue = newValue;
660 firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, old,
661 initialSelectionValue);
666 * This method changes the initial value property.
668 * @param newValue The new initial value.
670 public void setInitialValue(Object newValue)
672 if (initialValue != newValue)
674 Object old = initialValue;
675 initialValue = newValue;
676 firePropertyChange(INITIAL_VALUE_PROPERTY, old, initialValue);
681 * This method changes the inputValue property.
683 * @param newValue The new inputValue.
685 public void setInputValue(Object newValue)
687 if (inputValue != newValue)
689 Object old = inputValue;
690 inputValue = newValue;
691 firePropertyChange(INPUT_VALUE_PROPERTY, old, inputValue);
696 * This method changes the message property.
698 * @param newMessage The new message.
700 public void setMessage(Object newMessage)
702 if (message != newMessage)
704 Object old = message;
705 message = newMessage;
706 firePropertyChange(MESSAGE_PROPERTY, old, message);
711 * This method changes the messageType property.
713 * @param newType The new messageType.
715 * @throws IllegalArgumentException If the messageType is not valid.
717 public void setMessageType(int newType)
719 if (! validMessageType(newType))
720 throw new IllegalArgumentException("Message Type not legal value.");
721 if (newType != messageType)
723 int old = messageType;
724 messageType = newType;
725 firePropertyChange(MESSAGE_TYPE_PROPERTY, old, messageType);
730 * This method changes the options property.
732 * @param newOptions The new options.
734 public void setOptions(Object[] newOptions)
736 if (options != newOptions)
738 Object[] old = options;
739 options = newOptions;
740 firePropertyChange(OPTIONS_PROPERTY, old, options);
745 * This method changes the optionType property.
747 * @param newType The new optionType.
749 * @throws IllegalArgumentException If the optionType is not valid.
751 public void setOptionType(int newType)
753 if (! validOptionType(newType))
754 throw new IllegalArgumentException("Option Type not legal value.");
755 if (newType != optionType)
757 int old = optionType;
758 optionType = newType;
759 firePropertyChange(OPTION_TYPE_PROPERTY, old, optionType);
764 * This method changes the Frame used for JOptionPane dialogs that have no
765 * parent.
767 * @param newRootFrame The Frame to use for dialogs that have no parent.
769 public static void setRootFrame(Frame newRootFrame)
771 privFrame = newRootFrame;
775 * This method changes the selectionValues property.
777 * @param newValues The new selectionValues.
779 public void setSelectionValues(Object[] newValues)
781 if (newValues != selectionValues)
783 if (newValues != null)
784 wantsInput = true;
785 Object[] old = selectionValues;
786 selectionValues = newValues;
787 firePropertyChange(SELECTION_VALUES_PROPERTY, old, selectionValues);
792 * This method sets the UI used with the JOptionPane.
794 * @param ui The UI used with the JOptionPane.
796 public void setUI(OptionPaneUI ui)
798 super.setUI(ui);
802 * This method sets the value has been selected out of options.
804 * @param newValue The value that has been selected out of options.
806 public void setValue(Object newValue)
808 if (value != newValue)
810 Object old = value;
811 value = newValue;
812 firePropertyChange(VALUE_PROPERTY, old, value);
817 * This method changes the wantsInput property.
819 * @param newValue Whether this JOptionPane requires input.
821 public void setWantsInput(boolean newValue)
823 if (wantsInput != newValue)
825 boolean old = wantsInput;
826 wantsInput = newValue;
827 firePropertyChange(WANTS_INPUT_PROPERTY, old, wantsInput);
832 * This method shows a confirmation dialog with the title "Select an Option"
833 * and displays the given message. The parent frame will be the same as the
834 * parent frame of the given parentComponent. This method returns the
835 * option chosen by the user.
837 * @param parentComponent The parentComponent to find a frame in.
838 * @param message The message to display.
840 * @return The option that was selected.
842 public static int showConfirmDialog(Component parentComponent, Object message)
844 JOptionPane pane = new JOptionPane(message);
845 JDialog dialog = pane.createDialog(parentComponent, "Select an Option");
847 dialog.pack();
848 dialog.show();
850 return ((Integer) pane.getValue()).intValue();
854 * This method shows a confirmation dialog with the given message,
855 * optionType and title. The frame that owns the dialog will be the same
856 * frame that holds the given parentComponent. This method returns the
857 * option that was chosen.
859 * @param parentComponent The component to find a frame in.
860 * @param message The message displayed.
861 * @param title The title of the dialog.
862 * @param optionType The optionType.
864 * @return The option that was chosen.
866 public static int showConfirmDialog(Component parentComponent,
867 Object message, String title,
868 int optionType)
870 JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
871 JDialog dialog = pane.createDialog(parentComponent, title);
872 dialog.pack();
873 dialog.show();
875 return ((Integer) pane.getValue()).intValue();
879 * This method shows a confirmation dialog with the given message, title,
880 * messageType and optionType. The frame owner will be the same frame as
881 * the one that holds the given parentComponent. This method returns the
882 * option selected by the user.
884 * @param parentComponent The component to find a frame in.
885 * @param message The message displayed.
886 * @param title The title of the dialog.
887 * @param optionType The optionType.
888 * @param messageType The messageType.
890 * @return The selected option.
892 public static int showConfirmDialog(Component parentComponent,
893 Object message, String title,
894 int optionType, int messageType)
896 JOptionPane pane = new JOptionPane(message, messageType, optionType);
897 JDialog dialog = pane.createDialog(parentComponent, title);
898 dialog.pack();
899 dialog.show();
901 return ((Integer) pane.getValue()).intValue();
905 * This method shows a confirmation dialog with the given message, title,
906 * optionType, messageType and icon. The frame owner will be the same as
907 * the one that holds the given parentComponent. This method returns the
908 * option selected by the user.
910 * @param parentComponent The component to find a frame in.
911 * @param message The message displayed.
912 * @param title The title of the dialog.
913 * @param optionType The optionType.
914 * @param messageType The messsageType.
915 * @param icon The icon displayed.
917 * @return The selected option.
919 public static int showConfirmDialog(Component parentComponent,
920 Object message, String title,
921 int optionType, int messageType,
922 Icon icon)
924 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
925 JDialog dialog = pane.createDialog(parentComponent, title);
926 dialog.pack();
927 dialog.show();
929 return ((Integer) pane.getValue()).intValue();
933 * This method will show a QUESTION_MESSAGE input dialog with the given
934 * message. No selectionValues is set so the Look and Feel will usually
935 * give the user a TextField to fill out. The frame owner will be the same
936 * frame that holds the given parentComponent. This method will return the
937 * value entered by the user.
939 * @param parentComponent The component to find a frame in.
940 * @param message The message displayed.
942 * @return The value entered by the user.
944 public static String showInputDialog(Component parentComponent,
945 Object message)
947 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
948 pane.setWantsInput(true);
949 JDialog dialog = pane.createDialog(parentComponent, null);
950 dialog.pack();
951 dialog.show();
953 return (String) pane.getInputValue();
957 * This method will show a QUESTION_MESSAGE type input dialog with the given
958 * message and initialSelectionValue. Since there is no selectionValues
959 * set, the Look and Feel will usually give a TextField to fill out. The
960 * frame owner will be the same as the one that holds the given
961 * parentComponent. This method will return the value entered by the user.
963 * @param parentComponent The component to find a frame in.
964 * @param message The message to display.
965 * @param initialSelectionValue The initially selected value.
967 * @return The value the user input.
969 public static String showInputDialog(Component parentComponent,
970 Object message,
971 Object initialSelectionValue)
973 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
974 pane.setInitialSelectionValue(initialSelectionValue);
975 pane.setWantsInput(true);
976 JDialog dialog = pane.createDialog(parentComponent, null);
977 dialog.pack();
978 dialog.show();
980 return (String) pane.getInputValue();
984 * This method displays a new input dialog with the given message, title and
985 * messageType. Since no selectionValues value is given, the Look and Feel
986 * will usually give the user a TextField to input data to. This method
987 * returns the value the user inputs.
989 * @param parentComponent The component to find a frame in.
990 * @param message The message to display.
991 * @param title The title of the dialog.
992 * @param messageType The messageType.
994 * @return The value the user input.
996 public static String showInputDialog(Component parentComponent,
997 Object message, String title,
998 int messageType)
1000 JOptionPane pane = new JOptionPane(message, messageType);
1001 pane.setWantsInput(true);
1002 JDialog dialog = pane.createDialog(parentComponent, title);
1003 dialog.pack();
1004 dialog.show();
1006 return (String) pane.getInputValue();
1010 * This method shows an input dialog with the given message, title,
1011 * messageType, icon, selectionValues, and initialSelectionValue. This
1012 * method returns the value that the user selects.
1014 * @param parentComponent The component to find a frame in.
1015 * @param message The message displayed.
1016 * @param title The title of the dialog.
1017 * @param messageType The messageType.
1018 * @param icon The icon displayed.
1019 * @param selectionValues The list of values to select from.
1020 * @param initialSelectionValue The initially selected value.
1022 * @return The user selected value.
1024 public static Object showInputDialog(Component parentComponent,
1025 Object message, String title,
1026 int messageType, Icon icon,
1027 Object[] selectionValues,
1028 Object initialSelectionValue)
1030 JOptionPane pane = new JOptionPane(message, messageType);
1031 pane.setWantsInput(true);
1032 pane.setIcon(icon);
1033 pane.setSelectionValues(selectionValues);
1034 pane.setInitialSelectionValue(initialSelectionValue);
1035 JDialog dialog = pane.createDialog(parentComponent, title);
1036 dialog.pack();
1037 dialog.show();
1039 return (String) pane.getInputValue();
1043 * This method shows a QUESTION_MESSAGE type input dialog. Since no
1044 * selectionValues is set, the Look and Feel will usually give the user a
1045 * TextField to input data to. This method returns the value the user
1046 * inputs.
1048 * @param message The message to display.
1050 * @return The user selected value.
1052 public static String showInputDialog(Object message)
1054 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1055 pane.setWantsInput(true);
1056 JDialog dialog = pane.createDialog(null, null);
1057 dialog.pack();
1058 dialog.show();
1060 return (String) pane.getInputValue();
1064 * This method shows a QUESTION_MESSAGE type input dialog. Since no
1065 * selectionValues is set, the Look and Feel will usually give the user a
1066 * TextField to input data to. The input component will be initialized with
1067 * the initialSelectionValue. This method returns the value the user
1068 * inputs.
1070 * @param message The message to display.
1071 * @param initialSelectionValue The initialSelectionValue.
1073 * @return The user selected value.
1075 public static String showInputDialog(Object message,
1076 Object initialSelectionValue)
1078 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1079 pane.setWantsInput(true);
1080 pane.setInitialSelectionValue(initialSelectionValue);
1081 JDialog dialog = pane.createDialog(null, null);
1082 dialog.pack();
1083 dialog.show();
1085 return (String) pane.getInputValue();
1089 * This method shows an internal confirmation dialog with the given message.
1090 * The internal frame dialog will be placed in the first JDesktopPane
1091 * ancestor of the given parentComponent. This method will return the value
1092 * selected.
1094 * @param parentComponent The parent to find a JDesktopPane in.
1095 * @param message The message to display.
1097 * @return The value selected.
1099 public static int showInternalConfirmDialog(Component parentComponent,
1100 Object message)
1102 JOptionPane pane = new JOptionPane(message);
1103 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1105 startModal(frame, pane);
1107 return ((Integer) pane.getValue()).intValue();
1111 * This method shows an internal confirmation dialog with the given message,
1112 * optionType and title. The internal frame dialog will be placed in the
1113 * first JDesktopPane ancestor of the given parentComponent. This method
1114 * will return the selected value.
1116 * @param parentComponent The parent to find a JDesktopPane in.
1117 * @param message The message to display.
1118 * @param title The title to display.
1119 * @param optionType The option type.
1121 * @return The selected value.
1123 public static int showInternalConfirmDialog(Component parentComponent,
1124 Object message, String title,
1125 int optionType)
1127 JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
1128 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1130 startModal(frame, pane);
1132 return ((Integer) pane.getValue()).intValue();
1136 * This method shows an internal confirmation dialog with the given message,
1137 * title, optionTypes and icon for the given message type. The internal
1138 * confirmation dialog will be placed in the first instance of
1139 * JDesktopPane ancestor of the given parentComponent.
1141 * @param parentComponent The component to find a JDesktopPane in.
1142 * @param message The message to display.
1143 * @param title The title of the dialog.
1144 * @param optionType The option type.
1145 * @param messageType The message type.
1147 * @return The selected value.
1149 public static int showInternalConfirmDialog(Component parentComponent,
1150 Object message, String title,
1151 int optionType, int messageType)
1153 JOptionPane pane = new JOptionPane(message, messageType, optionType);
1154 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1156 startModal(frame, pane);
1158 return ((Integer) pane.getValue()).intValue();
1162 * This method shows an internal confirmation dialog with the given message,
1163 * title, option type, message type, and icon. The internal frame dialog
1164 * will be placed in the first JDesktopPane ancestor that is found in the
1165 * given parentComponent. This method returns the selected value.
1167 * @param parentComponent The parent to find a JDesktopPane in.
1168 * @param message The message to display.
1169 * @param title The title to display.
1170 * @param optionType The option type.
1171 * @param messageType The message type.
1172 * @param icon The icon to display.
1174 * @return The selected value.
1176 public static int showInternalConfirmDialog(Component parentComponent,
1177 Object message, String title,
1178 int optionType, int messageType,
1179 Icon icon)
1181 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
1182 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1184 startModal(frame, pane);
1186 return ((Integer) pane.getValue()).intValue();
1190 * This method shows an internal input dialog with the given message. The
1191 * internal frame dialog will be placed in the first JDesktopPane ancestor
1192 * of the given parent component. This method returns the value input by
1193 * the user.
1195 * @param parentComponent The parent to find a JDesktopPane in.
1196 * @param message The message to display.
1198 * @return The user selected value.
1200 public static String showInternalInputDialog(Component parentComponent,
1201 Object message)
1203 JOptionPane pane = new JOptionPane(message);
1204 pane.setWantsInput(true);
1205 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1207 startModal(frame, pane);
1209 return (String) pane.getInputValue();
1213 * This method shows an internal input dialog with the given message, title
1214 * and message type. The internal input dialog will be placed in the first
1215 * JDesktopPane ancestor found in the given parent component. This method
1216 * will return the input value given by the user.
1218 * @param parentComponent The component to find a JDesktopPane in.
1219 * @param message The message to display.
1220 * @param title The title to display.
1221 * @param messageType The message type.
1223 * @return The user input value.
1225 public static String showInternalInputDialog(Component parentComponent,
1226 Object message, String title,
1227 int messageType)
1229 JOptionPane pane = new JOptionPane(message, messageType);
1230 pane.setWantsInput(true);
1231 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1233 startModal(frame, pane);
1235 return (String) pane.getInputValue();
1239 * This method shows an internal input dialog with the given message, title
1240 * message type, icon, selection value list and initial selection value.
1241 * The internal frame dialog will be placed in the first JDesktopPane
1242 * ancestor found in the given parent component. This method returns the
1243 * input value from the user.
1245 * @param parentComponent The parent to find a JDesktopPane in.
1246 * @param message The message to display.
1247 * @param title The title to display.
1248 * @param messageType The message type.
1249 * @param icon The icon to display.
1250 * @param selectionValues The selection value list.
1251 * @param initialSelectionValue The initial selection value.
1253 * @return The user input value.
1255 public static Object showInternalInputDialog(Component parentComponent,
1256 Object message, String title,
1257 int messageType, Icon icon,
1258 Object[] selectionValues,
1259 Object initialSelectionValue)
1261 JOptionPane pane = new JOptionPane(message, messageType);
1262 pane.setWantsInput(true);
1263 pane.setIcon(icon);
1264 pane.setSelectionValues(selectionValues);
1265 pane.setInitialSelectionValue(initialSelectionValue);
1266 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1268 startModal(frame, pane);
1270 return (String) pane.getInputValue();
1274 * This method shows an internal message dialog with the given message. The
1275 * internal frame dialog will be placed in the first JDesktopPane ancestor
1276 * found in the given parent component.
1278 * @param parentComponent The component to find a JDesktopPane in.
1279 * @param message The message to display.
1281 public static void showInternalMessageDialog(Component parentComponent,
1282 Object message)
1284 JOptionPane pane = new JOptionPane(message);
1285 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1287 startModal(frame, pane);
1291 * This method shows an internal message dialog with the given message,
1292 * title and message type. The internal message dialog is placed in the
1293 * first JDesktopPane ancestor found in the given parent component.
1295 * @param parentComponent The parent component to find a JDesktopPane in.
1296 * @param message The message to display.
1297 * @param title The title to display.
1298 * @param messageType The message type.
1300 public static void showInternalMessageDialog(Component parentComponent,
1301 Object message, String title,
1302 int messageType)
1304 JOptionPane pane = new JOptionPane(message, messageType);
1305 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1307 startModal(frame, pane);
1311 * This method shows an internal message dialog with the given message,
1312 * title, message type and icon. The internal message dialog is placed in
1313 * the first JDesktopPane ancestor found in the given parent component.
1315 * @param parentComponent The component to find a JDesktopPane in.
1316 * @param message The message to display.
1317 * @param title The title to display.
1318 * @param messageType The message type.
1319 * @param icon The icon to display.
1321 public static void showInternalMessageDialog(Component parentComponent,
1322 Object message, String title,
1323 int messageType, Icon icon)
1325 JOptionPane pane = new JOptionPane(message, messageType);
1326 pane.setIcon(icon);
1327 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1329 startModal(frame, pane);
1333 * This method displays an internal option dialog with the given message,
1334 * title, option type, message type, icon, option list, and initial option
1335 * value. The internal option dialog is placed in the first JDesktopPane
1336 * ancestor found in the parent component. This method returns the option
1337 * selected.
1339 * @param parentComponent The parent to find a JDesktopPane in.
1340 * @param message The message displayed.
1341 * @param title The title displayed.
1342 * @param optionType The option type.
1343 * @param messageType The message type.
1344 * @param icon The icon to display.
1345 * @param options The array of options.
1346 * @param initialValue The initial value selected.
1348 * @return The option that was selected.
1350 public static int showInternalOptionDialog(Component parentComponent,
1351 Object message, String title,
1352 int optionType, int messageType,
1353 Icon icon, Object[] options,
1354 Object initialValue)
1356 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1357 options, initialValue);
1359 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1361 startModal(frame, pane);
1363 return ((Integer) pane.getValue()).intValue();
1367 * This method shows an INFORMATION_MESSAGE type message dialog.
1369 * @param parentComponent The component to find a frame in.
1370 * @param message The message displayed.
1372 public static void showMessageDialog(Component parentComponent,
1373 Object message)
1375 JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE);
1376 JDialog dialog = pane.createDialog(parentComponent, null);
1377 dialog.pack();
1378 dialog.show();
1382 * This method shows a message dialog with the given message, title and
1383 * messageType.
1385 * @param parentComponent The component to find a frame in.
1386 * @param message The message displayed.
1387 * @param title The title of the dialog.
1388 * @param messageType The messageType.
1390 public static void showMessageDialog(Component parentComponent,
1391 Object message, String title,
1392 int messageType)
1394 JOptionPane pane = new JOptionPane(message, messageType);
1395 JDialog dialog = pane.createDialog(parentComponent, title);
1396 dialog.pack();
1397 dialog.show();
1401 * This method shows a message dialog with the given message, title,
1402 * messageType and icon.
1404 * @param parentComponent The component to find a frame in.
1405 * @param message The message displayed.
1406 * @param title The title of the dialog.
1407 * @param messageType The messageType.
1408 * @param icon The icon displayed.
1410 public static void showMessageDialog(Component parentComponent,
1411 Object message, String title,
1412 int messageType, Icon icon)
1414 JOptionPane pane = new JOptionPane(message, messageType);
1415 pane.setIcon(icon);
1416 JDialog dialog = pane.createDialog(parentComponent, title);
1417 dialog.pack();
1418 dialog.show();
1422 * This method shows an option dialog with the given message, title,
1423 * optionType, messageType, icon, options and initialValue. This method
1424 * returns the option that was selected.
1426 * @param parentComponent The component to find a frame in.
1427 * @param message The message displayed.
1428 * @param title The title of the dialog.
1429 * @param optionType The optionType.
1430 * @param messageType The messageType.
1431 * @param icon The icon displayed.
1432 * @param options The options to choose from.
1433 * @param initialValue The initial value.
1435 * @return The selected option.
1437 public static int showOptionDialog(Component parentComponent,
1438 Object message, String title,
1439 int optionType, int messageType,
1440 Icon icon, Object[] options,
1441 Object initialValue)
1443 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1444 options, initialValue);
1446 JDialog dialog = pane.createDialog(parentComponent, title);
1447 dialog.pack();
1448 dialog.show();
1450 return ((Integer) pane.getValue()).intValue();
1454 * This method resets the UI to the Look and Feel default.
1456 public void updateUI()
1458 setUI((OptionPaneUI) UIManager.getUI(this));
1459 invalidate();
1463 * This method returns true if the key is a valid messageType.
1465 * @param key The key to check.
1467 * @return True if key is valid.
1469 private boolean validMessageType(int key)
1471 switch (key)
1473 case ERROR_MESSAGE:
1474 case INFORMATION_MESSAGE:
1475 case PLAIN_MESSAGE:
1476 case QUESTION_MESSAGE:
1477 case WARNING_MESSAGE:
1478 return true;
1480 return false;
1484 * This method returns true if the key is a valid optionType.
1486 * @param key The key to check.
1488 * @return True if key is valid.
1490 private boolean validOptionType(int key)
1492 switch (key)
1494 case DEFAULT_OPTION:
1495 case OK_CANCEL_OPTION:
1496 case YES_NO_CANCEL_OPTION:
1497 case YES_NO_OPTION:
1498 return true;
1500 return false;
1504 * This helper method makes the JInternalFrame wait until it is notified by
1505 * an InternalFrameClosing event. This method also adds the given
1506 * JOptionPane to the JInternalFrame and sizes it according to the
1507 * JInternalFrame's preferred size.
1509 * @param f The JInternalFrame to make modal.
1510 * @param pane The JOptionPane to add to the JInternalFrame.
1512 private static void startModal(JInternalFrame f, JOptionPane pane)
1514 f.getContentPane().add(pane);
1515 f.pack();
1516 f.show();
1518 Dimension pref = f.getPreferredSize();
1519 f.setBounds(0, 0, pref.width, pref.height);
1521 synchronized (f)
1523 final JInternalFrame tmp = f;
1524 tmp.toFront();
1526 f.addInternalFrameListener(new InternalFrameAdapter()
1528 public void internalFrameClosed(InternalFrameEvent e)
1530 synchronized (tmp)
1532 tmp.removeInternalFrameListener(this);
1533 tmp.notifyAll();
1539 while (! f.isClosed())
1540 f.wait();
1542 catch (InterruptedException ignored)