1. /*
  2. * @(#)JOptionPane.java 1.47 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.BorderLayout;
  9. import java.awt.Component;
  10. import java.awt.Container;
  11. import java.awt.Dialog;
  12. import java.awt.Dimension;
  13. import java.awt.Frame;
  14. import java.awt.Toolkit;
  15. import java.beans.PropertyChangeEvent;
  16. import java.beans.PropertyChangeListener;
  17. import java.awt.event.WindowAdapter;
  18. import java.awt.event.WindowEvent;
  19. import java.io.IOException;
  20. import java.io.ObjectInputStream;
  21. import java.io.ObjectOutputStream;
  22. import java.io.Serializable;
  23. import java.util.Vector;
  24. import javax.swing.plaf.OptionPaneUI;
  25. import javax.accessibility.*;
  26. /**
  27. * JOptionPane makes it easy to pop up a standard dialog box that
  28. * prompts users for a value or informs them of something. While the
  29. * class may appear complex because of the large number of methods, almost
  30. * all uses of this class are one-line calls to one of the static
  31. * <code>showXxxDialog</code> methods shown below:
  32. * <blockquote>
  33. * <table>
  34. * <tr align=top><td>showConfirmDialog<td>Asks a confirming question,
  35. * like yes/no/cancel.
  36. * <tr align=top><td>showInputDialog<td>Prompt for some input.
  37. * <tr align=top><td>showMessageDialog<td>Tell the user about something
  38. * that has happened.
  39. * <tr align=top><td>showOptionDialog<td>The Grand Unification of the above three.
  40. * </table>
  41. * </blockquote>
  42. * Each of these methods also comes in a <code>showInternalXXX</code>
  43. * flavor, which uses an internal frame to hold the dialog box (see
  44. * {@link JInternalFrame}).
  45. * Multiple convenience methods have also been defined -- overloaded
  46. * versions of the basic methods that use different parameter lists.
  47. * <p>
  48. * All dialogs are modal. Each <code>showXxxDialog</code> method blocks
  49. * the current thread until the user's interaction is complete.
  50. * <p>
  51. * <table cellspacing=6 cellpadding=4 border=0 align=right>
  52. * <tr>
  53. * <td bgcolor=#FFe0d0 rowspan=2>
  54. * icon
  55. * <td bgcolor=#FFe0d0>
  56. * message
  57. * <tr>
  58. * <td bgcolor=#FFe0d0>
  59. * input value
  60. * <tr>
  61. * <td bgcolor=#FFe0d0 colspan=2>
  62. * option buttons
  63. * </table>
  64. * The basic appearance of one of these dialog boxes is generally
  65. * similar to the picture at the right, although the various look-and-feels are
  66. * ultimatly responsible for the final result.
  67. * <br clear=all>
  68. * <p>
  69. * <b>Parameters:</b><br>
  70. * The parameters to these methods follow consistent patterns:
  71. * <blockquote>
  72. * <dl compact>
  73. * <dt>parentComponent<dd>
  74. * Defines the Component that is to be the parent of this dialog box.
  75. * It is used in two ways: the Frame that contains it is used as the Frame
  76. * parent for the dialog box, and its screen coordinates are used in
  77. * the placement of the dialog box. In general, the dialog box is placed
  78. * just below the component. This parameter may be null, in which case
  79. * a default Frame is used as the parent, and the dialog will be
  80. * centered on the screen (depending on the L&F).
  81. * <dt><a name=message>message</a><dd>
  82. * A descriptive message to be placed in the dialog box.
  83. * In the most common usage, message is just a String or String constant.
  84. * However, the type of this parameter is actually Object. It's
  85. * interpretation depends on its type:
  86. * <dl compact>
  87. * <dt>Object[]<dd>An array of objects is interpreted as a series of
  88. * messages (one per object) arranged in a vertical stack.
  89. * The interpretation is recursive -- each object in the
  90. * array is interpreted according to its type.
  91. * <dt>Component<dd>The Component is displayed in the dialog.
  92. * <dt>Icon<dd>The Icon is wrapped in a JLabel and displayed in the dialog.
  93. * <dt>others<dd>The object is converted to a String by calling its
  94. * <code>toString</code> method. The result is wrapped in a
  95. * JLabel and displayed.
  96. * </dl>
  97. * <dt>messageType<dd>Defines the style of the message. The look&feel
  98. * manager may lay out the dialog differently depending on this value, and
  99. * will often provide a default icon. The possible values are:
  100. * <ul>
  101. * <li>ERROR_MESSAGE
  102. * <li>INFORMATION_MESSAGE
  103. * <li>WARNING_MESSAGE
  104. * <li>QUESTION_MESSAGE
  105. * <li>PLAIN_MESSAGE
  106. * </ul>
  107. * <dt>optionType<dd>Defines the set of option buttons that appear at
  108. * the bottom of the dialog box:
  109. * <ul>
  110. * <li>DEFAULT_OPTION
  111. * <li>YES_NO_OPTION
  112. * <li>YES_NO_CANCEL_OPTION
  113. * <li>OK_CANCEL_OPTION
  114. * </ul>
  115. * You aren't limited to this set of option buttons. You can provide any
  116. * buttons you want using the options parameter.
  117. * <dt>options<dd>A more detailed description of the set of option buttons
  118. * that will appear at the bottom of the dialog box.
  119. * The usual value for the options parameter is an array of Strings. But
  120. * the parameter type is an array of Objects. A button is created for each
  121. * object depending on it's type:
  122. * <dl compact>
  123. * <dt>Component<dd>The component is added to the button row directly.
  124. * <dt>Icon<dd>A JButton is created with this as its label.
  125. * <dt>other<dd>The Object is converted to a string using its
  126. * <code>toString</code> method and the result is used to
  127. * label a JButton.
  128. * </dl>
  129. * <dt>icon<dd>A decorative icon to be placed in the dialog box. A default
  130. * value for this is determined by the messageType parameter.
  131. * <dt>title<dd>The title for the dialog box.
  132. * <dt>initialValue<dd>The default selection (input value).
  133. * </dl>
  134. * </blockquote>
  135. * <p>
  136. * When the selection is changed, <code>setValue</code> is invoked,
  137. * which generates a PropertyChangeEvent.
  138. * <p>
  139. * If a JOptionPane has configured to all input <code>setWantsInput</code>
  140. * the bound property JOptionPane.INPUT_VALUE_PROPERTY can also be listened
  141. * to, to determine when the user has input or selected a value.
  142. * <p>
  143. * When one of the <code>showXxxDialog</code> methods returns an integer,
  144. * the possible values are:<pre>
  145. * YES_OPTION,
  146. * NO_OPTION,
  147. * CANCEL_OPTION,
  148. * OK_OPTION, or
  149. * CLOSED_OPTION.
  150. * </pre>
  151. * <b>Examples:</b>
  152. * <dl>
  153. * <dt>Show an error dialog that displays the message, 'alert':
  154. * <dd><code>
  155. * JOptionPane.showMessageDialog(null, "alert", "alert", ERROR_MESSAGE);
  156. * </code><p>
  157. * <dt>Show an internal information dialog with the message, 'information':
  158. * <dd><code>
  159. * JOptionPane.showInternalMessageDialog(frame, INFORMATION_MESSAGE,<br>
  160. * <ul><ul>"information", "information");</ul></ul>
  161. * </code><p>
  162. * <dt>Show an information panel with the options yes/no and message 'choose one':
  163. * <dd><code>JOptionPane.showConfirmDialog(null,
  164. * <ul><ul>"choose one", "choose one", YES_NO_OPTION);</ul></ul>
  165. * </code><p>
  166. * <dt>Show an internal information dialog with the options yes/no/cancel and
  167. * message 'please choose one' and title information:
  168. * <dd><code>JOptionPane.showInternalConfirmDialog(frame,
  169. * <ul><ul>"please choose one", "information",</ul></ul>
  170. * <ul><ul>YES_NO_CANCEL_OPTION, INFORMATION_MESSAGE);</ul></ul>
  171. * </code><p>
  172. * <dt>Show a warning dialog with the options OK, CANCEL, title 'Warning', and
  173. * message 'Click OK to continue':
  174. * <dd><code>
  175. * Object[] options = { "OK", "CANCEL" };<br>
  176. * JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
  177. * <ul><ul>DEFAULT_OPTION, WARNING_MESSAGE,</ul></ul>
  178. * <ul><ul>null, options, options[0]);</ul></ul>
  179. * </code><p>
  180. * <dt>Show a dialog asking the user to type in a String:
  181. * <dd><code>
  182. * String inputValue = JOptionPane.showInputDialog("Please input a value");
  183. * </code><p>
  184. * <dt>Show a dialog asking the user to select a String:
  185. * <dd><code>
  186. * Object[] possibleValues = { "First", "Second", "Third" };<br>
  187. * Object selectedValue = JOptionPane.showInputDialog(null,
  188. * <ul><ul>"Choose one", "Input",</ul></ul>
  189. * <ul><ul>JOptionPane.INFORMATION_MESSAGE, null,</ul></ul>
  190. * <ul><ul>possibleValues, possibleValues[0]);</ul></ul>
  191. * </code><p>
  192. * </dl>
  193. * <b>Direct Use:</b><br>
  194. * To create and use an JOptionPane directly, the
  195. * standard pattern is roughly as follows:
  196. * <pre>
  197. * JOptionPane pane = new JOptionPane(<i>arguments</i>);
  198. * pane.set<i>.Xxxx(...); // Configure</i>
  199. * JDialog dialog = pane.createDialog(<i>parentComponent, title</i>);
  200. * dialog.show();
  201. * Object selectedValue = pane.getValue();
  202. * if(selectedValue == null)
  203. * return CLOSED_OPTION;
  204. * <i>//If there is <b>not</b> an array of option buttons:</i>
  205. * if(options == null) {
  206. * if(selectedValue instanceof Integer)
  207. * return ((Integer)selectedValue).intValue();
  208. * return CLOSED_OPTION;
  209. * }
  210. * <i>//If there is an array of option buttons:</i>
  211. * for(int counter = 0, maxCounter = options.length;
  212. * counter < maxCounter; counter++) {
  213. * if(options[counter].equals(selectedValue))
  214. * return counter;
  215. * }
  216. * return CLOSED_OPTION;
  217. * </pre>
  218. * <p>
  219. * For the keyboard keys used by this component in the standard Look and
  220. * Feel (L&F) renditions, see the
  221. * <a href="doc-files/Key-Index.html#JOptionPane">JOptionPane</a> key assignments.
  222. * <p>
  223. * <strong>Warning:</strong>
  224. * Serialized objects of this class will not be compatible with
  225. * future Swing releases. The current serialization support is appropriate
  226. * for short term storage or RMI between applications running the same
  227. * version of Swing. A future release of Swing will provide support for
  228. * long term persistence.
  229. *
  230. * @see JInternalFrame
  231. *
  232. * @beaninfo
  233. * attribute: isContainer true
  234. * description: A component which implements standard dialog box controls.
  235. *
  236. * @version 1.47 11/29/01
  237. * @author James Gosling
  238. * @author Scott Violet
  239. */
  240. public class JOptionPane extends JComponent implements Accessible
  241. {
  242. /**
  243. * @see #getUIClassID
  244. * @see #readObject
  245. */
  246. private static final String uiClassID = "OptionPaneUI";
  247. /**
  248. * Indicates that the user has not yet selected a value.
  249. */
  250. public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
  251. //
  252. // Option types
  253. //
  254. /**
  255. * Type meaning look and feel should not supply any options -- only
  256. * use the options from the JOptionPane.
  257. */
  258. public static final int DEFAULT_OPTION = -1;
  259. /** Type used for showConfirmDialog. */
  260. public static final int YES_NO_OPTION = 0;
  261. /** Type used for showConfirmDialog. */
  262. public static final int YES_NO_CANCEL_OPTION = 1;
  263. /** Type used for showConfirmDialog. */
  264. public static final int OK_CANCEL_OPTION = 2;
  265. //
  266. // Return values.
  267. //
  268. /** Return value from class method if YES is chosen. */
  269. public static final int YES_OPTION = 0;
  270. /** Return value from class method if NO is chosen. */
  271. public static final int NO_OPTION = 1;
  272. /** Return value from class method if CANCEL is chosen. */
  273. public static final int CANCEL_OPTION = 2;
  274. /** Return value form class method if OK is chosen. */
  275. public static final int OK_OPTION = 0;
  276. /** Return value from class method if user closes window without selecting
  277. * anything, more than likely this should be treated as either a
  278. * CANCEL_OPTION or NO_OPTION. */
  279. public static final int CLOSED_OPTION = -1;
  280. //
  281. // Message types. Used by the UI to determine what icon to display,
  282. // and possibly what behavior to give based on the type.
  283. //
  284. /** Used for error messages. */
  285. public static final int ERROR_MESSAGE = 0;
  286. /** Used for information messages. */
  287. public static final int INFORMATION_MESSAGE = 1;
  288. /** Used for warning messages. */
  289. public static final int WARNING_MESSAGE = 2;
  290. /** Used for questions. */
  291. public static final int QUESTION_MESSAGE = 3;
  292. /** No icon is used. */
  293. public static final int PLAIN_MESSAGE = -1;
  294. /** Bound property name for icon. */
  295. public static final String ICON_PROPERTY = "icon";
  296. /** Bound property name for message. */
  297. public static final String MESSAGE_PROPERTY = "message";
  298. /** Bounds property name for value. */
  299. public static final String VALUE_PROPERTY = "value";
  300. /** Bounds property namer for option. */
  301. public static final String OPTIONS_PROPERTY = "options";
  302. /** Bounds property name for initialValue. */
  303. public static final String INITIAL_VALUE_PROPERTY = "initialValue";
  304. /** Bounds property name for type. */
  305. public static final String MESSAGE_TYPE_PROPERTY = "messageType";
  306. /** Bound property name for optionType. */
  307. public static final String OPTION_TYPE_PROPERTY = "optionType";
  308. /** Bound property name for selectionValues. */
  309. public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
  310. /** Bound property name for initialSelectionValue. */
  311. public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
  312. /** Bound property name for inputValue. */
  313. public static final String INPUT_VALUE_PROPERTY = "inputValue";
  314. /** Bound property name for wantsInput. */
  315. public static final String WANTS_INPUT_PROPERTY = "wantsInput";
  316. /** Icon used in pane. */
  317. transient protected Icon icon;
  318. /** Message to display. */
  319. transient protected Object message;
  320. /** Options to display to the user. */
  321. transient protected Object[] options;
  322. /** Value that should be initialy selected in options. */
  323. transient protected Object initialValue;
  324. /** Message type. */
  325. protected int messageType;
  326. /** Option type, one of DEFAULT_OPTION, YES_NO_OPTION,
  327. * YES_NO_CANCEL_OPTION or OK_CANCEL_OPTION. */
  328. protected int optionType;
  329. /** Currently selected value, will be a valid option, or
  330. * UNINITIALIZED_VALUE or null. */
  331. transient protected Object value;
  332. /** Array of values the user can choose from. Look and feel will
  333. * provide the UI component to choose this from. */
  334. protected transient Object[] selectionValues;
  335. /** Value the user has input. */
  336. protected transient Object inputValue;
  337. /** Initial value to select in selectionValues. */
  338. protected transient Object initialSelectionValue;
  339. /** If true, a UI widget will be provided to the user to get input. */
  340. protected boolean wantsInput;
  341. /**
  342. * Shows a question-message dialog requesting input from the user. The
  343. * dialog uses the default frame, which usually means it is centered on
  344. * the screen.
  345. *
  346. * @param message the Object to display
  347. */
  348. public static String showInputDialog(Object message) {
  349. return showInputDialog(null, message);
  350. }
  351. /**
  352. * Shows a question-message dialog requesting input from the user parented to
  353. * <code>parentComponent</code>. The dialog is displayed in the Component's
  354. * frame, and is usually positioned below the Component.
  355. *
  356. * @param parentComponent the parent Component for the dialog
  357. * @param message the Object to display
  358. */
  359. public static String showInputDialog(Component parentComponent, Object message){
  360. return showInputDialog(parentComponent, message, "Input", QUESTION_MESSAGE);
  361. }
  362. /**
  363. * Shows a dialog requesting input from the user parented to
  364. * <code>parentComponent</code> with the dialog having the title
  365. * <code>title</code> and message type <code>messageType</code>.
  366. *
  367. * @param parentComponent the parent Component for the dialog
  368. * @param message the Object to display
  369. * @param title the String to display in the dialog title bar
  370. * @param messageType the type of message that is to be displayed:
  371. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  372. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  373. */
  374. public static String showInputDialog(Component parentComponent, Object message,
  375. String title, int messageType) {
  376. return (String)showInputDialog(parentComponent, message, title,
  377. messageType, null, null, null);
  378. }
  379. /**
  380. * Prompts the user for input in a blocking dialog where the
  381. * initial selection, possible selections, and all other options can
  382. * be specified. The user will able to choose from
  383. * <code>selectionValues</code>, where null implies the user can input
  384. * whatever they wish, usually by means of a JTextField.
  385. * <code>initialSelectionValue</code> is the initial value to prompt
  386. * the user with. It is up to the UI to decide how best to represent
  387. * the <code>selectionValues</code>, but usually a JComboBox, JList, or
  388. * JTextField will be used.
  389. *
  390. * @param parentComponent the parent Component for the dialog
  391. * @param message the Object to display
  392. * @param title the String to display in the dialog title bar
  393. * @param messageType the type of message to be displayed:
  394. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  395. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  396. * @param icon the Icon image to display
  397. * @param selectionValues an array of Objects that gives the possible
  398. * selections
  399. * @param initialSelectionValue the value used to initialize the input
  400. * field
  401. * @return users input, or null meaning the user canceled the input
  402. */
  403. public static Object showInputDialog(Component parentComponent, Object message,
  404. String title, int messageType, Icon icon,
  405. Object[] selectionValues, Object initialSelectionValue) {
  406. JOptionPane pane = new JOptionPane(message, messageType,
  407. OK_CANCEL_OPTION, icon,
  408. null, null);
  409. // Workaround for bug#4135218
  410. pane.adjustPreferredSize();
  411. pane.setWantsInput(true);
  412. pane.setSelectionValues(selectionValues);
  413. pane.setInitialSelectionValue(initialSelectionValue);
  414. JDialog dialog = pane.createDialog(parentComponent, title);
  415. pane.selectInitialValue();
  416. dialog.show();
  417. Object value = pane.getInputValue();
  418. if(value == UNINITIALIZED_VALUE)
  419. return null;
  420. return value;
  421. }
  422. /**
  423. * Brings up a confirmation dialog -- a modal information-message dialog
  424. * titled "Confirm".
  425. *
  426. * @param parentComponent Determines the Frame in which the dialog is displayed.
  427. * If null, or if the parentComponent has no Frame, a
  428. * default Frame is used.
  429. * @param message The Object to display
  430. */
  431. public static void showMessageDialog(Component parentComponent, Object message) {
  432. showMessageDialog(parentComponent, message, "Message", INFORMATION_MESSAGE);
  433. }
  434. /**
  435. * Brings up a dialog that displays a message using a default
  436. * icon determined by the messageType parameter.
  437. *
  438. * @param parentComponent Determines the Frame in which the dialog is displayed.
  439. * If null, or if the parentComponent has no Frame, a
  440. * default Frame is used.
  441. * @param message The Object to display
  442. * @param title the title string for the dialog
  443. * @param messageType the type of message to be displayed:
  444. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  445. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  446. */
  447. public static void showMessageDialog(Component parentComponent, Object message,
  448. String title, int messageType) {
  449. showMessageDialog(parentComponent, message, title, messageType, null);
  450. }
  451. /**
  452. * Brings up a dialog displaying a message, specifying all parameters.
  453. *
  454. * @param parentComponent Determines the Frame in which the dialog is displayed.
  455. * If null, or if the parentComponent has no Frame, a
  456. * default Frame is used.
  457. * @param message The Object to display
  458. * @param title the title string for the dialog
  459. * @param messageType the type of message to be displayed:
  460. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  461. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  462. * @param icon an icon to display in the dialog that helps the user
  463. * identify the kind of message that is being displayed.
  464. */
  465. public static void showMessageDialog(Component parentComponent, Object message,
  466. String title, int messageType,
  467. Icon icon){
  468. showOptionDialog(parentComponent, message, title, DEFAULT_OPTION,
  469. messageType, icon, null, null);
  470. }
  471. /**
  472. * Brings up a modal dialog with the options Yes, No and Cancel; with the
  473. * title, "Select an Option".
  474. *
  475. * @param parentComponent Determines the Frame in which the dialog is displayed.
  476. * If null, or if the parentComponent has no Frame, a
  477. * default Frame is used.
  478. * @param message The Object to display
  479. * @return an int indicating the option selected by the user
  480. */
  481. public static int showConfirmDialog(Component parentComponent, Object message) {
  482. return showConfirmDialog(parentComponent, message, "Select an Option",
  483. YES_NO_CANCEL_OPTION);
  484. }
  485. /**
  486. * Brings up a modal dialog where the number of choices is determined
  487. * by the <code>optionType</code> parameter.
  488. *
  489. * @param parentComponent Determines the Frame in which the dialog is displayed.
  490. * If null, or if the parentComponent has no Frame, a
  491. * default Frame is used.
  492. * @param message The Object to display
  493. * @param title the title string for the dialog
  494. * @param optionType an int designating the options available on the dialog:
  495. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  496. * @return an int indicating the option selected by the user
  497. */
  498. public static int showConfirmDialog(Component parentComponent, Object message,
  499. String title, int optionType) {
  500. return showConfirmDialog(parentComponent, message, title, optionType,
  501. QUESTION_MESSAGE);
  502. }
  503. /**
  504. * Brings up a modal dialog where the number of choices is determined
  505. * by the <code>optionType</code> parameter, where the <code>messageType</code>
  506. * parameter determines the icon to display.
  507. * The <code>messageType</code> parameter is primarily used to supply
  508. * a default icon from the look and feel.
  509. *
  510. * @param parentComponent Determines the Frame in which the dialog is displayed.
  511. * If null, or if the parentComponent has no Frame, a
  512. * default Frame is used.
  513. * @param message The Object to display
  514. * @param title the title string for the dialog
  515. * @param optionType an int designating the options available on the dialog:
  516. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  517. * @param messageType an int designating the kind of message this is,
  518. * primarily used to determine the icon from the pluggable
  519. * look and feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  520. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE.
  521. * @return an int indicating the option selected by the user
  522. */
  523. public static int showConfirmDialog(Component parentComponent, Object message,
  524. String title, int optionType,
  525. int messageType) {
  526. return showConfirmDialog(parentComponent, message, title, optionType,
  527. messageType, null);
  528. }
  529. /**
  530. * Brings up a modal dialog with a specified icon, where the number of
  531. * choices is determined by the <code>optionType</code> parameter.
  532. * The <code>messageType</code> parameter is primarily used to supply
  533. * a default icon from the look and feel.
  534. *
  535. * @param parentComponent Determines the Frame in which the dialog is displayed.
  536. * If null, or if the parentComponent has no Frame, a
  537. * default Frame is used.
  538. * @param message The Object to display
  539. * @param title the title string for the dialog
  540. * @param optionType an int designating the options available on the dialog:
  541. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  542. * @param messageType an int designating the kind of message this is,
  543. * primarily used to determine the icon from the pluggable
  544. * look and feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  545. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE.
  546. * @param icon the icon to display in the dialog
  547. * @return an int indicating the option selected by the user
  548. */
  549. public static int showConfirmDialog(Component parentComponent, Object message,
  550. String title, int optionType,
  551. int messageType, Icon icon) {
  552. return showOptionDialog(parentComponent, message, title, optionType,
  553. messageType, icon, null, null);
  554. }
  555. /**
  556. * Brings up a modal dialog with a specified icon, where the initial
  557. * choice is dermined by the <code>initialValue</code> parameter and
  558. * the number of choices is determined by the <code>optionType</code>
  559. * parameter.
  560. * <p>
  561. * If <code>optionType</code> is YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  562. * and the <code>options</code> parameter is null, then the options are
  563. * supplied by the look and feel.
  564. * <p>
  565. * The <code>messageType</code> parameter is primarily used to supply
  566. * a default icon from the look and feel.
  567. *
  568. * @param parentComponent Determines the Frame in which the dialog is displayed.
  569. * If null, or if the parentComponent has no Frame, a
  570. * default Frame is used.
  571. * @param message The Object to display
  572. * @param title the title string for the dialog
  573. * @param optionType an int designating the options available on the dialog:
  574. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  575. * @param messageType an int designating the kind of message this is,
  576. * primarily used to determine the icon from the pluggable
  577. * look and feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  578. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE.
  579. * @param icon the icon to display in the dialog
  580. * @param options an array of objects indicating the possible choices
  581. * the user can make. If the objects are components, they
  582. * are rendered properly. Non-String objects are
  583. * rendered using their <code>toString</code> methods.
  584. * If this parameter is null, the options are determined
  585. * by the look and feel.
  586. * @param initialValue the object that represents the default selection
  587. * for the dialog
  588. * @return an int indicating the option chosen by the user,
  589. * or CLOSED_OPTION if the user closed the Dialog
  590. */
  591. public static int showOptionDialog(Component parentComponent, Object message,
  592. String title, int optionType,
  593. int messageType, Icon icon,
  594. Object[] options, Object initialValue) {
  595. JOptionPane pane = new JOptionPane(message, messageType,
  596. optionType, icon,
  597. options, initialValue);
  598. // Workaround for bug#4135218
  599. pane.adjustPreferredSize();
  600. pane.setInitialValue(initialValue);
  601. JDialog dialog = pane.createDialog(parentComponent, title);
  602. pane.selectInitialValue();
  603. dialog.show();
  604. Object selectedValue = pane.getValue();
  605. if(selectedValue == null)
  606. return CLOSED_OPTION;
  607. if(options == null) {
  608. if(selectedValue instanceof Integer)
  609. return ((Integer)selectedValue).intValue();
  610. return CLOSED_OPTION;
  611. }
  612. for(int counter = 0, maxCounter = options.length;
  613. counter < maxCounter; counter++) {
  614. if(options[counter].equals(selectedValue))
  615. return counter;
  616. }
  617. return CLOSED_OPTION;
  618. }
  619. /**
  620. * Creates and returns a new JDialog wrapping <code>this</code>
  621. * centered on the <code>parentComponent</code> in the
  622. * <code>parentComponent</code>'s frame.
  623. * <code>title</code> is the title of the returned dialog.
  624. * The returned JDialog will be set up such that once it is closed,
  625. * or the user clicks on the OK button, the dialog will be disposed
  626. * and closed.
  627. *Re if the parentComponent has no Frame, a
  628. * default Frame is used.
  629. * @param title the title string for the dialog
  630. * @return a new JDialog containing this instance
  631. */
  632. public JDialog createDialog(Component parentComponent, String title) {
  633. Frame frame = JOptionPane.getFrameForComponent(parentComponent);
  634. final JDialog dialog;
  635. String osName = System.getProperty("os.name");
  636. final boolean solarisWorkaround = (osName != null &&
  637. osName.indexOf("Solaris") != -1);
  638. if (solarisWorkaround) {
  639. // Workaround for bug in Solaris where modal dialog
  640. // disposal causes segv (4137962); this workaround exposes
  641. // bugs on win32, so only do it on Solaris
  642. //
  643. dialog = SwingUtilities.getRecycledModalDialog(frame, title);
  644. } else {
  645. dialog = new JDialog(frame, title, true);
  646. }
  647. Container contentPane = dialog.getContentPane();
  648. contentPane.setLayout(new BorderLayout());
  649. contentPane.add(this, BorderLayout.CENTER);
  650. dialog.pack();
  651. dialog.setLocationRelativeTo(parentComponent);
  652. dialog.addWindowListener(new WindowAdapter() {
  653. boolean gotFocus = false;
  654. public void windowClosing(WindowEvent we) {
  655. setValue(null);
  656. }
  657. public void windowActivated(WindowEvent we) {
  658. // Once window gets focus, set initial focus
  659. if (!gotFocus) {
  660. selectInitialValue();
  661. gotFocus = true;
  662. }
  663. }
  664. });
  665. addPropertyChangeListener(new PropertyChangeListener() {
  666. public void propertyChange(PropertyChangeEvent event) {
  667. if(dialog.isVisible() && event.getSource() == JOptionPane.this &&
  668. (event.getPropertyName().equals(VALUE_PROPERTY) ||
  669. event.getPropertyName().equals(INPUT_VALUE_PROPERTY))) {
  670. dialog.setVisible(false);
  671. if (solarisWorkaround) {
  672. // Workaround for bug in Solaris where modal dialog
  673. // disposal causes segv (4137962)
  674. SwingUtilities.recycleModalDialog(dialog);
  675. } else {
  676. dialog.dispose();
  677. }
  678. }
  679. }
  680. });
  681. return dialog;
  682. }
  683. /**
  684. * Brings up an internal confirmation dialog panel. The dialog
  685. * is a modal information-message dialog titled "Message".
  686. *
  687. * @param parentComponent Determines the Frame in which the dialog is displayed.
  688. * If null, or if the parentComponent has no Frame, a
  689. * default Frame is used.
  690. * @param message The object to display
  691. */
  692. public static void showInternalMessageDialog(Component parentComponent,
  693. Object message) {
  694. showInternalMessageDialog(parentComponent, message, "Message",
  695. INFORMATION_MESSAGE);
  696. }
  697. /**
  698. * Brings up an internal dialog panel that displays a message
  699. * using a default icon determined by the messageType parameter.
  700. *
  701. * @param parentComponent Determines the Frame in which the dialog is displayed.
  702. * If null, or if the parentComponent has no Frame, a
  703. * default Frame is used.
  704. * @param message The Object to display
  705. * @param title the title string for the dialog
  706. * @param messageType the type of message to be displayed:
  707. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  708. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  709. */
  710. public static void showInternalMessageDialog(Component parentComponent,
  711. Object message, String title,
  712. int messageType) {
  713. showInternalMessageDialog(parentComponent, message, title, messageType,null);
  714. }
  715. /**
  716. * Brings up an internal dialog panel displaying a message,
  717. * specifying all parameters.
  718. *
  719. * @param parentComponent Determines the Frame in which the dialog is displayed.
  720. * If null, or if the parentComponent has no Frame, a
  721. * default Frame is used.
  722. * @param message The Object to display
  723. * @param title the title string for the dialog
  724. * @param messageType the type of message to be displayed:
  725. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  726. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  727. * @param icon an icon to display in the dialog that helps the user
  728. * identify the kind of message that is being displayed.
  729. */
  730. public static void showInternalMessageDialog(Component parentComponent,
  731. Object message,
  732. String title, int messageType,
  733. Icon icon){
  734. showInternalOptionDialog(parentComponent, message, title, DEFAULT_OPTION,
  735. messageType, icon, null, null);
  736. }
  737. /**
  738. * Brings up an internal dialog panel with the options Yes, No
  739. * and Cancel; with the title, "Select an Option".
  740. *
  741. * @param parentComponent Determines the Frame in which the dialog is displayed.
  742. * If null, or if the parentComponent has no Frame, a
  743. * default Frame is used.
  744. * @param message The Object to display
  745. * @return an int indicating the option selected by the user
  746. */
  747. public static int showInternalConfirmDialog(Component parentComponent,
  748. Object message) {
  749. return showInternalConfirmDialog(parentComponent, message,
  750. "Select an Option", YES_NO_CANCEL_OPTION);
  751. }
  752. /**
  753. * Brings up a internal dialog panel where the number of choices
  754. * is determined by the <code>optionType</code> parameter.
  755. *
  756. * @param parentComponent Determines the Frame in which the dialog is displayed.
  757. * If null, or if the parentComponent has no Frame, a
  758. * default Frame is used.
  759. * @param message The object to display in the dialog. A Component object
  760. * is rendered as a Component. A String object is rendered
  761. * as a string. Other objects are converted to a String
  762. * using the <code>toString</code> method.
  763. * @param title the title string for the dialog
  764. * @param optionType an int designating the options available on the dialog:
  765. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  766. * @return an int indicating the option selected by the user
  767. */
  768. public static int showInternalConfirmDialog(Component parentComponent,
  769. Object message, String title,
  770. int optionType) {
  771. return showInternalConfirmDialog(parentComponent, message, title, optionType,
  772. QUESTION_MESSAGE);
  773. }
  774. /**
  775. * Brings up an internal dialog panel where the number of choices
  776. * is determined by the <code>optionType</code> parameter, where
  777. * the <code>messageType</code> parameter determines the icon to display.
  778. * The <code>messageType</code> parameter is primarily used to supply
  779. * a default icon from the look and feel.
  780. *
  781. * @param parentComponent Determines the Frame in which the dialog is displayed.
  782. * If null, or if the parentComponent has no Frame, a
  783. * default Frame is used.
  784. * @param message The object to display in the dialog. A Component object
  785. * is rendered as a Component. A String object is rendered
  786. * as a string. Other objects are converted to a String
  787. * using the <code>toString</code> method.
  788. * @param title the title string for the dialog
  789. * @param optionType an int designating the options available on the dialog:
  790. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  791. * @param messageType an int designating the kind of message this is,
  792. * primarily used to determine the icon from the pluggable
  793. * look and feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  794. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE.
  795. * @return an int indicating the option selected by the user
  796. */
  797. public static int showInternalConfirmDialog(Component parentComponent,
  798. Object message,
  799. String title, int optionType,
  800. int messageType) {
  801. return showInternalConfirmDialog(parentComponent, message, title, optionType,
  802. messageType, null);
  803. }
  804. /**
  805. * Brings up an internal dialog panel with a specified icon, where
  806. * the number of choices is determined by the <code>optionType</code>
  807. * parameter.
  808. * The <code>messageType</code> parameter is primarily used to supply
  809. * a default icon from the look and feel.
  810. *
  811. * @param parentComponent Determines the Frame in which the dialog is displayed.
  812. * If null, or if the parentComponent has no Frame, a
  813. * default Frame is used.
  814. * @param message The object to display in the dialog. A Component object
  815. * is rendered as a Component. A String object is rendered
  816. * as a string. Other objects are converted to a String
  817. * using the <code>toString</code> method.
  818. * @param title the title string for the dialog
  819. * @param optionType an int designating the options available on the dialog:
  820. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  821. * @param messageType an int designating the kind of message this is,
  822. * primarily used to determine the icon from the pluggable
  823. * look and feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  824. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE.
  825. * @param icon the icon to display in the dialog
  826. * @return an int indicating the option selected by the user
  827. */
  828. public static int showInternalConfirmDialog(Component parentComponent,
  829. Object message,
  830. String title, int optionType,
  831. int messageType, Icon icon) {
  832. return showInternalOptionDialog(parentComponent, message, title, optionType,
  833. messageType, icon, null, null);
  834. }
  835. /**
  836. * Brings up an internal dialog panel with a specified icon, where
  837. * the initial choice is dermined by the <code>initialValue</code>
  838. * parameter and the number of choices is determined by the
  839. * <code>optionType</code> parameter.
  840. * <p>
  841. * If <code>optionType</code> is YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  842. * and the <code>options</code> parameter is null, then the options are
  843. * supplied by the look and feel.
  844. * <p>
  845. * The <code>messageType</code> parameter is primarily used to supply
  846. * a default icon from the look and feel.
  847. *
  848. * @param parentComponent Determines the Frame in which the dialog is displayed.
  849. * If null, or if the parentComponent has no Frame, a
  850. * default Frame is used.
  851. * @param message The object to display in the dialog. A Component object
  852. * is rendered as a Component. A String object is rendered
  853. * as a string. Other objects are converted to a String
  854. * using the <code>toString</code> method.
  855. * @param title the title string for the dialog
  856. * @param optionType an int designating the options available on the dialog:
  857. * YES_NO_OPTION, or YES_NO_CANCEL_OPTION
  858. * @param messageType an int designating the kind of message this is,
  859. * primarily used to determine the icon from the pluggable
  860. * look and feel: ERROR_MESSAGE, INFORMATION_MESSAGE,
  861. * WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE.
  862. * @param icon the icon to display in the dialog
  863. * @param options an array of objects indicating the possible choices
  864. * the user can make. If the objects are components, they
  865. * are rendered properly. Non-String objects are
  866. * rendered using their <code>toString</code> methods.
  867. * If this parameter is null, the options are determined
  868. * by the look and feel.
  869. * @param initialValue the object that represents the default selection
  870. * for the dialog
  871. * @return an int indicating the option chosen by the user,
  872. * or CLOSED_OPTION if the user closed the Dialog
  873. */
  874. public static int showInternalOptionDialog(Component parentComponent,
  875. Object message,
  876. String title, int optionType,
  877. int messageType, Icon icon,
  878. Object[] options, Object initialValue) {
  879. JOptionPane pane = new JOptionPane(message, messageType,
  880. optionType, icon,
  881. options, initialValue);
  882. pane.setInitialValue(initialValue);
  883. JInternalFrame dialog = pane.createInternalFrame(parentComponent, title);
  884. pane.selectInitialValue();
  885. dialog.startModal();
  886. Object selectedValue = pane.getValue();
  887. if(selectedValue == null)
  888. return CLOSED_OPTION;
  889. if(options == null) {
  890. if(selectedValue instanceof Integer)
  891. return ((Integer)selectedValue).intValue();
  892. return CLOSED_OPTION;
  893. }
  894. for(int counter = 0, maxCounter = options.length;
  895. counter < maxCounter; counter++) {
  896. if(options[counter].equals(selectedValue))
  897. return counter;
  898. }
  899. return CLOSED_OPTION;
  900. }
  901. /**
  902. * Shows an internal question-message dialog requesting input from
  903. * the user parented to <code>parentComponent</code>. The dialog
  904. * is displayed in the Component's frame, and is usually positioned
  905. * below the Component.
  906. *
  907. * @param parentComponent the parent Component for the dialog
  908. * @param message the Object to display
  909. */
  910. public static String showInternalInputDialog(Component parentComponent,
  911. Object message) {
  912. return showInternalInputDialog(parentComponent, message, "Input",
  913. QUESTION_MESSAGE);
  914. }
  915. /**
  916. * Shows an internal dialog requesting input from the user parented
  917. * to <code>parentComponent</code> with the dialog having the title
  918. * <code>title</code> and message type <code>messageType</code>.
  919. *
  920. * @param parentComponent the parent Component for the dialog
  921. * @param message the Object to display
  922. * @param title the String to display in the dialog title bar
  923. * @param messageType the type of message that is to be displayed:
  924. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  925. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  926. */
  927. public static String showInternalInputDialog(Component parentComponent,
  928. Object message, String title, int messageType) {
  929. return (String)showInternalInputDialog(parentComponent, message, title,
  930. messageType, null, null, null);
  931. }
  932. /**
  933. * Prompts the user for input in a blocking internal dialog where
  934. * the initial selection, possible selections, and all other
  935. * options can be specified. The user will able to choose from
  936. * <code>selectionValues</code>, where null implies the user can input
  937. * whatever they wish, usually by means of a JTextField.
  938. * <code>initialSelectionValue</code> is the initial value to prompt
  939. * the user with. It is up to the UI to decide how best to represent
  940. * the <code>selectionValues</code>, but usually a JComboBox, JList, or
  941. * JTextField will be used.
  942. *
  943. * @param parentComponent the parent Component for the dialog
  944. * @param message the Object to display
  945. * @param title the String to display in the dialog title bar
  946. * @param messageType the type of message to be displayed:
  947. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  948. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  949. * @param icon the Icon image to display
  950. * @param selectionValues an array of Objects that gives the possible
  951. * selections
  952. * @param initialSelectionValue the value used to initialize the input
  953. * field
  954. * @return users input, or null meaning the user canceled the input
  955. */
  956. public static Object showInternalInputDialog(Component parentComponent,
  957. Object message, String title, int messageType, Icon icon,
  958. Object[] selectionValues, Object initialSelectionValue) {
  959. JOptionPane pane = new JOptionPane(message, messageType,
  960. OK_CANCEL_OPTION, icon,
  961. null, null);
  962. pane.setWantsInput(true);
  963. pane.setSelectionValues(selectionValues);
  964. pane.setInitialSelectionValue(initialSelectionValue);
  965. JInternalFrame dialog = pane.createInternalFrame(parentComponent, title);
  966. pane.selectInitialValue();
  967. dialog.startModal();
  968. Object value = pane.getInputValue();
  969. if(value == UNINITIALIZED_VALUE)
  970. return null;
  971. return (String)value;
  972. }
  973. /**
  974. * Creates and returns an instance of JInternalFrame.
  975. * The internal frame is created with the specified title,
  976. * and wrapping the JOptionPane. The returned JInternalFrame is
  977. * added to the JDesktopPane ancestor of parentComponent, or components
  978. * parent if one its ancestors isn't a JDesktopPane, or if parentComponent
  979. * doesn't have a parent then a <code>RuntimeException</code> is thrown.
  980. *
  981. * @param parentComponent the parent Component for the internal frame
  982. * @param title the String to display in the frame's title bar
  983. * @return a JInternalFrame containing a JOptionPane
  984. */
  985. public JInternalFrame createInternalFrame(Component parentComponent,
  986. String title) {
  987. Container parent = JOptionPane.
  988. getDesktopPaneForComponent(parentComponent);
  989. if(parent == null && (parentComponent == null ||
  990. (parent = parentComponent.getParent()) == null))
  991. throw new RuntimeException("JOptionPane: parentComponent does not have a valid parent");
  992. final JInternalFrame iFrame = new JInternalFrame(title, true, false,
  993. false, false);
  994. addPropertyChangeListener(new PropertyChangeListener() {
  995. public void propertyChange(PropertyChangeEvent event) {
  996. if(iFrame.isVisible() && event.getSource() == JOptionPane.this &&
  997. (event.getPropertyName().equals(VALUE_PROPERTY) ||
  998. event.getPropertyName().equals(INPUT_VALUE_PROPERTY))) {
  999. try {
  1000. iFrame.setClosed(true);
  1001. } catch (java.beans.PropertyVetoException e) {}
  1002. iFrame.setVisible(false);
  1003. iFrame.stopModal();
  1004. }
  1005. }
  1006. });
  1007. iFrame.getContentPane().add(this, BorderLayout.CENTER);
  1008. if(parent instanceof JDesktopPane) {
  1009. parent.add(iFrame, JLayeredPane.MODAL_LAYER);
  1010. } else {
  1011. parent.add(iFrame, BorderLayout.CENTER);
  1012. }
  1013. Dimension iFrameSize = iFrame.getPreferredSize();
  1014. Dimension rootSize = parent.getSize();
  1015. iFrame.setBounds((rootSize.width - iFrameSize.width) / 2,
  1016. (rootSize.height - iFrameSize.height) / 2,
  1017. iFrameSize.width, iFrameSize.height);
  1018. parent.validate();
  1019. try {
  1020. iFrame.setSelected(true);
  1021. } catch (java.beans.PropertyVetoException e) {}
  1022. return iFrame;
  1023. }
  1024. /**
  1025. * Returns the specified component's Frame.
  1026. *
  1027. * @param parentComponent the Component to check for a Frame
  1028. * @return the Frame that contains the component, or the default
  1029. * frame if the component is null, or does not have a valid
  1030. * Frame parent
  1031. */
  1032. public static Frame getFrameForComponent(Component parentComponent) {
  1033. if (parentComponent == null)
  1034. return getRootFrame();
  1035. if (parentComponent instanceof Frame)
  1036. return (Frame)parentComponent;
  1037. return JOptionPane.getFrameForComponent(parentComponent.getParent());
  1038. }
  1039. /**
  1040. * Returns the specified component's desktop pane.
  1041. *
  1042. * @param parentComponent the Component to check for a desktop
  1043. * @return the JDesktopPane that contains the component, or null
  1044. * if the component is null or does not have an ancestor
  1045. * that is a JInternalFrame
  1046. */
  1047. public static JDesktopPane getDesktopPaneForComponent(Component parentComponent) {
  1048. if(parentComponent == null)
  1049. return null;
  1050. if(parentComponent instanceof JDesktopPane)
  1051. return (JDesktopPane)parentComponent;
  1052. return getDesktopPaneForComponent(parentComponent.getParent());
  1053. }
  1054. private static final Object sharedFrameKey = JOptionPane.class;
  1055. /**
  1056. * Sets the frame to use for class methods in which a frame is
  1057. * not provided.
  1058. *
  1059. * @param newRootFrame the default Frame to use
  1060. */
  1061. public static void setRootFrame(Frame newRootFrame) {
  1062. if (newRootFrame != null) {
  1063. SwingUtilities.appContextPut(sharedFrameKey, newRootFrame);
  1064. } else {
  1065. SwingUtilities.appContextRemove(sharedFrameKey);
  1066. }
  1067. }
  1068. /**
  1069. * Returns the Frame to use for the class methods in which a frame
  1070. * is not provided.
  1071. *
  1072. * @return the default Frame to use
  1073. */
  1074. public static Frame getRootFrame() {
  1075. Frame sharedFrame =
  1076. (Frame)SwingUtilities.appContextGet(sharedFrameKey);
  1077. if (sharedFrame == null) {
  1078. sharedFrame = SwingUtilities.getSharedOwnerFrame();
  1079. SwingUtilities.appContextPut(sharedFrameKey, sharedFrame);
  1080. }
  1081. return sharedFrame;
  1082. }
  1083. /**
  1084. * Creates a JOptionPane with a test message.
  1085. */
  1086. public JOptionPane() {
  1087. this("JOptionPane message");
  1088. }
  1089. /**
  1090. * Creates a instance of JOptionPane to display a message using the
  1091. * plain-message message type and the default options delivered by
  1092. * the UI.
  1093. *
  1094. * @param message the Object to display
  1095. */
  1096. public JOptionPane(Object message) {
  1097. this(message, PLAIN_MESSAGE);
  1098. }
  1099. /**
  1100. * Creates an instance of JOptionPane to display a message
  1101. * with the specified message type and the default options,
  1102. *
  1103. * @param message the Object to display
  1104. * @param messageType the type of message to be displayed:
  1105. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1106. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  1107. */
  1108. public JOptionPane(Object message, int messageType) {
  1109. this(message, messageType, DEFAULT_OPTION);
  1110. }
  1111. /**
  1112. * Creates an instance of JOptionPane to display a message
  1113. * with the specified message type and options.
  1114. *
  1115. * @param message the Object to display
  1116. * @param messageType the type of message to be displayed:
  1117. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1118. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  1119. * @param optionType the options to display in the pane:
  1120. * DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION
  1121. * OK_CANCEL_OPTION
  1122. */
  1123. public JOptionPane(Object message, int messageType, int optionType) {
  1124. this(message, messageType, optionType, null);
  1125. }
  1126. /**
  1127. * Creates an instance of JOptionPane to display a message
  1128. * with the specified message type, options, and icon.
  1129. *
  1130. * @param message the Object to display
  1131. * @param messageType the type of message to be displayed:
  1132. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1133. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  1134. * @param optionType the options to display in the pane:
  1135. * DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION
  1136. * OK_CANCEL_OPTION
  1137. * @param icon the Icon image to display
  1138. */
  1139. public JOptionPane(Object message, int messageType, int optionType,
  1140. Icon icon) {
  1141. this(message, messageType, optionType, icon, null);
  1142. }
  1143. /**
  1144. * Creates an instance of JOptionPane to display a message
  1145. * with the specified message type, icon, and options.
  1146. * None of the options is initially selected.
  1147. * <p>
  1148. * The options objects should contain either instances of Components,
  1149. * (which are added directly) or Strings (which are wrapped in a
  1150. * JButton). If you provide Components, you must ensure that when the
  1151. * Component is clicked it messages <code>setValue</code> in the
  1152. * created JOptionPane.
  1153. *
  1154. * @param message the Object to display
  1155. * @param messageType the type of message to be displayed:
  1156. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1157. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  1158. * @param optionType the options to display in the pane:
  1159. * DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION
  1160. * OK_CANCEL_OPTION. Only meaningful if the
  1161. * <code>options</code> parameter is null.
  1162. * @param icon the Icon image to display
  1163. * @param options the choices the user can select
  1164. */
  1165. public JOptionPane(Object message, int messageType, int optionType,
  1166. Icon icon, Object[] options) {
  1167. this(message, messageType, optionType, icon, options, null);
  1168. }
  1169. /**
  1170. * Creates an instance of JOptionPane to display a message
  1171. * with the specified message type, icon, and options, with the
  1172. * inititially-selected option specified.
  1173. *
  1174. * @param message the Object to display
  1175. * @param messageType the type of message to be displayed:
  1176. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1177. * QUESTION_MESSAGE, or PLAIN_MESSAGE.
  1178. * @param optionType the options to display in the pane:
  1179. * DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION