PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/java-1.7.0-openjdk/openjdk/jdk/src/share/classes/javax/swing/JOptionPane.java

#
Java | 2601 lines | 916 code | 168 blank | 1517 comment | 191 complexity | 9fd606c2c445ab0970b5928fceeef25d MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package javax.swing;
  26. import java.awt.BorderLayout;
  27. import java.awt.Component;
  28. import java.awt.Container;
  29. import java.awt.Dialog;
  30. import java.awt.Dimension;
  31. import java.awt.KeyboardFocusManager;
  32. import java.awt.Frame;
  33. import java.awt.Point;
  34. import java.awt.HeadlessException;
  35. import java.awt.Window;
  36. import java.beans.PropertyChangeEvent;
  37. import java.beans.PropertyChangeListener;
  38. import java.awt.event.WindowListener;
  39. import java.awt.event.WindowAdapter;
  40. import java.awt.event.WindowEvent;
  41. import java.awt.event.ComponentAdapter;
  42. import java.awt.event.ComponentEvent;
  43. import java.io.IOException;
  44. import java.io.ObjectInputStream;
  45. import java.io.ObjectOutputStream;
  46. import java.io.Serializable;
  47. import java.lang.reflect.Method;
  48. import java.lang.reflect.InvocationTargetException;
  49. import java.security.AccessController;
  50. import java.security.PrivilegedAction;
  51. import java.util.Vector;
  52. import javax.swing.plaf.OptionPaneUI;
  53. import javax.swing.event.InternalFrameEvent;
  54. import javax.swing.event.InternalFrameAdapter;
  55. import javax.accessibility.*;
  56. import static javax.swing.ClientPropertyKey.PopupFactory_FORCE_HEAVYWEIGHT_POPUP;
  57. /**
  58. * <code>JOptionPane</code> makes it easy to pop up a standard dialog box that
  59. * prompts users for a value or informs them of something.
  60. * For information about using <code>JOptionPane</code>, see
  61. * <a
  62. href="http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html">How to Make Dialogs</a>,
  63. * a section in <em>The Java Tutorial</em>.
  64. *
  65. * <p>
  66. *
  67. * While the <code>JOptionPane</code>
  68. * class may appear complex because of the large number of methods, almost
  69. * all uses of this class are one-line calls to one of the static
  70. * <code>showXxxDialog</code> methods shown below:
  71. * <blockquote>
  72. *
  73. *
  74. * <table border=1 summary="Common JOptionPane method names and their descriptions">
  75. * <tr>
  76. * <th>Method Name</th>
  77. * <th>Description</th>
  78. * </tr>
  79. * <tr>
  80. * <td>showConfirmDialog</td>
  81. * <td>Asks a confirming question, like yes/no/cancel.</td>
  82. * </tr>
  83. * <tr>
  84. * <td>showInputDialog</td>
  85. * <td>Prompt for some input.</td>
  86. * </tr>
  87. * <tr>
  88. * <td>showMessageDialog</td>
  89. * <td>Tell the user about something that has happened.</td>
  90. * </tr>
  91. * <tr>
  92. * <td>showOptionDialog</td>
  93. * <td>The Grand Unification of the above three.</td>
  94. * </tr>
  95. * </table>
  96. *
  97. * </blockquote>
  98. * Each of these methods also comes in a <code>showInternalXXX</code>
  99. * flavor, which uses an internal frame to hold the dialog box (see
  100. * {@link JInternalFrame}).
  101. * Multiple convenience methods have also been defined -- overloaded
  102. * versions of the basic methods that use different parameter lists.
  103. * <p>
  104. * All dialogs are modal. Each <code>showXxxDialog</code> method blocks
  105. * the caller until the user's interaction is complete.
  106. * <p>
  107. *
  108. * <table cellspacing=6 cellpadding=4 border=0 align=right summary="layout">
  109. * <tr>
  110. * <td bgcolor=#FFe0d0 rowspan=2>icon</td>
  111. * <td bgcolor=#FFe0d0>message</td>
  112. * </tr>
  113. * <tr>
  114. * <td bgcolor=#FFe0d0>input value</td>
  115. * </tr>
  116. * <tr>
  117. * <td bgcolor=#FFe0d0 colspan=2>option buttons</td>
  118. * </tr>
  119. * </table>
  120. *
  121. * The basic appearance of one of these dialog boxes is generally
  122. * similar to the picture at the right, although the various
  123. * look-and-feels are
  124. * ultimately responsible for the final result. In particular, the
  125. * look-and-feels will adjust the layout to accommodate the option pane's
  126. * <code>ComponentOrientation</code> property.
  127. * <br clear=all>
  128. * <p>
  129. * <b>Parameters:</b><br>
  130. * The parameters to these methods follow consistent patterns:
  131. * <blockquote>
  132. * <dl compact>
  133. * <dt>parentComponent<dd>
  134. * Defines the <code>Component</code> that is to be the parent of this
  135. * dialog box.
  136. * It is used in two ways: the <code>Frame</code> that contains
  137. * it is used as the <code>Frame</code>
  138. * parent for the dialog box, and its screen coordinates are used in
  139. * the placement of the dialog box. In general, the dialog box is placed
  140. * just below the component. This parameter may be <code>null</code>,
  141. * in which case a default <code>Frame</code> is used as the parent,
  142. * and the dialog will be
  143. * centered on the screen (depending on the L&F).
  144. * <dt><a name=message>message</a><dd>
  145. * A descriptive message to be placed in the dialog box.
  146. * In the most common usage, message is just a <code>String</code> or
  147. * <code>String</code> constant.
  148. * However, the type of this parameter is actually <code>Object</code>. Its
  149. * interpretation depends on its type:
  150. * <dl compact>
  151. * <dt>Object[]<dd>An array of objects is interpreted as a series of
  152. * messages (one per object) arranged in a vertical stack.
  153. * The interpretation is recursive -- each object in the
  154. * array is interpreted according to its type.
  155. * <dt>Component<dd>The <code>Component</code> is displayed in the dialog.
  156. * <dt>Icon<dd>The <code>Icon</code> is wrapped in a <code>JLabel</code>
  157. * and displayed in the dialog.
  158. * <dt>others<dd>The object is converted to a <code>String</code> by calling
  159. * its <code>toString</code> method. The result is wrapped in a
  160. * <code>JLabel</code> and displayed.
  161. * </dl>
  162. * <dt>messageType<dd>Defines the style of the message. The Look and Feel
  163. * manager may lay out the dialog differently depending on this value, and
  164. * will often provide a default icon. The possible values are:
  165. * <ul>
  166. * <li><code>ERROR_MESSAGE</code>
  167. * <li><code>INFORMATION_MESSAGE</code>
  168. * <li><code>WARNING_MESSAGE</code>
  169. * <li><code>QUESTION_MESSAGE</code>
  170. * <li><code>PLAIN_MESSAGE</code>
  171. * </ul>
  172. * <dt>optionType<dd>Defines the set of option buttons that appear at
  173. * the bottom of the dialog box:
  174. * <ul>
  175. * <li><code>DEFAULT_OPTION</code>
  176. * <li><code>YES_NO_OPTION</code>
  177. * <li><code>YES_NO_CANCEL_OPTION</code>
  178. * <li><code>OK_CANCEL_OPTION</code>
  179. * </ul>
  180. * You aren't limited to this set of option buttons. You can provide any
  181. * buttons you want using the options parameter.
  182. * <dt>options<dd>A more detailed description of the set of option buttons
  183. * that will appear at the bottom of the dialog box.
  184. * The usual value for the options parameter is an array of
  185. * <code>String</code>s. But
  186. * the parameter type is an array of <code>Objects</code>.
  187. * A button is created for each object depending on its type:
  188. * <dl compact>
  189. * <dt>Component<dd>The component is added to the button row directly.
  190. * <dt>Icon<dd>A <code>JButton</code> is created with this as its label.
  191. * <dt>other<dd>The <code>Object</code> is converted to a string using its
  192. * <code>toString</code> method and the result is used to
  193. * label a <code>JButton</code>.
  194. * </dl>
  195. * <dt>icon<dd>A decorative icon to be placed in the dialog box. A default
  196. * value for this is determined by the <code>messageType</code> parameter.
  197. * <dt>title<dd>The title for the dialog box.
  198. * <dt>initialValue<dd>The default selection (input value).
  199. * </dl>
  200. * </blockquote>
  201. * <p>
  202. * When the selection is changed, <code>setValue</code> is invoked,
  203. * which generates a <code>PropertyChangeEvent</code>.
  204. * <p>
  205. * If a <code>JOptionPane</code> has configured to all input
  206. * <code>setWantsInput</code>
  207. * the bound property <code>JOptionPane.INPUT_VALUE_PROPERTY</code>
  208. * can also be listened
  209. * to, to determine when the user has input or selected a value.
  210. * <p>
  211. * When one of the <code>showXxxDialog</code> methods returns an integer,
  212. * the possible values are:
  213. * <ul>
  214. * <li><code>YES_OPTION</code>
  215. * <li><code>NO_OPTION</code>
  216. * <li><code>CANCEL_OPTION</code>
  217. * <li><code>OK_OPTION</code>
  218. * <li><code>CLOSED_OPTION</code>
  219. * </ul>
  220. * <b>Examples:</b>
  221. * <dl>
  222. * <dt>Show an error dialog that displays the message, 'alert':
  223. * <dd><code>
  224. * JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
  225. * </code><p>
  226. * <dt>Show an internal information dialog with the message, 'information':
  227. * <dd><code>
  228. * JOptionPane.showInternalMessageDialog(frame, "information",<br>
  229. * <ul><ul>"information", JOptionPane.INFORMATION_MESSAGE);</ul></ul>
  230. * </code><p>
  231. * <dt>Show an information panel with the options yes/no and message 'choose one':
  232. * <dd><code>JOptionPane.showConfirmDialog(null,
  233. * <ul><ul>"choose one", "choose one", JOptionPane.YES_NO_OPTION);</ul></ul>
  234. * </code><p>
  235. * <dt>Show an internal information dialog with the options yes/no/cancel and
  236. * message 'please choose one' and title information:
  237. * <dd><code>JOptionPane.showInternalConfirmDialog(frame,
  238. * <ul><ul>"please choose one", "information",</ul></ul>
  239. * <ul><ul>JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);</ul></ul>
  240. * </code><p>
  241. * <dt>Show a warning dialog with the options OK, CANCEL, title 'Warning', and
  242. * message 'Click OK to continue':
  243. * <dd><code>
  244. * Object[] options = { "OK", "CANCEL" };<br>
  245. * JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
  246. * <ul><ul>JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,</ul></ul>
  247. * <ul><ul>null, options, options[0]);</ul></ul>
  248. * </code><p>
  249. * <dt>Show a dialog asking the user to type in a String:
  250. * <dd><code>
  251. * String inputValue = JOptionPane.showInputDialog("Please input a value");
  252. * </code><p>
  253. * <dt>Show a dialog asking the user to select a String:
  254. * <dd><code>
  255. * Object[] possibleValues = { "First", "Second", "Third" };<br>
  256. * Object selectedValue = JOptionPane.showInputDialog(null,
  257. * <ul><ul>"Choose one", "Input",</ul></ul>
  258. * <ul><ul>JOptionPane.INFORMATION_MESSAGE, null,</ul></ul>
  259. * <ul><ul>possibleValues, possibleValues[0]);</ul></ul>
  260. * </code><p>
  261. * </dl>
  262. * <b>Direct Use:</b><br>
  263. * To create and use an <code>JOptionPane</code> directly, the
  264. * standard pattern is roughly as follows:
  265. * <pre>
  266. * JOptionPane pane = new JOptionPane(<i>arguments</i>);
  267. * pane.set<i>.Xxxx(...); // Configure</i>
  268. * JDialog dialog = pane.createDialog(<i>parentComponent, title</i>);
  269. * dialog.show();
  270. * Object selectedValue = pane.getValue();
  271. * if(selectedValue == null)
  272. * return CLOSED_OPTION;
  273. * <i>//If there is <b>not</b> an array of option buttons:</i>
  274. * if(options == null) {
  275. * if(selectedValue instanceof Integer)
  276. * return ((Integer)selectedValue).intValue();
  277. * return CLOSED_OPTION;
  278. * }
  279. * <i>//If there is an array of option buttons:</i>
  280. * for(int counter = 0, maxCounter = options.length;
  281. * counter < maxCounter; counter++) {
  282. * if(options[counter].equals(selectedValue))
  283. * return counter;
  284. * }
  285. * return CLOSED_OPTION;
  286. * </pre>
  287. * <p>
  288. * <strong>Warning:</strong> Swing is not thread safe. For more
  289. * information see <a
  290. * href="package-summary.html#threading">Swing's Threading
  291. * Policy</a>.
  292. * <p>
  293. * <strong>Warning:</strong>
  294. * Serialized objects of this class will not be compatible with
  295. * future Swing releases. The current serialization support is
  296. * appropriate for short term storage or RMI between applications running
  297. * the same version of Swing. As of 1.4, support for long term storage
  298. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  299. * has been added to the <code>java.beans</code> package.
  300. * Please see {@link java.beans.XMLEncoder}.
  301. *
  302. * @see JInternalFrame
  303. *
  304. * @beaninfo
  305. * attribute: isContainer true
  306. * description: A component which implements standard dialog box controls.
  307. *
  308. * @author James Gosling
  309. * @author Scott Violet
  310. */
  311. public class JOptionPane extends JComponent implements Accessible
  312. {
  313. /**
  314. * @see #getUIClassID
  315. * @see #readObject
  316. */
  317. private static final String uiClassID = "OptionPaneUI";
  318. /**
  319. * Indicates that the user has not yet selected a value.
  320. */
  321. public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
  322. //
  323. // Option types
  324. //
  325. /**
  326. * Type meaning Look and Feel should not supply any options -- only
  327. * use the options from the <code>JOptionPane</code>.
  328. */
  329. public static final int DEFAULT_OPTION = -1;
  330. /** Type used for <code>showConfirmDialog</code>. */
  331. public static final int YES_NO_OPTION = 0;
  332. /** Type used for <code>showConfirmDialog</code>. */
  333. public static final int YES_NO_CANCEL_OPTION = 1;
  334. /** Type used for <code>showConfirmDialog</code>. */
  335. public static final int OK_CANCEL_OPTION = 2;
  336. //
  337. // Return values.
  338. //
  339. /** Return value from class method if YES is chosen. */
  340. public static final int YES_OPTION = 0;
  341. /** Return value from class method if NO is chosen. */
  342. public static final int NO_OPTION = 1;
  343. /** Return value from class method if CANCEL is chosen. */
  344. public static final int CANCEL_OPTION = 2;
  345. /** Return value form class method if OK is chosen. */
  346. public static final int OK_OPTION = 0;
  347. /** Return value from class method if user closes window without selecting
  348. * anything, more than likely this should be treated as either a
  349. * <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */
  350. public static final int CLOSED_OPTION = -1;
  351. //
  352. // Message types. Used by the UI to determine what icon to display,
  353. // and possibly what behavior to give based on the type.
  354. //
  355. /** Used for error messages. */
  356. public static final int ERROR_MESSAGE = 0;
  357. /** Used for information messages. */
  358. public static final int INFORMATION_MESSAGE = 1;
  359. /** Used for warning messages. */
  360. public static final int WARNING_MESSAGE = 2;
  361. /** Used for questions. */
  362. public static final int QUESTION_MESSAGE = 3;
  363. /** No icon is used. */
  364. public static final int PLAIN_MESSAGE = -1;
  365. /** Bound property name for <code>icon</code>. */
  366. public static final String ICON_PROPERTY = "icon";
  367. /** Bound property name for <code>message</code>. */
  368. public static final String MESSAGE_PROPERTY = "message";
  369. /** Bound property name for <code>value</code>. */
  370. public static final String VALUE_PROPERTY = "value";
  371. /** Bound property name for <code>option</code>. */
  372. public static final String OPTIONS_PROPERTY = "options";
  373. /** Bound property name for <code>initialValue</code>. */
  374. public static final String INITIAL_VALUE_PROPERTY = "initialValue";
  375. /** Bound property name for <code>type</code>. */
  376. public static final String MESSAGE_TYPE_PROPERTY = "messageType";
  377. /** Bound property name for <code>optionType</code>. */
  378. public static final String OPTION_TYPE_PROPERTY = "optionType";
  379. /** Bound property name for <code>selectionValues</code>. */
  380. public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
  381. /** Bound property name for <code>initialSelectionValue</code>. */
  382. public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
  383. /** Bound property name for <code>inputValue</code>. */
  384. public static final String INPUT_VALUE_PROPERTY = "inputValue";
  385. /** Bound property name for <code>wantsInput</code>. */
  386. public static final String WANTS_INPUT_PROPERTY = "wantsInput";
  387. /** Icon used in pane. */
  388. transient protected Icon icon;
  389. /** Message to display. */
  390. transient protected Object message;
  391. /** Options to display to the user. */
  392. transient protected Object[] options;
  393. /** Value that should be initially selected in <code>options</code>. */
  394. transient protected Object initialValue;
  395. /** Message type. */
  396. protected int messageType;
  397. /**
  398. * Option type, one of <code>DEFAULT_OPTION</code>,
  399. * <code>YES_NO_OPTION</code>,
  400. * <code>YES_NO_CANCEL_OPTION</code> or
  401. * <code>OK_CANCEL_OPTION</code>.
  402. */
  403. protected int optionType;
  404. /** Currently selected value, will be a valid option, or
  405. * <code>UNINITIALIZED_VALUE</code> or <code>null</code>. */
  406. transient protected Object value;
  407. /** Array of values the user can choose from. Look and feel will
  408. * provide the UI component to choose this from. */
  409. protected transient Object[] selectionValues;
  410. /** Value the user has input. */
  411. protected transient Object inputValue;
  412. /** Initial value to select in <code>selectionValues</code>. */
  413. protected transient Object initialSelectionValue;
  414. /** If true, a UI widget will be provided to the user to get input. */
  415. protected boolean wantsInput;
  416. /**
  417. * Shows a question-message dialog requesting input from the user. The
  418. * dialog uses the default frame, which usually means it is centered on
  419. * the screen.
  420. *
  421. * @param message the <code>Object</code> to display
  422. * @exception HeadlessException if
  423. * <code>GraphicsEnvironment.isHeadless</code> returns
  424. * <code>true</code>
  425. * @see java.awt.GraphicsEnvironment#isHeadless
  426. */
  427. public static String showInputDialog(Object message)
  428. throws HeadlessException {
  429. return showInputDialog(null, message);
  430. }
  431. /**
  432. * Shows a question-message dialog requesting input from the user, with
  433. * the input value initialized to <code>initialSelectionValue</code>. The
  434. * dialog uses the default frame, which usually means it is centered on
  435. * the screen.
  436. *
  437. * @param message the <code>Object</code> to display
  438. * @param initialSelectionValue the value used to initialize the input
  439. * field
  440. * @since 1.4
  441. */
  442. public static String showInputDialog(Object message, Object initialSelectionValue) {
  443. return showInputDialog(null, message, initialSelectionValue);
  444. }
  445. /**
  446. * Shows a question-message dialog requesting input from the user
  447. * parented to <code>parentComponent</code>.
  448. * The dialog is displayed on top of the <code>Component</code>'s
  449. * frame, and is usually positioned below the <code>Component</code>.
  450. *
  451. * @param parentComponent the parent <code>Component</code> for the
  452. * dialog
  453. * @param message the <code>Object</code> to display
  454. * @exception HeadlessException if
  455. * <code>GraphicsEnvironment.isHeadless</code> returns
  456. * <code>true</code>
  457. * @see java.awt.GraphicsEnvironment#isHeadless
  458. */
  459. public static String showInputDialog(Component parentComponent,
  460. Object message) throws HeadlessException {
  461. return showInputDialog(parentComponent, message, UIManager.getString(
  462. "OptionPane.inputDialogTitle", parentComponent), QUESTION_MESSAGE);
  463. }
  464. /**
  465. * Shows a question-message dialog requesting input from the user and
  466. * parented to <code>parentComponent</code>. The input value will be
  467. * initialized to <code>initialSelectionValue</code>.
  468. * The dialog is displayed on top of the <code>Component</code>'s
  469. * frame, and is usually positioned below the <code>Component</code>.
  470. *
  471. * @param parentComponent the parent <code>Component</code> for the
  472. * dialog
  473. * @param message the <code>Object</code> to display
  474. * @param initialSelectionValue the value used to initialize the input
  475. * field
  476. * @since 1.4
  477. */
  478. public static String showInputDialog(Component parentComponent, Object message,
  479. Object initialSelectionValue) {
  480. return (String)showInputDialog(parentComponent, message,
  481. UIManager.getString("OptionPane.inputDialogTitle",
  482. parentComponent), QUESTION_MESSAGE, null, null,
  483. initialSelectionValue);
  484. }
  485. /**
  486. * Shows a dialog requesting input from the user parented to
  487. * <code>parentComponent</code> with the dialog having the title
  488. * <code>title</code> and message type <code>messageType</code>.
  489. *
  490. * @param parentComponent the parent <code>Component</code> for the
  491. * dialog
  492. * @param message the <code>Object</code> to display
  493. * @param title the <code>String</code> to display in the dialog
  494. * title bar
  495. * @param messageType the type of message that is to be displayed:
  496. * <code>ERROR_MESSAGE</code>,
  497. * <code>INFORMATION_MESSAGE</code>,
  498. * <code>WARNING_MESSAGE</code>,
  499. * <code>QUESTION_MESSAGE</code>,
  500. * or <code>PLAIN_MESSAGE</code>
  501. * @exception HeadlessException if
  502. * <code>GraphicsEnvironment.isHeadless</code> returns
  503. * <code>true</code>
  504. * @see java.awt.GraphicsEnvironment#isHeadless
  505. */
  506. public static String showInputDialog(Component parentComponent,
  507. Object message, String title, int messageType)
  508. throws HeadlessException {
  509. return (String)showInputDialog(parentComponent, message, title,
  510. messageType, null, null, null);
  511. }
  512. /**
  513. * Prompts the user for input in a blocking dialog where the
  514. * initial selection, possible selections, and all other options can
  515. * be specified. The user will able to choose from
  516. * <code>selectionValues</code>, where <code>null</code> implies the
  517. * user can input
  518. * whatever they wish, usually by means of a <code>JTextField</code>.
  519. * <code>initialSelectionValue</code> is the initial value to prompt
  520. * the user with. It is up to the UI to decide how best to represent
  521. * the <code>selectionValues</code>, but usually a
  522. * <code>JComboBox</code>, <code>JList</code>, or
  523. * <code>JTextField</code> will be used.
  524. *
  525. * @param parentComponent the parent <code>Component</code> for the
  526. * dialog
  527. * @param message the <code>Object</code> to display
  528. * @param title the <code>String</code> to display in the
  529. * dialog title bar
  530. * @param messageType the type of message to be displayed:
  531. * <code>ERROR_MESSAGE</code>,
  532. * <code>INFORMATION_MESSAGE</code>,
  533. * <code>WARNING_MESSAGE</code>,
  534. * <code>QUESTION_MESSAGE</code>,
  535. * or <code>PLAIN_MESSAGE</code>
  536. * @param icon the <code>Icon</code> image to display
  537. * @param selectionValues an array of <code>Object</code>s that
  538. * gives the possible selections
  539. * @param initialSelectionValue the value used to initialize the input
  540. * field
  541. * @return user's input, or <code>null</code> meaning the user
  542. * canceled the input
  543. * @exception HeadlessException if
  544. * <code>GraphicsEnvironment.isHeadless</code> returns
  545. * <code>true</code>
  546. * @see java.awt.GraphicsEnvironment#isHeadless
  547. */
  548. public static Object showInputDialog(Component parentComponent,
  549. Object message, String title, int messageType, Icon icon,
  550. Object[] selectionValues, Object initialSelectionValue)
  551. throws HeadlessException {
  552. JOptionPane pane = new JOptionPane(message, messageType,
  553. OK_CANCEL_OPTION, icon,
  554. null, null);
  555. pane.setWantsInput(true);
  556. pane.setSelectionValues(selectionValues);
  557. pane.setInitialSelectionValue(initialSelectionValue);
  558. pane.setComponentOrientation(((parentComponent == null) ?
  559. getRootFrame() : parentComponent).getComponentOrientation());
  560. int style = styleFromMessageType(messageType);
  561. JDialog dialog = pane.createDialog(parentComponent, title, style);
  562. pane.selectInitialValue();
  563. dialog.show();
  564. dialog.dispose();
  565. Object value = pane.getInputValue();
  566. if (value == UNINITIALIZED_VALUE) {
  567. return null;
  568. }
  569. return value;
  570. }
  571. /**
  572. * Brings up an information-message dialog titled "Message".
  573. *
  574. * @param parentComponent determines the <code>Frame</code> in
  575. * which the dialog is displayed; if <code>null</code>,
  576. * or if the <code>parentComponent</code> has no
  577. * <code>Frame</code>, a default <code>Frame</code> is used
  578. * @param message the <code>Object</code> to display
  579. * @exception HeadlessException if
  580. * <code>GraphicsEnvironment.isHeadless</code> returns
  581. * <code>true</code>
  582. * @see java.awt.GraphicsEnvironment#isHeadless
  583. */
  584. public static void showMessageDialog(Component parentComponent,
  585. Object message) throws HeadlessException {
  586. showMessageDialog(parentComponent, message, UIManager.getString(
  587. "OptionPane.messageDialogTitle", parentComponent),
  588. INFORMATION_MESSAGE);
  589. }
  590. /**
  591. * Brings up a dialog that displays a message using a default
  592. * icon determined by the <code>messageType</code> parameter.
  593. *
  594. * @param parentComponent determines the <code>Frame</code>
  595. * in which the dialog is displayed; if <code>null</code>,
  596. * or if the <code>parentComponent</code> has no
  597. * <code>Frame</code>, a default <code>Frame</code> is used
  598. * @param message the <code>Object</code> to display
  599. * @param title the title string for the dialog
  600. * @param messageType the type of message to be displayed:
  601. * <code>ERROR_MESSAGE</code>,
  602. * <code>INFORMATION_MESSAGE</code>,
  603. * <code>WARNING_MESSAGE</code>,
  604. * <code>QUESTION_MESSAGE</code>,
  605. * or <code>PLAIN_MESSAGE</code>
  606. * @exception HeadlessException if
  607. * <code>GraphicsEnvironment.isHeadless</code> returns
  608. * <code>true</code>
  609. * @see java.awt.GraphicsEnvironment#isHeadless
  610. */
  611. public static void showMessageDialog(Component parentComponent,
  612. Object message, String title, int messageType)
  613. throws HeadlessException {
  614. showMessageDialog(parentComponent, message, title, messageType, null);
  615. }
  616. /**
  617. * Brings up a dialog displaying a message, specifying all parameters.
  618. *
  619. * @param parentComponent determines the <code>Frame</code> in which the
  620. * dialog is displayed; if <code>null</code>,
  621. * or if the <code>parentComponent</code> has no
  622. * <code>Frame</code>, a
  623. * default <code>Frame</code> is used
  624. * @param message the <code>Object</code> to display
  625. * @param title the title string for the dialog
  626. * @param messageType the type of message to be displayed:
  627. * <code>ERROR_MESSAGE</code>,
  628. * <code>INFORMATION_MESSAGE</code>,
  629. * <code>WARNING_MESSAGE</code>,
  630. * <code>QUESTION_MESSAGE</code>,
  631. * or <code>PLAIN_MESSAGE</code>
  632. * @param icon an icon to display in the dialog that helps the user
  633. * identify the kind of message that is being displayed
  634. * @exception HeadlessException if
  635. * <code>GraphicsEnvironment.isHeadless</code> returns
  636. * <code>true</code>
  637. * @see java.awt.GraphicsEnvironment#isHeadless
  638. */
  639. public static void showMessageDialog(Component parentComponent,
  640. Object message, String title, int messageType, Icon icon)
  641. throws HeadlessException {
  642. showOptionDialog(parentComponent, message, title, DEFAULT_OPTION,
  643. messageType, icon, null, null);
  644. }
  645. /**
  646. * Brings up a dialog with the options <i>Yes</i>,
  647. * <i>No</i> and <i>Cancel</i>; with the
  648. * title, <b>Select an Option</b>.
  649. *
  650. * @param parentComponent determines the <code>Frame</code> in which the
  651. * dialog is displayed; if <code>null</code>,
  652. * or if the <code>parentComponent</code> has no
  653. * <code>Frame</code>, a
  654. * default <code>Frame</code> is used
  655. * @param message the <code>Object</code> to display
  656. * @return an integer indicating the option selected by the user
  657. * @exception HeadlessException if
  658. * <code>GraphicsEnvironment.isHeadless</code> returns
  659. * <code>true</code>
  660. * @see java.awt.GraphicsEnvironment#isHeadless
  661. */
  662. public static int showConfirmDialog(Component parentComponent,
  663. Object message) throws HeadlessException {
  664. return showConfirmDialog(parentComponent, message,
  665. UIManager.getString("OptionPane.titleText"),
  666. YES_NO_CANCEL_OPTION);
  667. }
  668. /**
  669. * Brings up a dialog where the number of choices is determined
  670. * by the <code>optionType</code> parameter.
  671. *
  672. * @param parentComponent determines the <code>Frame</code> in which the
  673. * dialog is displayed; if <code>null</code>,
  674. * or if the <code>parentComponent</code> has no
  675. * <code>Frame</code>, a
  676. * default <code>Frame</code> is used
  677. * @param message the <code>Object</code> to display
  678. * @param title the title string for the dialog
  679. * @param optionType an int designating the options available on the dialog:
  680. * <code>YES_NO_OPTION</code>,
  681. * <code>YES_NO_CANCEL_OPTION</code>,
  682. * or <code>OK_CANCEL_OPTION</code>
  683. * @return an int indicating the option selected by the user
  684. * @exception HeadlessException if
  685. * <code>GraphicsEnvironment.isHeadless</code> returns
  686. * <code>true</code>
  687. * @see java.awt.GraphicsEnvironment#isHeadless
  688. */
  689. public static int showConfirmDialog(Component parentComponent,
  690. Object message, String title, int optionType)
  691. throws HeadlessException {
  692. return showConfirmDialog(parentComponent, message, title, optionType,
  693. QUESTION_MESSAGE);
  694. }
  695. /**
  696. * Brings up a dialog where the number of choices is determined
  697. * by the <code>optionType</code> parameter, where the
  698. * <code>messageType</code>
  699. * parameter determines the icon to display.
  700. * The <code>messageType</code> parameter is primarily used to supply
  701. * a default icon from the Look and Feel.
  702. *
  703. * @param parentComponent determines the <code>Frame</code> in
  704. * which the dialog is displayed; if <code>null</code>,
  705. * or if the <code>parentComponent</code> has no
  706. * <code>Frame</code>, a
  707. * default <code>Frame</code> is used.
  708. * @param message the <code>Object</code> to display
  709. * @param title the title string for the dialog
  710. * @param optionType an integer designating the options available
  711. * on the dialog: <code>YES_NO_OPTION</code>,
  712. * <code>YES_NO_CANCEL_OPTION</code>,
  713. * or <code>OK_CANCEL_OPTION</code>
  714. * @param messageType an integer designating the kind of message this is;
  715. * primarily used to determine the icon from the pluggable
  716. * Look and Feel: <code>ERROR_MESSAGE</code>,
  717. * <code>INFORMATION_MESSAGE</code>,
  718. * <code>WARNING_MESSAGE</code>,
  719. * <code>QUESTION_MESSAGE</code>,
  720. * or <code>PLAIN_MESSAGE</code>
  721. * @return an integer indicating the option selected by the user
  722. * @exception HeadlessException if
  723. * <code>GraphicsEnvironment.isHeadless</code> returns
  724. * <code>true</code>
  725. * @see java.awt.GraphicsEnvironment#isHeadless
  726. */
  727. public static int showConfirmDialog(Component parentComponent,
  728. Object message, String title, int optionType, int messageType)
  729. throws HeadlessException {
  730. return showConfirmDialog(parentComponent, message, title, optionType,
  731. messageType, null);
  732. }
  733. /**
  734. * Brings up a dialog with a specified icon, where the number of
  735. * choices is determined by the <code>optionType</code> parameter.
  736. * The <code>messageType</code> parameter is primarily used to supply
  737. * a default icon from the look and feel.
  738. *
  739. * @param parentComponent determines the <code>Frame</code> in which the
  740. * dialog is displayed; if <code>null</code>,
  741. * or if the <code>parentComponent</code> has no
  742. * <code>Frame</code>, a
  743. * default <code>Frame</code> is used
  744. * @param message the Object to display
  745. * @param title the title string for the dialog
  746. * @param optionType an int designating the options available on the dialog:
  747. * <code>YES_NO_OPTION</code>,
  748. * <code>YES_NO_CANCEL_OPTION</code>,
  749. * or <code>OK_CANCEL_OPTION</code>
  750. * @param messageType an int designating the kind of message this is,
  751. * primarily used to determine the icon from the pluggable
  752. * Look and Feel: <code>ERROR_MESSAGE</code>,
  753. * <code>INFORMATION_MESSAGE</code>,
  754. * <code>WARNING_MESSAGE</code>,
  755. * <code>QUESTION_MESSAGE</code>,
  756. * or <code>PLAIN_MESSAGE</code>
  757. * @param icon the icon to display in the dialog
  758. * @return an int indicating the option selected by the user
  759. * @exception HeadlessException if
  760. * <code>GraphicsEnvironment.isHeadless</code> returns
  761. * <code>true</code>
  762. * @see java.awt.GraphicsEnvironment#isHeadless
  763. */
  764. public static int showConfirmDialog(Component parentComponent,
  765. Object message, String title, int optionType,
  766. int messageType, Icon icon) throws HeadlessException {
  767. return showOptionDialog(parentComponent, message, title, optionType,
  768. messageType, icon, null, null);
  769. }
  770. /**
  771. * Brings up a dialog with a specified icon, where the initial
  772. * choice is determined by the <code>initialValue</code> parameter and
  773. * the number of choices is determined by the <code>optionType</code>
  774. * parameter.
  775. * <p>
  776. * If <code>optionType</code> is <code>YES_NO_OPTION</code>,
  777. * or <code>YES_NO_CANCEL_OPTION</code>
  778. * and the <code>options</code> parameter is <code>null</code>,
  779. * then the options are
  780. * supplied by the look and feel.
  781. * <p>
  782. * The <code>messageType</code> parameter is primarily used to supply
  783. * a default icon from the look and feel.
  784. *
  785. * @param parentComponent determines the <code>Frame</code>
  786. * in which the dialog is displayed; if
  787. * <code>null</code>, or if the
  788. * <code>parentComponent</code> has no
  789. * <code>Frame</code>, a
  790. * default <code>Frame</code> is used
  791. * @param message the <code>Object</code> to display
  792. * @param title the title string for the dialog
  793. * @param optionType an integer designating the options available on the
  794. * dialog: <code>DEFAULT_OPTION</code>,
  795. * <code>YES_NO_OPTION</code>,
  796. * <code>YES_NO_CANCEL_OPTION</code>,
  797. * or <code>OK_CANCEL_OPTION</code>
  798. * @param messageType an integer designating the kind of message this is,
  799. * primarily used to determine the icon from the
  800. * pluggable Look and Feel: <code>ERROR_MESSAGE</code>,
  801. * <code>INFORMATION_MESSAGE</code>,
  802. * <code>WARNING_MESSAGE</code>,
  803. * <code>QUESTION_MESSAGE</code>,
  804. * or <code>PLAIN_MESSAGE</code>
  805. * @param icon the icon to display in the dialog
  806. * @param options an array of objects indicating the possible choices
  807. * the user can make; if the objects are components, they
  808. * are rendered properly; non-<code>String</code>
  809. * objects are
  810. * rendered using their <code>toString</code> methods;
  811. * if this parameter is <code>null</code>,
  812. * the options are determined by the Look and Feel
  813. * @param initialValue the object that represents the default selection
  814. * for the dialog; only meaningful if <code>options</code>
  815. * is used; can be <code>null</code>
  816. * @return an integer indicating the option chosen by the user,
  817. * or <code>CLOSED_OPTION</code> if the user closed
  818. * the dialog
  819. * @exception HeadlessException if
  820. * <code>GraphicsEnvironment.isHeadless</code> returns
  821. * <code>true</code>
  822. * @see java.awt.GraphicsEnvironment#isHeadless
  823. */
  824. public static int showOptionDialog(Component parentComponent,
  825. Object message, String title, int optionType, int messageType,
  826. Icon icon, Object[] options, Object initialValue)
  827. throws HeadlessException {
  828. JOptionPane pane = new JOptionPane(message, messageType,
  829. optionType, icon,
  830. options, initialValue);
  831. pane.setInitialValue(initialValue);
  832. pane.setComponentOrientation(((parentComponent == null) ?
  833. getRootFrame() : parentComponent).getComponentOrientation());
  834. int style = styleFromMessageType(messageType);
  835. JDialog dialog = pane.createDialog(parentComponent, title, style);
  836. pane.selectInitialValue();
  837. dialog.show();
  838. dialog.dispose();
  839. Object selectedValue = pane.getValue();
  840. if(selectedValue == null)
  841. return CLOSED_OPTION;
  842. if(options == null) {
  843. if(selectedValue instanceof Integer)
  844. return ((Integer)selectedValue).intValue();
  845. return CLOSED_OPTION;
  846. }
  847. for(int counter = 0, maxCounter = options.length;
  848. counter < maxCounter; counter++) {
  849. if(options[counter].equals(selectedValue))
  850. return counter;
  851. }
  852. return CLOSED_OPTION;
  853. }
  854. /**
  855. * Creates and returns a new <code>JDialog</code> wrapping
  856. * <code>this</code> centered on the <code>parentComponent</code>
  857. * in the <code>parentComponent</code>'s frame.
  858. * <code>title</code> is the title of the returned dialog.
  859. * The returned <code>JDialog</code> will not be resizable by the
  860. * user, however programs can invoke <code>setResizable</code> on
  861. * the <code>JDialog</code> instance to change this property.
  862. * The returned <code>JDialog</code> will be set up such that
  863. * once it is closed, or the user clicks on one of the buttons,
  864. * the optionpane's value property will be set accordingly and
  865. * the dialog will be closed. Each time the dialog is made visible,
  866. * it will reset the option pane's value property to
  867. * <code>JOptionPane.UNINITIALIZED_VALUE</code> to ensure the
  868. * user's subsequent action closes the dialog properly.
  869. *
  870. * @param parentComponent determines the frame in which the dialog
  871. * is displayed; if the <code>parentComponent</code> has
  872. * no <code>Frame</code>, a default <code>Frame</code> is used
  873. * @param title the title string for the dialog
  874. * @return a new <code>JDialog</code> containing this instance
  875. * @exception HeadlessException if
  876. * <code>GraphicsEnvironment.isHeadless</code> returns
  877. * <code>true</code>
  878. * @see java.awt.GraphicsEnvironment#isHeadless
  879. */
  880. public JDialog createDialog(Component parentComponent, String title)
  881. throws HeadlessException {
  882. int style = styleFromMessageType(getMessageType());
  883. return createDialog(parentComponent, title, style);
  884. }
  885. /**
  886. * Creates and returns a new parentless <code>JDialog</code>
  887. * with the specified title.
  888. * The returned <code>JDialog</code> will not be resizable by the
  889. * user, however programs can invoke <code>setResizable</code> on
  890. * the <code>JDialog</code> instance to change this property.
  891. * The returned <code>JDialog</code> will be set up such that
  892. * once it is closed, or the user clicks on one of the buttons,
  893. * the optionpane's value property will be set accordingly and
  894. * the dialog will be closed. Each time the dialog is made visible,
  895. * it will reset the option pane's value property to
  896. * <code>JOptionPane.UNINITIALIZED_VALUE</code> to ensure the
  897. * user's subsequent action closes the dialog properly.
  898. *
  899. * @param title the title string for the dialog
  900. * @return a new <code>JDialog</code> containing this instance
  901. * @exception HeadlessException if
  902. * <code>GraphicsEnvironment.isHeadless</code> returns
  903. * <code>true</code>
  904. * @see java.awt.GraphicsEnvironment#isHeadless
  905. * @since 1.6
  906. */
  907. public JDialog createDialog(String title) throws HeadlessException {
  908. int style = styleFromMessageType(getMessageType());
  909. JDialog dialog = new JDialog((Dialog) null, title, true);
  910. initDialog(dialog, style, null);
  911. return dialog;
  912. }
  913. private JDialog createDialog(Component parentComponent, String title,
  914. int style)
  915. throws HeadlessException {
  916. final JDialog dialog;
  917. Window window = JOptionPane.getWindowForComponent(parentComponent);
  918. if (window instanceof Frame) {
  919. dialog = new JDialog((Frame)window, title, true);
  920. } else {
  921. dialog = new JDialog((Dialog)window, title, true);
  922. }
  923. if (window instanceof SwingUtilities.SharedOwnerFrame) {
  924. WindowListener ownerShutdownListener =
  925. SwingUtilities.getSharedOwnerFrameShutdownListener();
  926. dialog.addWindowListener(ownerShutdownListener);
  927. }
  928. initDialog(dialog, style, parentComponent);
  929. return dialog;
  930. }
  931. private void initDialog(final JDialog dialog, int style, Component parentComponent) {
  932. dialog.setComponentOrientation(this.getComponentOrientation());
  933. Container contentPane = dialog.getContentPane();
  934. contentPane.setLayout(new BorderLayout());
  935. contentPane.add(this, BorderLayout.CENTER);
  936. dialog.setResizable(false);
  937. if (JDialog.isDefaultLookAndFeelDecorated()) {
  938. boolean supportsWindowDecorations =
  939. UIManager.getLookAndFeel().getSupportsWindowDecorations();
  940. if (supportsWindowDecorations) {
  941. dialog.setUndecorated(true);
  942. getRootPane().setWindowDecorationStyle(style);
  943. }
  944. }
  945. dialog.pack();
  946. dialog.setLocationRelativeTo(parentComponent);
  947. final PropertyChangeListener listener = new PropertyChangeListener() {
  948. public void propertyChange(PropertyChangeEvent event) {
  949. // Let the defaultCloseOperation handle the closing
  950. // if the user closed the window without selecting a button
  951. // (newValue = null in that case). Otherwise, close the dialog.
  952. if (dialog.isVisible() && event.getSource() == JOptionPane.this &&
  953. (event.getPropertyName().equals(VALUE_PROPERTY)) &&
  954. event.getNewValue() != null &&
  955. event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) {
  956. dialog.setVisible(false);
  957. }
  958. }
  959. };
  960. WindowAdapter adapter = new WindowAdapter() {
  961. private boolean gotFocus = false;
  962. public void windowClosing(WindowEvent we) {
  963. setValue(null);
  964. }
  965. public void windowClosed(WindowEvent e) {
  966. removePropertyChangeListener(listener);
  967. dialog.getContentPane().removeAll();
  968. }
  969. public void windowGainedFocus(WindowEvent we) {
  970. // Once window gets focus, set initial focus
  971. if (!gotFocus) {
  972. selectInitialValue();
  973. gotFocus = true;
  974. }
  975. }
  976. };
  977. dialog.addWindowListener(adapter);
  978. dialog.addWindowFocusListener(adapter);
  979. dialog.addComponentListener(new ComponentAdapter() {
  980. public void componentShown(ComponentEvent ce) {
  981. // reset value to ensure closing works properly
  982. setValue(JOptionPane.UNINITIALIZED_VALUE);
  983. }
  984. });
  985. addPropertyChangeListener(listener);
  986. }
  987. /**
  988. * Brings up an internal confirmation dialog panel. The dialog
  989. * is a information-message dialog titled "Message".
  990. *
  991. * @param parentComponent determines the <code>Frame</code>
  992. * in which the dialog is displayed; if <code>null</code>,
  993. * or if the <code>parentComponent</code> has no
  994. * <code>Frame</code>, a default <code>Frame</code> is used
  995. * @param message the object to display
  996. */
  997. public static void showInternalMessageDialog(Component parentComponent,
  998. Object message) {
  999. showInternalMessageDialog(parentComponent, message, UIManager.
  1000. getString("OptionPane.messageDialogTitle",
  1001. parentComponent), INFORMATION_MESSAGE);
  1002. }
  1003. /**
  1004. * Brings up an internal dialog panel that displays a message
  1005. * using a default icon determined by the <code>messageType</code>
  1006. * parameter.
  1007. *
  1008. * @param parentComponent determines the <code>Frame</code>
  1009. * in which the dialog is displayed; if <code>null</code>,
  1010. * or if the <code>parentComponent</code> has no
  1011. * <code>Frame</code>, a default <code>Frame</code> is used
  1012. * @param message the <code>Object</code> to display
  1013. * @param title the title string for the dialog
  1014. * @param messageType the type of message to be displayed:
  1015. * <code>ERROR_MESSAGE</code>,
  1016. * <code>INFORMATION_MESSAGE</code>,
  1017. * <code>WARNING_MESSAGE</code>,
  1018. * <code>QUESTION_MESSAGE</code>,
  1019. * or <code>PLAIN_MESSAGE</code>
  1020. */
  1021. public static void showInternalMessageDialog(Component parentComponent,
  1022. Object message, String title,
  1023. int messageType) {
  1024. showInternalMessageDialog(parentComponent, message, title, messageType,null);
  1025. }
  1026. /**
  1027. * Brings up an internal dialog panel displaying a message,
  1028. * specifying all parameters.
  1029. *
  1030. * @param parentComponent determines the <code>Frame</code>
  1031. * in which the dialog is displayed; if <code>null</code>,
  1032. * or if the <code>parentComponent</code> has no
  1033. * <code>Frame</code>, a default <code>Frame</code> is used
  1034. * @param message the <code>Object</code> to display
  1035. * @param title the title string for the dialog
  1036. * @param messageType the type of message to be displayed:
  1037. * <code>ERROR_MESSAGE</code>,
  1038. * <code>INFORMATION_MESSAGE</code>,
  1039. * <code>WARNING_MESSAGE</code>,
  1040. * <code>QUESTION_MESSAGE</code>,
  1041. * or <code>PLAIN_MESSAGE</code>
  1042. * @param icon an icon to display in the dialog that helps the user
  1043. * identify the kind of message that is being displayed
  1044. */
  1045. public static void showInternalMessageDialog(Component parentComponent,
  1046. Object message,
  1047. String title, int messageType,
  1048. Icon icon){
  1049. showInternalOptionDialog(parentComponent, message, title, DEFAULT_OPTION,
  1050. messageType, icon, null, null);
  1051. }
  1052. /**
  1053. * Brings up an internal dialog panel with the options <i>Yes</i>, <i>No</i>
  1054. * and …

Large files files are truncated, but you can click here to view the full file