Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / JOptionPane.java
blob0bef12b61fc8deaa9a44d00249b8c7095e4b8c77
1 /* JOptionPane.java
2 Copyright (C) 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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.Frame;
44 import javax.accessibility.Accessible;
45 import javax.accessibility.AccessibleContext;
46 import javax.accessibility.AccessibleRole;
47 import javax.swing.event.InternalFrameAdapter;
48 import javax.swing.event.InternalFrameEvent;
49 import javax.swing.plaf.OptionPaneUI;
51 /**
52 * This class creates different types of JDialogs and JInternalFrames that can
53 * ask users for input or pass on information. JOptionPane can be used by
54 * calling one of the show static methods or by creating an instance of
55 * JOptionPane and calling createDialog or createInternalFrame.
57 public class JOptionPane extends JComponent implements Accessible
59 /**
60 * DOCUMENT ME!
62 // FIXME: This inner class is a complete stub and needs to be implemented
63 // properly.
64 protected class AccessibleJOptionPane extends JComponent.AccessibleJComponent
66 /** DOCUMENT ME! */
67 private static final long serialVersionUID = 686071432213084821L;
69 /**
70 * Creates a new AccessibleJOptionPane object.
72 protected AccessibleJOptionPane()
74 // Nothing to do here.
77 /**
78 * Returns the accessible role of this object, which is always
79 * {@link AccessibleRole#OPTION_PANE}.
81 * @return the accessible role of this object
83 public AccessibleRole getAccessibleRole()
85 return AccessibleRole.OPTION_PANE;
89 /** DOCUMENT ME! */
90 private static final long serialVersionUID = 5231143276678566796L;
92 /** The value returned when cancel option is selected. */
93 public static final int CANCEL_OPTION = 2;
95 /** The value returned when the dialog is closed without a selection. */
96 public static final int CLOSED_OPTION = -1;
98 /** An option used in confirmation dialog methods. */
99 public static final int DEFAULT_OPTION = -1;
101 /** The value returned when the no option is selected. */
102 public static final int NO_OPTION = 1;
104 /** An option used in confirmation dialog methods. */
105 public static final int OK_CANCEL_OPTION = 2;
107 /** The value returned when the ok option is selected. */
108 public static final int OK_OPTION = 0;
110 /** An option used in confirmation dialog methods. */
111 public static final int YES_NO_CANCEL_OPTION = 1;
113 /** An option used in confirmation dialog methods. */
114 public static final int YES_NO_OPTION = 0;
116 /** The value returned when the yes option is selected. */
117 public static final int YES_OPTION = 0;
119 /** Identifier for the error message type. */
120 public static final int ERROR_MESSAGE = 0;
122 /** Identifier for the information message type. */
123 public static final int INFORMATION_MESSAGE = 1;
125 /** Identifier for the plain message type. */
126 public static final int PLAIN_MESSAGE = -1;
128 /** Identifier for the question message type. */
129 public static final int QUESTION_MESSAGE = 3;
131 /** Identifier for the warning message type. */
132 public static final int WARNING_MESSAGE = 2;
135 * The identifier for the propertyChangeEvent when the icon property
136 * changes.
138 public static final String ICON_PROPERTY = "icon";
141 * The identifier for the propertyChangeEvent when the initialSelectionValue
142 * property changes.
144 public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
147 * The identifier for the propertyChangeEvent when the initialValue property
148 * changes.
150 public static final String INITIAL_VALUE_PROPERTY = "initialValue";
153 * The identifier for the propertyChangeEvent when the inputValue property
154 * changes.
156 public static final String INPUT_VALUE_PROPERTY = "inputValue";
159 * The identifier for the propertyChangeEvent when the message property
160 * changes.
162 public static final String MESSAGE_PROPERTY = "message";
165 * The identifier for the propertyChangeEvent when the messageType property
166 * changes.
168 public static final String MESSAGE_TYPE_PROPERTY = "messageType";
171 * The identifier for the propertyChangeEvent when the optionType property
172 * changes.
174 public static final String OPTION_TYPE_PROPERTY = "optionType";
177 * The identifier for the propertyChangeEvent when the options property
178 * changes.
180 public static final String OPTIONS_PROPERTY = "options";
183 * The identifier for the propertyChangeEvent when the selectionValues
184 * property changes.
186 public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
189 * The identifier for the propertyChangeEvent when the value property
190 * changes.
192 public static final String VALUE_PROPERTY = "value";
195 * The identifier for the propertyChangeEvent when the wantsInput property
196 * changes.
198 public static final String WANTS_INPUT_PROPERTY = "wantsInput";
200 /** The value returned when the inputValue is uninitialized. */
201 public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
203 /** The icon displayed in the dialog/internal frame. */
204 protected Icon icon;
206 /** The initial selected value in the input component. */
207 protected Object initialSelectionValue;
209 /** The object that is initially selected for options. */
210 protected Object initialValue;
212 /** The value the user inputs. */
213 protected Object inputValue = UNINITIALIZED_VALUE;
215 /** The message displayed in the dialog/internal frame. */
216 protected Object message;
218 /** The type of message displayed. */
219 protected int messageType = PLAIN_MESSAGE;
222 * The options (usually buttons) aligned at the bottom for the user to
223 * select.
225 protected Object[] options;
227 /** The type of options to display. */
228 protected int optionType = DEFAULT_OPTION;
230 /** The input values the user can select. */
231 protected Object[] selectionValues;
233 /** The value returned by selecting an option. */
234 protected Object value = UNINITIALIZED_VALUE;
236 /** Whether the Dialog/InternalFrame needs input. */
237 protected boolean wantsInput;
239 /** The common frame used when no parent is provided. */
240 private static Frame privFrame = (Frame) SwingUtilities.getOwnerFrame(null);
243 * Creates a new JOptionPane object using a message of "JOptionPane
244 * message", using the PLAIN_MESSAGE type and DEFAULT_OPTION.
246 public JOptionPane()
248 this("JOptionPane message", PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
252 * Creates a new JOptionPane object using the given message using the
253 * PLAIN_MESSAGE type and DEFAULT_OPTION.
255 * @param message The message to display.
257 public JOptionPane(Object message)
259 this(message, PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
263 * Creates a new JOptionPane object using the given message and messageType
264 * and DEFAULT_OPTION.
266 * @param message The message to display.
267 * @param messageType The type of message.
269 public JOptionPane(Object message, int messageType)
271 this(message, messageType, DEFAULT_OPTION, null, null, null);
275 * Creates a new JOptionPane object using the given message, messageType and
276 * optionType.
278 * @param message The message to display.
279 * @param messageType The type of message.
280 * @param optionType The type of options.
282 public JOptionPane(Object message, int messageType, int optionType)
284 this(message, messageType, optionType, null, null, null);
288 * Creates a new JOptionPane object using the given message, messageType,
289 * optionType and icon.
291 * @param message The message to display.
292 * @param messageType The type of message.
293 * @param optionType The type of options.
294 * @param icon The icon to display.
296 public JOptionPane(Object message, int messageType, int optionType, Icon icon)
298 this(message, messageType, optionType, icon, null, null);
302 * Creates a new JOptionPane object using the given message, messageType,
303 * optionType, icon and options.
305 * @param message The message to display.
306 * @param messageType The type of message.
307 * @param optionType The type of options.
308 * @param icon The icon to display.
309 * @param options The options given.
311 public JOptionPane(Object message, int messageType, int optionType,
312 Icon icon, Object[] options)
314 this(message, messageType, optionType, icon, options, null);
318 * Creates a new JOptionPane object using the given message, messageType,
319 * optionType, icon, options and initialValue. The initialValue will be
320 * focused initially.
322 * @param message The message to display.
323 * @param messageType The type of message.
324 * @param optionType The type of options.
325 * @param icon The icon to display.
326 * @param options The options given.
327 * @param initialValue The component to focus on initially.
329 * @throws IllegalArgumentException If the messageType or optionType are not
330 * legal values.
332 public JOptionPane(Object message, int messageType, int optionType,
333 Icon icon, Object[] options, Object initialValue)
335 this.message = message;
336 if (! validMessageType(messageType))
337 throw new IllegalArgumentException("Message Type not legal value.");
338 this.messageType = messageType;
339 if (! validOptionType(optionType))
340 throw new IllegalArgumentException("Option Type not legal value.");
341 this.optionType = optionType;
342 this.icon = icon;
343 this.options = options;
344 this.initialValue = initialValue;
346 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
348 updateUI();
352 * This method creates a new JDialog that is either centered around the
353 * parent's frame or centered on the screen (if the parent is null). The
354 * JDialog will not be resizable and will be modal. Once the JDialog is
355 * disposed, the inputValue and value properties will be set by the
356 * optionPane.
358 * @param parentComponent The parent of the Dialog.
359 * @param title The title in the bar of the JDialog.
361 * @return A new JDialog based on the JOptionPane configuration.
363 public JDialog createDialog(Component parentComponent, String title)
365 Frame toUse = getFrameForComponent(parentComponent);
366 if (toUse == null)
367 toUse = getRootFrame();
369 JDialog dialog = new JDialog(toUse, title);
370 inputValue = UNINITIALIZED_VALUE;
371 value = UNINITIALIZED_VALUE;
373 dialog.getContentPane().add(this);
374 dialog.setModal(true);
375 dialog.setResizable(false);
376 dialog.pack();
377 dialog.setLocationRelativeTo(parentComponent);
379 return dialog;
383 * This method creates a new JInternalFrame that is in the JLayeredPane
384 * which contains the parentComponent given. If no suitable JLayeredPane
385 * can be found from the parentComponent given, a RuntimeException will be
386 * thrown.
388 * @param parentComponent The parent to find a JDesktopPane from.
389 * @param title The title of the JInternalFrame.
391 * @return A new JInternalFrame based on the JOptionPane configuration.
393 * @throws RuntimeException If no suitable JDesktopPane is found.
395 * @specnote The specification says that the internal frame is placed
396 * in the nearest <code>JDesktopPane</code> that is found in
397 * <code>parent</code>'s ancestors. The behaviour of the JDK
398 * is that it actually looks up the nearest
399 * <code>JLayeredPane</code> in <code>parent</code>'s ancestors.
400 * So do we.
402 public JInternalFrame createInternalFrame(Component parentComponent,
403 String title)
404 throws RuntimeException
406 // Try to find a JDesktopPane.
407 JLayeredPane toUse = getDesktopPaneForComponent(parentComponent);
408 // If we don't have a JDesktopPane, we try to find a JLayeredPane.
409 if (toUse == null)
410 toUse = JLayeredPane.getLayeredPaneAbove(parentComponent);
411 // If this still fails, we throw a RuntimeException.
412 if (toUse == null)
413 throw new RuntimeException
414 ("parentComponent does not have a valid parent");
416 JInternalFrame frame = new JInternalFrame(title);
418 inputValue = UNINITIALIZED_VALUE;
419 value = UNINITIALIZED_VALUE;
421 frame.setContentPane(this);
422 frame.setClosable(true);
424 toUse.add(frame);
425 frame.setLayer(JLayeredPane.MODAL_LAYER);
427 frame.pack();
428 frame.setVisible(true);
430 return frame;
434 * DOCUMENT ME!
436 * @return DOCUMENT ME!
438 public AccessibleContext getAccessibleContext()
440 if (accessibleContext == null)
441 accessibleContext = new AccessibleJOptionPane();
442 return accessibleContext;
446 * This method returns the JDesktopPane for the given parentComponent or
447 * null if none can be found.
449 * @param parentComponent The component to look in.
451 * @return The JDesktopPane for the given component or null if none can be
452 * found.
454 public static JDesktopPane getDesktopPaneForComponent(Component parentComponent)
456 return (JDesktopPane) SwingUtilities.getAncestorOfClass(JDesktopPane.class,
457 parentComponent);
461 * This method returns the Frame for the given parentComponent or null if
462 * none can be found.
464 * @param parentComponent The component to look in.
466 * @return The Frame for the given component or null if none can be found.
468 public static Frame getFrameForComponent(Component parentComponent)
470 return (Frame) SwingUtilities.getAncestorOfClass(Frame.class,
471 parentComponent);
475 * This method returns the icon displayed.
477 * @return The icon displayed.
479 public Icon getIcon()
481 return icon;
485 * This method returns the value initially selected from the list of values
486 * the user can input.
488 * @return The initial selection value.
490 public Object getInitialSelectionValue()
492 return initialSelectionValue;
496 * This method returns the value that is focused from the list of options.
498 * @return The initial value from options.
500 public Object getInitialValue()
502 return initialValue;
506 * This method returns the value that the user input.
508 * @return The user's input value.
510 public Object getInputValue()
512 if (getValue().equals(new Integer(CANCEL_OPTION)))
513 setInputValue(null);
514 return inputValue;
518 * This method returns the maximum characters per line. By default, this is
519 * Integer.MAX_VALUE.
521 * @return The maximum characters per line.
523 public int getMaxCharactersPerLineCount()
525 return Integer.MAX_VALUE;
529 * This method returns the message displayed.
531 * @return The message displayed.
533 public Object getMessage()
535 return message;
539 * This method returns the message type.
541 * @return The message type.
543 public int getMessageType()
545 return messageType;
549 * This method returns the options.
551 * @return The options.
553 public Object[] getOptions()
555 return options;
559 * This method returns the option type.
561 * @return The option type.
563 public int getOptionType()
565 return optionType;
569 * This method returns the Frame used by JOptionPane dialog's that have no
570 * parent.
572 * @return The Frame used by dialogs that have no parent.
574 public static Frame getRootFrame()
576 return privFrame;
580 * This method returns the selection values.
582 * @return The selection values.
584 public Object[] getSelectionValues()
586 return selectionValues;
590 * This method returns the UI used by the JOptionPane.
592 * @return The UI used by the JOptionPane.
594 public OptionPaneUI getUI()
596 return (OptionPaneUI) ui;
600 * This method returns an identifier to determine which UI class will act as
601 * the UI.
603 * @return The UI identifier.
605 public String getUIClassID()
607 return "OptionPaneUI";
611 * This method returns the value that the user selected out of options.
613 * @return The value that the user selected out of options.
615 public Object getValue()
617 return value;
621 * This method returns whether this JOptionPane wants input.
623 * @return Whether this JOptionPane wants input.
625 public boolean getWantsInput()
627 return wantsInput;
631 * This method returns a String that describes this JOptionPane.
633 * @return A String that describes this JOptionPane.
635 protected String paramString()
637 return "JOptionPane";
641 * This method requests focus for the initial value.
643 public void selectInitialValue()
645 if (ui != null)
646 ((OptionPaneUI) ui).selectInitialValue(this);
650 * This method changes the icon property.
652 * @param newIcon The new icon to use.
654 public void setIcon(Icon newIcon)
656 if (icon != newIcon)
658 Icon old = icon;
659 icon = newIcon;
660 firePropertyChange(ICON_PROPERTY, old, icon);
665 * This method changes the initial selection property.
667 * @param newValue The new initial selection.
669 public void setInitialSelectionValue(Object newValue)
671 if (initialSelectionValue != newValue)
673 Object old = initialSelectionValue;
674 initialSelectionValue = newValue;
675 firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, old,
676 initialSelectionValue);
681 * This method changes the initial value property.
683 * @param newValue The new initial value.
685 public void setInitialValue(Object newValue)
687 if (initialValue != newValue)
689 Object old = initialValue;
690 initialValue = newValue;
691 firePropertyChange(INITIAL_VALUE_PROPERTY, old, initialValue);
696 * This method changes the inputValue property.
698 * @param newValue The new inputValue.
700 public void setInputValue(Object newValue)
702 if (inputValue != newValue)
704 Object old = inputValue;
705 inputValue = newValue;
706 firePropertyChange(INPUT_VALUE_PROPERTY, old, inputValue);
711 * This method changes the message property.
713 * @param newMessage The new message.
715 public void setMessage(Object newMessage)
717 if (message != newMessage)
719 Object old = message;
720 message = newMessage;
721 firePropertyChange(MESSAGE_PROPERTY, old, message);
726 * This method changes the messageType property.
728 * @param newType The new messageType.
730 * @throws IllegalArgumentException If the messageType is not valid.
732 public void setMessageType(int newType)
734 if (! validMessageType(newType))
735 throw new IllegalArgumentException("Message Type not legal value.");
736 if (newType != messageType)
738 int old = messageType;
739 messageType = newType;
740 firePropertyChange(MESSAGE_TYPE_PROPERTY, old, messageType);
745 * This method changes the options property.
747 * @param newOptions The new options.
749 public void setOptions(Object[] newOptions)
751 if (options != newOptions)
753 Object[] old = options;
754 options = newOptions;
755 firePropertyChange(OPTIONS_PROPERTY, old, options);
760 * This method changes the optionType property.
762 * @param newType The new optionType.
764 * @throws IllegalArgumentException If the optionType is not valid.
766 public void setOptionType(int newType)
768 if (! validOptionType(newType))
769 throw new IllegalArgumentException("Option Type not legal value.");
770 if (newType != optionType)
772 int old = optionType;
773 optionType = newType;
774 firePropertyChange(OPTION_TYPE_PROPERTY, old, optionType);
779 * This method changes the Frame used for JOptionPane dialogs that have no
780 * parent.
782 * @param newRootFrame The Frame to use for dialogs that have no parent.
784 public static void setRootFrame(Frame newRootFrame)
786 privFrame = newRootFrame;
790 * This method changes the selectionValues property.
792 * @param newValues The new selectionValues.
794 public void setSelectionValues(Object[] newValues)
796 if (newValues != selectionValues)
798 if (newValues != null)
799 wantsInput = true;
800 Object[] old = selectionValues;
801 selectionValues = newValues;
802 firePropertyChange(SELECTION_VALUES_PROPERTY, old, selectionValues);
807 * This method sets the UI used with the JOptionPane.
809 * @param ui The UI used with the JOptionPane.
811 public void setUI(OptionPaneUI ui)
813 super.setUI(ui);
817 * This method sets the value has been selected out of options.
819 * @param newValue The value that has been selected out of options.
821 public void setValue(Object newValue)
823 if (value != newValue)
825 Object old = value;
826 value = newValue;
827 firePropertyChange(VALUE_PROPERTY, old, value);
832 * This method changes the wantsInput property.
834 * @param newValue Whether this JOptionPane requires input.
836 public void setWantsInput(boolean newValue)
838 if (wantsInput != newValue)
840 boolean old = wantsInput;
841 wantsInput = newValue;
842 firePropertyChange(WANTS_INPUT_PROPERTY, old, wantsInput);
847 * This method shows a confirmation dialog with the title "Select an Option"
848 * and displays the given message. The parent frame will be the same as the
849 * parent frame of the given parentComponent. This method returns the
850 * option chosen by the user.
852 * @param parentComponent The parentComponent to find a frame in.
853 * @param message The message to display.
855 * @return The option that was selected.
857 public static int showConfirmDialog(Component parentComponent, Object message)
859 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
860 JDialog dialog = pane.createDialog(parentComponent, "Select an Option");
861 dialog.show();
863 if (pane.getValue() instanceof Integer)
864 return ((Integer) pane.getValue()).intValue();
865 return -1;
869 * This method shows a confirmation dialog with the given message,
870 * optionType and title. The frame that owns the dialog will be the same
871 * frame that holds the given parentComponent. This method returns the
872 * option that was chosen.
874 * @param parentComponent The component to find a frame in.
875 * @param message The message displayed.
876 * @param title The title of the dialog.
877 * @param optionType The optionType.
879 * @return The option that was chosen.
881 public static int showConfirmDialog(Component parentComponent,
882 Object message, String title,
883 int optionType)
885 JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
886 JDialog dialog = pane.createDialog(parentComponent, title);
887 dialog.show();
889 if (pane.getValue() instanceof Integer)
890 return ((Integer) pane.getValue()).intValue();
891 return -1;
895 * This method shows a confirmation dialog with the given message, title,
896 * messageType and optionType. The frame owner will be the same frame as
897 * the one that holds the given parentComponent. This method returns the
898 * option selected by the user.
900 * @param parentComponent The component to find a frame in.
901 * @param message The message displayed.
902 * @param title The title of the dialog.
903 * @param optionType The optionType.
904 * @param messageType The messageType.
906 * @return The selected option.
908 public static int showConfirmDialog(Component parentComponent,
909 Object message, String title,
910 int optionType, int messageType)
912 JOptionPane pane = new JOptionPane(message, messageType, optionType);
913 JDialog dialog = pane.createDialog(parentComponent, title);
914 dialog.show();
916 if (pane.getValue() instanceof Integer)
917 return ((Integer) pane.getValue()).intValue();
918 return -1;
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,
939 Icon icon)
941 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
942 JDialog dialog = pane.createDialog(parentComponent, title);
943 dialog.show();
945 if (pane.getValue() instanceof Integer)
946 return ((Integer) pane.getValue()).intValue();
947 return -1;
951 * This method will show a QUESTION_MESSAGE input dialog with the given
952 * message. No selectionValues is set so the Look and Feel will usually
953 * give the user a TextField to fill out. The frame owner will be the same
954 * frame that holds the given parentComponent. This method will return the
955 * value entered by the user.
957 * @param parentComponent The component to find a frame in.
958 * @param message The message displayed.
960 * @return The value entered by the user.
962 public static String showInputDialog(Component parentComponent,
963 Object message)
965 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
966 pane.setWantsInput(true);
967 JDialog dialog = pane.createDialog(parentComponent, null);
968 dialog.show();
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,
987 Object message,
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);
994 dialog.show();
996 return (String) pane.getInputValue();
1000 * This method displays a new input dialog with the given message, title and
1001 * messageType. Since no selectionValues value is given, the Look and Feel
1002 * will usually give the user a TextField to input data to. This method
1003 * returns the value the user inputs.
1005 * @param parentComponent The component to find a frame in.
1006 * @param message The message to display.
1007 * @param title The title of the dialog.
1008 * @param messageType The messageType.
1010 * @return The value the user input.
1012 public static String showInputDialog(Component parentComponent,
1013 Object message, String title,
1014 int messageType)
1016 JOptionPane pane = new JOptionPane(message, messageType);
1017 pane.setWantsInput(true);
1018 JDialog dialog = pane.createDialog(parentComponent, title);
1019 dialog.show();
1021 return (String) pane.getInputValue();
1025 * This method shows an input dialog with the given message, title,
1026 * messageType, icon, selectionValues, and initialSelectionValue. This
1027 * method returns the value that the user selects.
1029 * @param parentComponent The component to find a frame in.
1030 * @param message The message displayed.
1031 * @param title The title of the dialog.
1032 * @param messageType The messageType.
1033 * @param icon The icon displayed.
1034 * @param selectionValues The list of values to select from.
1035 * @param initialSelectionValue The initially selected value.
1037 * @return The user selected value.
1039 public static Object showInputDialog(Component parentComponent,
1040 Object message, String title,
1041 int messageType, Icon icon,
1042 Object[] selectionValues,
1043 Object initialSelectionValue)
1045 JOptionPane pane = new JOptionPane(message, messageType);
1046 pane.setWantsInput(true);
1047 pane.setIcon(icon);
1048 pane.setSelectionValues(selectionValues);
1049 pane.setInitialSelectionValue(initialSelectionValue);
1050 JDialog dialog = pane.createDialog(parentComponent, title);
1051 dialog.show();
1053 return pane.getInputValue();
1057 * This method shows a QUESTION_MESSAGE type input dialog. Since no
1058 * selectionValues is set, the Look and Feel will usually give the user a
1059 * TextField to input data to. This method returns the value the user
1060 * inputs.
1062 * @param message The message to display.
1064 * @return The user selected value.
1066 public static String showInputDialog(Object message)
1068 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1069 pane.setWantsInput(true);
1070 JDialog dialog = pane.createDialog(null, null);
1071 dialog.show();
1073 return (String) pane.getInputValue();
1077 * This method shows a QUESTION_MESSAGE type input dialog. Since no
1078 * selectionValues is set, the Look and Feel will usually give the user a
1079 * TextField to input data to. The input component will be initialized with
1080 * the initialSelectionValue. This method returns the value the user
1081 * inputs.
1083 * @param message The message to display.
1084 * @param initialSelectionValue The initialSelectionValue.
1086 * @return The user selected value.
1088 public static String showInputDialog(Object message,
1089 Object initialSelectionValue)
1091 JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1092 pane.setWantsInput(true);
1093 pane.setInitialSelectionValue(initialSelectionValue);
1094 JDialog dialog = pane.createDialog(null, null);
1095 dialog.show();
1097 return (String) pane.getInputValue();
1101 * This method shows an internal confirmation dialog with the given message.
1102 * The internal frame dialog will be placed in the first JDesktopPane
1103 * ancestor of the given parentComponent. This method will return the value
1104 * selected.
1106 * @param parentComponent The parent to find a JDesktopPane in.
1107 * @param message The message to display.
1109 * @return The value selected.
1111 public static int showInternalConfirmDialog(Component parentComponent,
1112 Object message)
1114 JOptionPane pane = new JOptionPane(message);
1115 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1117 startModal(frame);
1119 if (pane.getValue() instanceof Integer)
1120 return ((Integer) pane.getValue()).intValue();
1121 return -1;
1125 * This method shows an internal confirmation dialog with the given message,
1126 * optionType and title. The internal frame dialog will be placed in the
1127 * first JDesktopPane ancestor of the given parentComponent. This method
1128 * will return the selected value.
1130 * @param parentComponent The parent to find a JDesktopPane in.
1131 * @param message The message to display.
1132 * @param title The title to display.
1133 * @param optionType The option type.
1135 * @return The selected value.
1137 public static int showInternalConfirmDialog(Component parentComponent,
1138 Object message, String title,
1139 int optionType)
1141 JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
1142 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1144 startModal(frame);
1146 if (pane.getValue() instanceof Integer)
1147 return ((Integer) pane.getValue()).intValue();
1148 return -1;
1152 * This method shows an internal confirmation dialog with the given message,
1153 * title, optionTypes and icon for the given message type. The internal
1154 * confirmation dialog will be placed in the first instance of
1155 * JDesktopPane ancestor of the given parentComponent.
1157 * @param parentComponent The component to find a JDesktopPane in.
1158 * @param message The message to display.
1159 * @param title The title of the dialog.
1160 * @param optionType The option type.
1161 * @param messageType The message type.
1163 * @return The selected value.
1165 public static int showInternalConfirmDialog(Component parentComponent,
1166 Object message, String title,
1167 int optionType, int messageType)
1169 JOptionPane pane = new JOptionPane(message, messageType, optionType);
1170 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1172 startModal(frame);
1174 if (pane.getValue() instanceof Integer)
1175 return ((Integer) pane.getValue()).intValue();
1176 return -1;
1180 * This method shows an internal confirmation dialog with the given message,
1181 * title, option type, message type, and icon. The internal frame dialog
1182 * will be placed in the first JDesktopPane ancestor that is found in the
1183 * given parentComponent. This method returns the selected value.
1185 * @param parentComponent The parent to find a JDesktopPane in.
1186 * @param message The message to display.
1187 * @param title The title to display.
1188 * @param optionType The option type.
1189 * @param messageType The message type.
1190 * @param icon The icon to display.
1192 * @return The selected value.
1194 public static int showInternalConfirmDialog(Component parentComponent,
1195 Object message, String title,
1196 int optionType, int messageType,
1197 Icon icon)
1199 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
1200 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1202 startModal(frame);
1204 if (pane.getValue() instanceof Integer)
1205 return ((Integer) pane.getValue()).intValue();
1206 return -1;
1210 * This method shows an internal input dialog with the given message. The
1211 * internal frame dialog will be placed in the first JDesktopPane ancestor
1212 * of the given parent component. This method returns the value input by
1213 * the user.
1215 * @param parentComponent The parent to find a JDesktopPane in.
1216 * @param message The message to display.
1218 * @return The user selected value.
1220 public static String showInternalInputDialog(Component parentComponent,
1221 Object message)
1223 JOptionPane pane = new JOptionPane(message);
1224 pane.setWantsInput(true);
1225 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1227 startModal(frame);
1229 return (String) pane.getInputValue();
1233 * This method shows an internal input dialog with the given message, title
1234 * and message type. The internal input dialog will be placed in the first
1235 * JDesktopPane ancestor found in the given parent component. This method
1236 * will return the input value given by the user.
1238 * @param parentComponent The component to find a JDesktopPane in.
1239 * @param message The message to display.
1240 * @param title The title to display.
1241 * @param messageType The message type.
1243 * @return The user input value.
1245 public static String showInternalInputDialog(Component parentComponent,
1246 Object message, String title,
1247 int messageType)
1249 JOptionPane pane = new JOptionPane(message, messageType);
1250 pane.setWantsInput(true);
1251 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1253 startModal(frame);
1255 return (String) pane.getInputValue();
1259 * This method shows an internal input dialog with the given message, title
1260 * message type, icon, selection value list and initial selection value.
1261 * The internal frame dialog will be placed in the first JDesktopPane
1262 * ancestor found in the given parent component. This method returns the
1263 * input value from the user.
1265 * @param parentComponent The parent to find a JDesktopPane in.
1266 * @param message The message to display.
1267 * @param title The title to display.
1268 * @param messageType The message type.
1269 * @param icon The icon to display.
1270 * @param selectionValues The selection value list.
1271 * @param initialSelectionValue The initial selection value.
1273 * @return The user input value.
1275 public static Object showInternalInputDialog(Component parentComponent,
1276 Object message, String title,
1277 int messageType, Icon icon,
1278 Object[] selectionValues,
1279 Object initialSelectionValue)
1281 JOptionPane pane = new JOptionPane(message, messageType);
1282 pane.setWantsInput(true);
1283 pane.setIcon(icon);
1284 pane.setSelectionValues(selectionValues);
1285 pane.setInitialSelectionValue(initialSelectionValue);
1286 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1288 startModal(frame);
1290 return pane.getInputValue();
1294 * This method shows an internal message dialog with the given message. The
1295 * internal frame dialog will be placed in the first JDesktopPane ancestor
1296 * found in the given parent component.
1298 * @param parentComponent The component to find a JDesktopPane in.
1299 * @param message The message to display.
1301 public static void showInternalMessageDialog(Component parentComponent,
1302 Object message)
1304 JOptionPane pane = new JOptionPane(message);
1305 JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1307 startModal(frame);
1311 * This method shows an internal message dialog with the given message,
1312 * title and message type. The internal message dialog is placed in the
1313 * first JDesktopPane ancestor found in the given parent component.
1315 * @param parentComponent The parent 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.
1320 public static void showInternalMessageDialog(Component parentComponent,
1321 Object message, String title,
1322 int messageType)
1324 JOptionPane pane = new JOptionPane(message, messageType);
1325 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1327 startModal(frame);
1331 * This method shows an internal message dialog with the given message,
1332 * title, message type and icon. The internal message dialog is placed in
1333 * the first JDesktopPane ancestor found in the given parent component.
1335 * @param parentComponent The component to find a JDesktopPane in.
1336 * @param message The message to display.
1337 * @param title The title to display.
1338 * @param messageType The message type.
1339 * @param icon The icon to display.
1341 public static void showInternalMessageDialog(Component parentComponent,
1342 Object message, String title,
1343 int messageType, Icon icon)
1345 JOptionPane pane = new JOptionPane(message, messageType);
1346 pane.setIcon(icon);
1347 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1349 startModal(frame);
1353 * This method displays an internal option dialog with the given message,
1354 * title, option type, message type, icon, option list, and initial option
1355 * value. The internal option dialog is placed in the first JDesktopPane
1356 * ancestor found in the parent component. This method returns the option
1357 * selected.
1359 * @param parentComponent The parent to find a JDesktopPane in.
1360 * @param message The message displayed.
1361 * @param title The title displayed.
1362 * @param optionType The option type.
1363 * @param messageType The message type.
1364 * @param icon The icon to display.
1365 * @param options The array of options.
1366 * @param initialValue The initial value selected.
1368 * @return The option that was selected.
1370 public static int showInternalOptionDialog(Component parentComponent,
1371 Object message, String title,
1372 int optionType, int messageType,
1373 Icon icon, Object[] options,
1374 Object initialValue)
1376 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1377 options, initialValue);
1379 JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1381 startModal(frame);
1383 if (pane.getValue() instanceof Integer)
1384 return ((Integer) pane.getValue()).intValue();
1385 return -1;
1389 * This method shows an INFORMATION_MESSAGE type message dialog.
1391 * @param parentComponent The component to find a frame in.
1392 * @param message The message displayed.
1394 public static void showMessageDialog(Component parentComponent,
1395 Object message)
1397 JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE);
1398 JDialog dialog = pane.createDialog(parentComponent, null);
1399 dialog.show();
1403 * This method shows a message dialog with the given message, title and
1404 * messageType.
1406 * @param parentComponent The component to find a frame in.
1407 * @param message The message displayed.
1408 * @param title The title of the dialog.
1409 * @param messageType The messageType.
1411 public static void showMessageDialog(Component parentComponent,
1412 Object message, String title,
1413 int messageType)
1415 JOptionPane pane = new JOptionPane(message, messageType);
1416 JDialog dialog = pane.createDialog(parentComponent, title);
1417 dialog.show();
1421 * This method shows a message dialog with the given message, title,
1422 * messageType and icon.
1424 * @param parentComponent The component to find a frame in.
1425 * @param message The message displayed.
1426 * @param title The title of the dialog.
1427 * @param messageType The messageType.
1428 * @param icon The icon displayed.
1430 public static void showMessageDialog(Component parentComponent,
1431 Object message, String title,
1432 int messageType, Icon icon)
1434 JOptionPane pane = new JOptionPane(message, messageType);
1435 pane.setIcon(icon);
1436 JDialog dialog = pane.createDialog(parentComponent, title);
1437 dialog.show();
1441 * This method shows an option dialog with the given message, title,
1442 * optionType, messageType, icon, options and initialValue. This method
1443 * returns the option that was selected.
1445 * @param parentComponent The component to find a frame in.
1446 * @param message The message displayed.
1447 * @param title The title of the dialog.
1448 * @param optionType The optionType.
1449 * @param messageType The messageType.
1450 * @param icon The icon displayed.
1451 * @param options The options to choose from.
1452 * @param initialValue The initial value.
1454 * @return The selected option.
1456 public static int showOptionDialog(Component parentComponent,
1457 Object message, String title,
1458 int optionType, int messageType,
1459 Icon icon, Object[] options,
1460 Object initialValue)
1462 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1463 options, initialValue);
1465 JDialog dialog = pane.createDialog(parentComponent, title);
1466 dialog.show();
1468 if (pane.getValue() instanceof Integer)
1469 return ((Integer) pane.getValue()).intValue();
1470 return -1;
1474 * This method resets the UI to the Look and Feel default.
1476 public void updateUI()
1478 setUI((OptionPaneUI) UIManager.getUI(this));
1482 * This method returns true if the key is a valid messageType.
1484 * @param key The key to check.
1486 * @return True if key is valid.
1488 private boolean validMessageType(int key)
1490 switch (key)
1492 case ERROR_MESSAGE:
1493 case INFORMATION_MESSAGE:
1494 case PLAIN_MESSAGE:
1495 case QUESTION_MESSAGE:
1496 case WARNING_MESSAGE:
1497 return true;
1499 return false;
1503 * This method returns true if the key is a valid optionType.
1505 * @param key The key to check.
1507 * @return True if key is valid.
1509 private boolean validOptionType(int key)
1511 switch (key)
1513 case DEFAULT_OPTION:
1514 case OK_CANCEL_OPTION:
1515 case YES_NO_CANCEL_OPTION:
1516 case YES_NO_OPTION:
1517 return true;
1519 return false;
1523 * This helper method makes the JInternalFrame wait until it is notified by
1524 * an InternalFrameClosing event. This method also adds the given
1525 * JOptionPane to the JInternalFrame and sizes it according to the
1526 * JInternalFrame's preferred size.
1528 * @param f The JInternalFrame to make modal.
1530 private static void startModal(JInternalFrame f)
1532 synchronized (f)
1534 final JInternalFrame tmp = f;
1535 tmp.toFront();
1537 f.addInternalFrameListener(new InternalFrameAdapter()
1539 public void internalFrameClosed(InternalFrameEvent e)
1541 synchronized (tmp)
1543 tmp.removeInternalFrameListener(this);
1544 tmp.notifyAll();
1550 while (! f.isClosed())
1551 f.wait();
1553 catch (InterruptedException ignored)
1555 // Ignore this Exception.