1. /*
  2. * @(#)JOptionPane.java 1.81 03/01/23
  3. *
  4. * Copyright 2003 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.KeyboardFocusManager;
  14. import java.awt.Frame;
  15. import java.awt.Point;
  16. import java.awt.HeadlessException;
  17. import java.awt.Toolkit;
  18. import java.awt.Window;
  19. import java.beans.PropertyChangeEvent;
  20. import java.beans.PropertyChangeListener;
  21. import java.awt.event.WindowAdapter;
  22. import java.awt.event.WindowEvent;
  23. import java.awt.event.ComponentAdapter;
  24. import java.awt.event.ComponentEvent;
  25. import java.io.IOException;
  26. import java.io.ObjectInputStream;
  27. import java.io.ObjectOutputStream;
  28. import java.io.Serializable;
  29. import java.util.Vector;
  30. import javax.swing.plaf.OptionPaneUI;
  31. import javax.swing.event.InternalFrameEvent;
  32. import javax.swing.event.InternalFrameAdapter;
  33. import javax.accessibility.*;
  34. /**
  35. * <code>JOptionPane</code> makes it easy to pop up a standard dialog box that
  36. * prompts users for a value or informs them of something.
  37. * For information about using <code>JOptionPane</code>, see
  38. * <a
  39. href="http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html">How to Make Dialogs</a>,
  40. * a section in <em>The Java Tutorial</em>.
  41. *
  42. * <p>
  43. *
  44. * While the <code>JOptionPane</code>
  45. * class may appear complex because of the large number of methods, almost
  46. * all uses of this class are one-line calls to one of the static
  47. * <code>showXxxDialog</code> methods shown below:
  48. * <blockquote>
  49. *
  50. *
  51. * <table border=1 summary="Common JOptionPane method names and their descriptions">
  52. * <tr>
  53. * <th>Method Name</th>
  54. * <th>Description</th>
  55. * </tr>
  56. * <tr>
  57. * <td>showConfirmDialog</td>
  58. * <td>Asks a confirming question, like yes/no/cancel.</td>
  59. * </tr>
  60. * <tr>
  61. * <td>showInputDialog</td>
  62. * <td>Prompt for some input.</td>
  63. * </tr>
  64. * <tr>
  65. * <td>showMessageDialog</td>
  66. * <td>Tell the user about something that has happened.</td>
  67. * </tr>
  68. * <tr>
  69. * <td>showOptionDialog</td>
  70. * <td>The Grand Unification of the above three.</td>
  71. * </tr>
  72. * </table>
  73. *
  74. * </blockquote>
  75. * Each of these methods also comes in a <code>showInternalXXX</code>
  76. * flavor, which uses an internal frame to hold the dialog box (see
  77. * {@link JInternalFrame}).
  78. * Multiple convenience methods have also been defined -- overloaded
  79. * versions of the basic methods that use different parameter lists.
  80. * <p>
  81. * All dialogs are modal. Each <code>showXxxDialog</code> method blocks
  82. * the current thread until the user's interaction is complete.
  83. * <p>
  84. *
  85. * <table cellspacing=6 cellpadding=4 border=0 align=right summary="layout">
  86. * <tr>
  87. * <td bgcolor=#FFe0d0 rowspan=2>icon</td>
  88. * <td bgcolor=#FFe0d0>message</td>
  89. * </tr>
  90. * <tr>
  91. * <td bgcolor=#FFe0d0>input value</td>
  92. * </tr>
  93. * <tr>
  94. * <td bgcolor=#FFe0d0 colspan=2>option buttons</td>
  95. * </tr>
  96. * </table>
  97. *
  98. * The basic appearance of one of these dialog boxes is generally
  99. * similar to the picture at the right, although the various
  100. * look-and-feels are
  101. * ultimately responsible for the final result. In particular, the
  102. * look-and-feels will adjust the layout to accommodate the option pane's
  103. * <code>ComponentOrientation</code> property.
  104. * <br clear=all>
  105. * <p>
  106. * <b>Parameters:</b><br>
  107. * The parameters to these methods follow consistent patterns:
  108. * <blockquote>
  109. * <dl compact>
  110. * <dt>parentComponent<dd>
  111. * Defines the <code>Component</code> that is to be the parent of this
  112. * dialog box.
  113. * It is used in two ways: the <code>Frame</code> that contains
  114. * it is used as the <code>Frame</code>
  115. * parent for the dialog box, and its screen coordinates are used in
  116. * the placement of the dialog box. In general, the dialog box is placed
  117. * just below the component. This parameter may be <code>null</code>,
  118. * in which case a default <code>Frame</code> is used as the parent,
  119. * and the dialog will be
  120. * centered on the screen (depending on the L&F).
  121. * <dt><a name=message>message</a><dd>
  122. * A descriptive message to be placed in the dialog box.
  123. * In the most common usage, message is just a <code>String</code> or
  124. * <code>String</code> constant.
  125. * However, the type of this parameter is actually <code>Object</code>. Its
  126. * interpretation depends on its type:
  127. * <dl compact>
  128. * <dt>Object[]<dd>An array of objects is interpreted as a series of
  129. * messages (one per object) arranged in a vertical stack.
  130. * The interpretation is recursive -- each object in the
  131. * array is interpreted according to its type.
  132. * <dt>Component<dd>The <code>Component</code> is displayed in the dialog.
  133. * <dt>Icon<dd>The <code>Icon</code> is wrapped in a <code>JLabel</code>
  134. * and displayed in the dialog.
  135. * <dt>others<dd>The object is converted to a <code>String</code> by calling
  136. * its <code>toString</code> method. The result is wrapped in a
  137. * <code>JLabel</code> and displayed.
  138. * </dl>
  139. * <dt>messageType<dd>Defines the style of the message. The Look and Feel
  140. * manager may lay out the dialog differently depending on this value, and
  141. * will often provide a default icon. The possible values are:
  142. * <ul>
  143. * <li><code>ERROR_MESSAGE</code>
  144. * <li><code>INFORMATION_MESSAGE</code>
  145. * <li><code>WARNING_MESSAGE</code>
  146. * <li><code>QUESTION_MESSAGE</code>
  147. * <li><code>PLAIN_MESSAGE</code>
  148. * </ul>
  149. * <dt>optionType<dd>Defines the set of option buttons that appear at
  150. * the bottom of the dialog box:
  151. * <ul>
  152. * <li><code>DEFAULT_OPTION</code>
  153. * <li><code>YES_NO_OPTION</code>
  154. * <li><code>YES_NO_CANCEL_OPTION</code>
  155. * <li><code>OK_CANCEL_OPTION</code>
  156. * </ul>
  157. * You aren't limited to this set of option buttons. You can provide any
  158. * buttons you want using the options parameter.
  159. * <dt>options<dd>A more detailed description of the set of option buttons
  160. * that will appear at the bottom of the dialog box.
  161. * The usual value for the options parameter is an array of
  162. * <code>String</code>s. But
  163. * the parameter type is an array of <code>Objects</code>.
  164. * A button is created for each object depending on its type:
  165. * <dl compact>
  166. * <dt>Component<dd>The component is added to the button row directly.
  167. * <dt>Icon<dd>A <code>JButton</code> is created with this as its label.
  168. * <dt>other<dd>The <code>Object</code> is converted to a string using its
  169. * <code>toString</code> method and the result is used to
  170. * label a <code>JButton</code>.
  171. * </dl>
  172. * <dt>icon<dd>A decorative icon to be placed in the dialog box. A default
  173. * value for this is determined by the <code>messageType</code> parameter.
  174. * <dt>title<dd>The title for the dialog box.
  175. * <dt>initialValue<dd>The default selection (input value).
  176. * </dl>
  177. * </blockquote>
  178. * <p>
  179. * When the selection is changed, <code>setValue</code> is invoked,
  180. * which generates a <code>PropertyChangeEvent</code>.
  181. * <p>
  182. * If a <code>JOptionPane</code> has configured to all input
  183. * <code>setWantsInput</code>
  184. * the bound property <code>JOptionPane.INPUT_VALUE_PROPERTY</code>
  185. * can also be listened
  186. * to, to determine when the user has input or selected a value.
  187. * <p>
  188. * When one of the <code>showXxxDialog</code> methods returns an integer,
  189. * the possible values are:
  190. * <ul>
  191. * <li><code>YES_OPTION</code>
  192. * <li><code>NO_OPTION</code>
  193. * <li><code>CANCEL_OPTION</code>
  194. * <li><code>OK_OPTION</code>
  195. * <li><code>CLOSED_OPTION</code>
  196. * </ul>
  197. * <b>Examples:</b>
  198. * <dl>
  199. * <dt>Show an error dialog that displays the message, 'alert':
  200. * <dd><code>
  201. * JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
  202. * </code><p>
  203. * <dt>Show an internal information dialog with the message, 'information':
  204. * <dd><code>
  205. * JOptionPane.showInternalMessageDialog(frame, "information",<br>
  206. * <ul><ul>"information", JOptionPane.INFORMATION_MESSAGE);</ul></ul>
  207. * </code><p>
  208. * <dt>Show an information panel with the options yes/no and message 'choose one':
  209. * <dd><code>JOptionPane.showConfirmDialog(null,
  210. * <ul><ul>"choose one", "choose one", JOptionPane.YES_NO_OPTION);</ul></ul>
  211. * </code><p>
  212. * <dt>Show an internal information dialog with the options yes/no/cancel and
  213. * message 'please choose one' and title information:
  214. * <dd><code>JOptionPane.showInternalConfirmDialog(frame,
  215. * <ul><ul>"please choose one", "information",</ul></ul>
  216. * <ul><ul>JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);</ul></ul>
  217. * </code><p>
  218. * <dt>Show a warning dialog with the options OK, CANCEL, title 'Warning', and
  219. * message 'Click OK to continue':
  220. * <dd><code>
  221. * Object[] options = { "OK", "CANCEL" };<br>
  222. * JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
  223. * <ul><ul>JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,</ul></ul>
  224. * <ul><ul>null, options, options[0]);</ul></ul>
  225. * </code><p>
  226. * <dt>Show a dialog asking the user to type in a String:
  227. * <dd><code>
  228. * String inputValue = JOptionPane.showInputDialog("Please input a value");
  229. * </code><p>
  230. * <dt>Show a dialog asking the user to select a String:
  231. * <dd><code>
  232. * Object[] possibleValues = { "First", "Second", "Third" };<br>
  233. * Object selectedValue = JOptionPane.showInputDialog(null,
  234. * <ul><ul>"Choose one", "Input",</ul></ul>
  235. * <ul><ul>JOptionPane.INFORMATION_MESSAGE, null,</ul></ul>
  236. * <ul><ul>possibleValues, possibleValues[0]);</ul></ul>
  237. * </code><p>
  238. * </dl>
  239. * <b>Direct Use:</b><br>
  240. * To create and use an <code>JOptionPane</code> directly, the
  241. * standard pattern is roughly as follows:
  242. * <pre>
  243. * JOptionPane pane = new JOptionPane(<i>arguments</i>);
  244. * pane.set<i>.Xxxx(...); // Configure</i>
  245. * JDialog dialog = pane.createDialog(<i>parentComponent, title</i>);
  246. * dialog.show();
  247. * Object selectedValue = pane.getValue();
  248. * if(selectedValue == null)
  249. * return CLOSED_OPTION;
  250. * <i>//If there is <b>not</b> an array of option buttons:</i>
  251. * if(options == null) {
  252. * if(selectedValue instanceof Integer)
  253. * return ((Integer)selectedValue).intValue();
  254. * return CLOSED_OPTION;
  255. * }
  256. * <i>//If there is an array of option buttons:</i>
  257. * for(int counter = 0, maxCounter = options.length;
  258. * counter < maxCounter; counter++) {
  259. * if(options[counter].equals(selectedValue))
  260. * return counter;
  261. * }
  262. * return CLOSED_OPTION;
  263. * </pre>
  264. * <p>
  265. * For the keyboard keys used by this component in the standard Look and
  266. * Feel (L&F) renditions, see the
  267. * <a href="doc-files/Key-Index.html#JOptionPane"><code>JOptionPane</code> key assignments</a>.
  268. * <p>
  269. * <strong>Warning:</strong>
  270. * Serialized objects of this class will not be compatible with
  271. * future Swing releases. The current serialization support is
  272. * appropriate for short term storage or RMI between applications running
  273. * the same version of Swing. As of 1.4, support for long term storage
  274. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  275. * has been added to the <code>java.beans</code> package.
  276. * Please see {@link java.beans.XMLEncoder}.
  277. *
  278. * @see JInternalFrame
  279. *
  280. * @beaninfo
  281. * attribute: isContainer true
  282. * description: A component which implements standard dialog box controls.
  283. *
  284. * @version 1.81 01/23/03
  285. * @author James Gosling
  286. * @author Scott Violet
  287. */
  288. public class JOptionPane extends JComponent implements Accessible
  289. {
  290. /**
  291. * @see #getUIClassID
  292. * @see #readObject
  293. */
  294. private static final String uiClassID = "OptionPaneUI";
  295. /**
  296. * Indicates that the user has not yet selected a value.
  297. */
  298. public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
  299. //
  300. // Option types
  301. //
  302. /**
  303. * Type meaning Look and Feel should not supply any options -- only
  304. * use the options from the <code>JOptionPane</code>.
  305. */
  306. /** Type used for <code>showConfirmDialog</code>. */
  307. public static final int DEFAULT_OPTION = -1;
  308. /** Type used for <code>showConfirmDialog</code>. */
  309. public static final int YES_NO_OPTION = 0;
  310. /** Type used for <code>showConfirmDialog</code>. */
  311. public static final int YES_NO_CANCEL_OPTION = 1;
  312. /** Type used for <code>showConfirmDialog</code>. */
  313. public static final int OK_CANCEL_OPTION = 2;
  314. //
  315. // Return values.
  316. //
  317. /** Return value from class method if YES is chosen. */
  318. public static final int YES_OPTION = 0;
  319. /** Return value from class method if NO is chosen. */
  320. public static final int NO_OPTION = 1;
  321. /** Return value from class method if CANCEL is chosen. */
  322. public static final int CANCEL_OPTION = 2;
  323. /** Return value form class method if OK is chosen. */
  324. public static final int OK_OPTION = 0;
  325. /** Return value from class method if user closes window without selecting
  326. * anything, more than likely this should be treated as either a
  327. * <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */
  328. public static final int CLOSED_OPTION = -1;
  329. //
  330. // Message types. Used by the UI to determine what icon to display,
  331. // and possibly what behavior to give based on the type.
  332. //
  333. /** Used for error messages. */
  334. public static final int ERROR_MESSAGE = 0;
  335. /** Used for information messages. */
  336. public static final int INFORMATION_MESSAGE = 1;
  337. /** Used for warning messages. */
  338. public static final int WARNING_MESSAGE = 2;
  339. /** Used for questions. */
  340. public static final int QUESTION_MESSAGE = 3;
  341. /** No icon is used. */
  342. public static final int PLAIN_MESSAGE = -1;
  343. /** Bound property name for <code>icon</code>. */
  344. public static final String ICON_PROPERTY = "icon";
  345. /** Bound property name for <code>message</code>. */
  346. public static final String MESSAGE_PROPERTY = "message";
  347. /** Bound property name for <code>value</code>. */
  348. public static final String VALUE_PROPERTY = "value";
  349. /** Bound property name for <code>option</code>. */
  350. public static final String OPTIONS_PROPERTY = "options";
  351. /** Bound property name for <code>initialValue</code>. */
  352. public static final String INITIAL_VALUE_PROPERTY = "initialValue";
  353. /** Bound property name for <code>type</code>. */
  354. public static final String MESSAGE_TYPE_PROPERTY = "messageType";
  355. /** Bound property name for <code>optionType</code>. */
  356. public static final String OPTION_TYPE_PROPERTY = "optionType";
  357. /** Bound property name for <code>selectionValues</code>. */
  358. public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
  359. /** Bound property name for <code>initialSelectionValue</code>. */
  360. public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
  361. /** Bound property name for <code>inputValue</code>. */
  362. public static final String INPUT_VALUE_PROPERTY = "inputValue";
  363. /** Bound property name for <code>wantsInput</code>. */
  364. public static final String WANTS_INPUT_PROPERTY = "wantsInput";
  365. /** Icon used in pane. */
  366. transient protected Icon icon;
  367. /** Message to display. */
  368. transient protected Object message;
  369. /** Options to display to the user. */
  370. transient protected Object[] options;
  371. /** Value that should be initially selected in <code>options</code>. */
  372. transient protected Object initialValue;
  373. /** Message type. */
  374. protected int messageType;
  375. /**
  376. * Option type, one of <code>DEFAULT_OPTION</code>,
  377. * <code>YES_NO_OPTION</code>,
  378. * <code>YES_NO_CANCEL_OPTION</code> or
  379. * <code>OK_CANCEL_OPTION</code>.
  380. */
  381. protected int optionType;
  382. /** Currently selected value, will be a valid option, or
  383. * <code>UNINITIALIZED_VALUE</code> or <code>null</code>. */
  384. transient protected Object value;
  385. /** Array of values the user can choose from. Look and feel will
  386. * provide the UI component to choose this from. */
  387. protected transient Object[] selectionValues;
  388. /** Value the user has input. */
  389. protected transient Object inputValue;
  390. /** Initial value to select in <code>selectionValues</code>. */
  391. protected transient Object initialSelectionValue;
  392. /** If true, a UI widget will be provided to the user to get input. */
  393. protected boolean wantsInput;
  394. /**
  395. * Shows a question-message dialog requesting input from the user. The
  396. * dialog uses the default frame, which usually means it is centered on
  397. * the screen.
  398. *
  399. * @param message the <code>Object</code> to display
  400. * @exception HeadlessException if
  401. * <code>GraphicsEnvironment.isHeadless</code> returns
  402. * <code>true</code>
  403. * @see java.awt.GraphicsEnvironment#isHeadless
  404. */
  405. public static String showInputDialog(Object message)
  406. throws HeadlessException {
  407. return showInputDialog(null, message);
  408. }
  409. /**
  410. * Shows a question-message dialog requesting input from the user, with
  411. * the input value initialized to <code>initialSelectionValue</code>. The
  412. * dialog uses the default frame, which usually means it is centered on
  413. * the screen.
  414. *
  415. * @param message the <code>Object</code> to display
  416. * @param initialSelectionValue the value used to initialize the input
  417. * field
  418. */
  419. public static String showInputDialog(Object message, Object initialSelectionValue) {
  420. return showInputDialog(null, message, initialSelectionValue);
  421. }
  422. /**
  423. * Shows a question-message dialog requesting input from the user
  424. * parented to <code>parentComponent</code>.
  425. * The dialog is displayed on top of the <code>Component</code>'s
  426. * frame, and is usually positioned below the <code>Component</code>.
  427. *
  428. * @param parentComponent the parent <code>Component</code> for the
  429. * dialog
  430. * @param message the <code>Object</code> to display
  431. * @exception HeadlessException if
  432. * <code>GraphicsEnvironment.isHeadless</code> returns
  433. * <code>true</code>
  434. * @see java.awt.GraphicsEnvironment#isHeadless
  435. */
  436. public static String showInputDialog(Component parentComponent,
  437. Object message) throws HeadlessException {
  438. return showInputDialog(parentComponent, message, UIManager.getString(
  439. "OptionPane.inputDialogTitle", parentComponent), QUESTION_MESSAGE);
  440. }
  441. /**
  442. * Shows a question-message dialog requesting input from the user and
  443. * parented to <code>parentComponent</code>. The input value will be
  444. * initialized to <code>initialSelectionValue</code>.
  445. * The dialog is displayed on top of the <code>Component</code>'s
  446. * frame, and is usually positioned below the <code>Component</code>.
  447. *
  448. * @param parentComponent the parent <code>Component</code> for the
  449. * dialog
  450. * @param message the <code>Object</code> to display
  451. * @param initialSelectionValue the value used to initialize the input
  452. * field
  453. */
  454. public static String showInputDialog(Component parentComponent, Object message,
  455. Object initialSelectionValue) {
  456. return (String)showInputDialog(parentComponent, message,
  457. UIManager.getString("OptionPane.inputDialogTitle",
  458. parentComponent), QUESTION_MESSAGE, null, null,
  459. initialSelectionValue);
  460. }
  461. /**
  462. * Shows a dialog requesting input from the user parented to
  463. * <code>parentComponent</code> with the dialog having the title
  464. * <code>title</code> and message type <code>messageType</code>.
  465. *
  466. * @param parentComponent the parent <code>Component</code> for the
  467. * dialog
  468. * @param message the <code>Object</code> to display
  469. * @param title the <code>String</code> to display in the dialog
  470. * title bar
  471. * @param messageType the type of message that is to be displayed:
  472. * <code>ERROR_MESSAGE</code>,
  473. * <code>INFORMATION_MESSAGE</code>,
  474. * <code>WARNING_MESSAGE</code>,
  475. * <code>QUESTION_MESSAGE</code>,
  476. * or <code>PLAIN_MESSAGE</code>
  477. * @exception HeadlessException if
  478. * <code>GraphicsEnvironment.isHeadless</code> returns
  479. * <code>true</code>
  480. * @see java.awt.GraphicsEnvironment#isHeadless
  481. */
  482. public static String showInputDialog(Component parentComponent,
  483. Object message, String title, int messageType)
  484. throws HeadlessException {
  485. return (String)showInputDialog(parentComponent, message, title,
  486. messageType, null, null, null);
  487. }
  488. /**
  489. * Prompts the user for input in a blocking dialog where the
  490. * initial selection, possible selections, and all other options can
  491. * be specified. The user will able to choose from
  492. * <code>selectionValues</code>, where <code>null</code> implies the
  493. * user can input
  494. * whatever they wish, usually by means of a <code>JTextField</code>.
  495. * <code>initialSelectionValue</code> is the initial value to prompt
  496. * the user with. It is up to the UI to decide how best to represent
  497. * the <code>selectionValues</code>, but usually a
  498. * <code>JComboBox</code>, <code>JList</code>, or
  499. * <code>JTextField</code> will be used.
  500. *
  501. * @param parentComponent the parent <code>Component</code> for the
  502. * dialog
  503. * @param message the <code>Object</code> to display
  504. * @param title the <code>String</code> to display in the
  505. * dialog title bar
  506. * @param messageType the type of message to be displayed:
  507. * <code>ERROR_MESSAGE</code>,
  508. * <code>INFORMATION_MESSAGE</code>,
  509. * <code>WARNING_MESSAGE</code>,
  510. * <code>QUESTION_MESSAGE</code>,
  511. * or <code>PLAIN_MESSAGE</code>
  512. * @param icon the <code>Icon</code> image to display
  513. * @param selectionValues an array of <code>Object</code>s that
  514. * gives the possible selections
  515. * @param initialSelectionValue the value used to initialize the input
  516. * field
  517. * @return user's input, or <code>null</code> meaning the user
  518. * canceled the input
  519. * @exception HeadlessException if
  520. * <code>GraphicsEnvironment.isHeadless</code> returns
  521. * <code>true</code>
  522. * @see java.awt.GraphicsEnvironment#isHeadless
  523. */
  524. public static Object showInputDialog(Component parentComponent,
  525. Object message, String title, int messageType, Icon icon,
  526. Object[] selectionValues, Object initialSelectionValue)
  527. throws HeadlessException {
  528. JOptionPane pane = new JOptionPane(message, messageType,
  529. OK_CANCEL_OPTION, icon,
  530. null, null);
  531. pane.setWantsInput(true);
  532. pane.setSelectionValues(selectionValues);
  533. pane.setInitialSelectionValue(initialSelectionValue);
  534. pane.setComponentOrientation(((parentComponent == null) ?
  535. getRootFrame() : parentComponent).getComponentOrientation());
  536. int style = styleFromMessageType(messageType);
  537. JDialog dialog = pane.createDialog(parentComponent, title, style);
  538. pane.selectInitialValue();
  539. dialog.show();
  540. dialog.dispose();
  541. Object value = pane.getInputValue();
  542. if (value == UNINITIALIZED_VALUE) {
  543. return null;
  544. }
  545. return value;
  546. }
  547. /**
  548. * Brings up an information-message dialog titled "Message".
  549. *
  550. * @param parentComponent determines the <code>Frame</code> in
  551. * which the dialog is displayed; if <code>null</code>,
  552. * or if the <code>parentComponent</code> has no
  553. * <code>Frame</code>, a default <code>Frame</code> is used
  554. * @param message the <code>Object</code> to display
  555. * @exception HeadlessException if
  556. * <code>GraphicsEnvironment.isHeadless</code> returns
  557. * <code>true</code>
  558. * @see java.awt.GraphicsEnvironment#isHeadless
  559. */
  560. public static void showMessageDialog(Component parentComponent,
  561. Object message) throws HeadlessException {
  562. showMessageDialog(parentComponent, message, UIManager.getString(
  563. "OptionPane.messageDialogTitle", parentComponent),
  564. INFORMATION_MESSAGE);
  565. }
  566. /**
  567. * Brings up a dialog that displays a message using a default
  568. * icon determined by the <code>messageType</code> parameter.
  569. *
  570. * @param parentComponent determines the <code>Frame</code>
  571. * in which the dialog is displayed; if <code>null</code>,
  572. * or if the <code>parentComponent</code> has no
  573. * <code>Frame</code>, a default <code>Frame</code> is used
  574. * @param message the <code>Object</code> to display
  575. * @param title the title string for the dialog
  576. * @param messageType the type of message to be displayed:
  577. * <code>ERROR_MESSAGE</code>,
  578. * <code>INFORMATION_MESSAGE</code>,
  579. * <code>WARNING_MESSAGE</code>,
  580. * <code>QUESTION_MESSAGE</code>,
  581. * or <code>PLAIN_MESSAGE</code>
  582. * @exception HeadlessException if
  583. * <code>GraphicsEnvironment.isHeadless</code> returns
  584. * <code>true</code>
  585. * @see java.awt.GraphicsEnvironment#isHeadless
  586. */
  587. public static void showMessageDialog(Component parentComponent,
  588. Object message, String title, int messageType)
  589. throws HeadlessException {
  590. showMessageDialog(parentComponent, message, title, messageType, null);
  591. }
  592. /**
  593. * Brings up a dialog displaying a message, specifying all parameters.
  594. *
  595. * @param parentComponent determines the <code>Frame</code> in which the
  596. * dialog is displayed; if <code>null</code>,
  597. * or if the <code>parentComponent</code> has no
  598. * <code>Frame</code>, a
  599. * default <code>Frame</code> is used
  600. * @param message the <code>Object</code> to display
  601. * @param title the title string for the dialog
  602. * @param messageType the type of message to be displayed:
  603. * <code>ERROR_MESSAGE</code>,
  604. * <code>INFORMATION_MESSAGE</code>,
  605. * <code>WARNING_MESSAGE</code>,
  606. * <code>QUESTION_MESSAGE</code>,
  607. * or <code>PLAIN_MESSAGE</code>
  608. * @param icon an icon to display in the dialog that helps the user
  609. * identify the kind of message that is being displayed
  610. * @exception HeadlessException if
  611. * <code>GraphicsEnvironment.isHeadless</code> returns
  612. * <code>true</code>
  613. * @see java.awt.GraphicsEnvironment#isHeadless
  614. */
  615. public static void showMessageDialog(Component parentComponent,
  616. Object message, String title, int messageType, Icon icon)
  617. throws HeadlessException {
  618. showOptionDialog(parentComponent, message, title, DEFAULT_OPTION,
  619. messageType, icon, null, null);
  620. }
  621. /**
  622. * Brings up a dialog with the options <i>Yes</i>,
  623. * <i>No</i> and <i>Cancel</i> with the
  624. * title, <b>Select an Option</b>.
  625. *
  626. * @param parentComponent determines the <code>Frame</code> in which the
  627. * dialog is displayed; if <code>null</code>,
  628. * or if the <code>parentComponent</code> has no
  629. * <code>Frame</code>, a
  630. * default <code>Frame</code> is used
  631. * @param message the <code>Object</code> to display
  632. * @return an integer indicating the option selected by the user
  633. * @exception HeadlessException if
  634. * <code>GraphicsEnvironment.isHeadless</code> returns
  635. * <code>true</code>
  636. * @see java.awt.GraphicsEnvironment#isHeadless
  637. */
  638. public static int showConfirmDialog(Component parentComponent,
  639. Object message) throws HeadlessException {
  640. return showConfirmDialog(parentComponent, message,
  641. UIManager.getString("OptionPane.titleText"),
  642. YES_NO_CANCEL_OPTION);
  643. }
  644. /**
  645. * Brings up a dialog where the number of choices is determined
  646. * by the <code>optionType</code> parameter.
  647. *
  648. * @param parentComponent determines the <code>Frame</code> in which the
  649. * dialog is displayed; if <code>null</code>,
  650. * or if the <code>parentComponent</code> has no
  651. * <code>Frame</code>, a
  652. * default <code>Frame</code> is used
  653. * @param message the <code>Object</code> to display
  654. * @param title the title string for the dialog
  655. * @param optionType an int designating the options available on the dialog:
  656. * <code>YES_NO_OPTION</code>, or
  657. * <code>YES_NO_CANCEL_OPTION</code>
  658. * @return an int indicating the option selected by the user
  659. * @exception HeadlessException if
  660. * <code>GraphicsEnvironment.isHeadless</code> returns
  661. * <code>true</code>
  662. * @see java.awt.GraphicsEnvironment#isHeadless
  663. */
  664. public static int showConfirmDialog(Component parentComponent,
  665. Object message, String title, int optionType)
  666. throws HeadlessException {
  667. return showConfirmDialog(parentComponent, message, title, optionType,
  668. QUESTION_MESSAGE);
  669. }
  670. /**
  671. * Brings up a dialog where the number of choices is determined
  672. * by the <code>optionType</code> parameter, where the
  673. * <code>messageType</code>
  674. * parameter determines the icon to display.
  675. * The <code>messageType</code> parameter is primarily used to supply
  676. * a default icon from the Look and Feel.
  677. *
  678. * @param parentComponent determines the <code>Frame</code> in
  679. * which the dialog is displayed; if <code>null</code>,
  680. * or if the <code>parentComponent</code> has no
  681. * <code>Frame</code>, a
  682. * default <code>Frame</code> is used.
  683. * @param message the <code>Object</code> to display
  684. * @param title the title string for the dialog
  685. * @param optionType an integer designating the options available
  686. * on the dialog: <code>YES_NO_OPTION</code>,
  687. * or <code>YES_NO_CANCEL_OPTION</code>
  688. * @param messageType an integer designating the kind of message this is;
  689. * primarily used to determine the icon from the pluggable
  690. * Look and Feel: <code>ERROR_MESSAGE</code>,
  691. * <code>INFORMATION_MESSAGE</code>,
  692. * <code>WARNING_MESSAGE</code>,
  693. * <code>QUESTION_MESSAGE</code>,
  694. * or <code>PLAIN_MESSAGE</code>
  695. * @return an integer indicating the option selected by the user
  696. * @exception HeadlessException if
  697. * <code>GraphicsEnvironment.isHeadless</code> returns
  698. * <code>true</code>
  699. * @see java.awt.GraphicsEnvironment#isHeadless
  700. */
  701. public static int showConfirmDialog(Component parentComponent,
  702. Object message, String title, int optionType, int messageType)
  703. throws HeadlessException {
  704. return showConfirmDialog(parentComponent, message, title, optionType,
  705. messageType, null);
  706. }
  707. /**
  708. * Brings up a dialog with a specified icon, where the number of
  709. * choices is determined by the <code>optionType</code> parameter.
  710. * The <code>messageType</code> parameter is primarily used to supply
  711. * a default icon from the look and feel.
  712. *
  713. * @param parentComponent determines the <code>Frame</code> in which the
  714. * dialog is displayed; if <code>null</code>,
  715. * or if the <code>parentComponent</code> has no
  716. * <code>Frame</code>, a
  717. * default <code>Frame</code> is used
  718. * @param message the Object to display
  719. * @param title the title string for the dialog
  720. * @param optionType an int designating the options available on the dialog:
  721. * <code>YES_NO_OPTION</code>,
  722. * or <code>YES_NO_CANCEL_OPTION</code>
  723. * @param messageType an int designating the kind of message this is,
  724. * primarily used to determine the icon from the pluggable
  725. * Look and Feel: <code>ERROR_MESSAGE</code>,
  726. * <code>INFORMATION_MESSAGE</code>,
  727. * <code>WARNING_MESSAGE</code>,
  728. * <code>QUESTION_MESSAGE</code>,
  729. * or <code>PLAIN_MESSAGE</code>
  730. * @param icon the icon to display in the dialog
  731. * @return an int indicating the option selected by the user
  732. * @exception HeadlessException if
  733. * <code>GraphicsEnvironment.isHeadless</code> returns
  734. * <code>true</code>
  735. * @see java.awt.GraphicsEnvironment#isHeadless
  736. */
  737. public static int showConfirmDialog(Component parentComponent,
  738. Object message, String title, int optionType,
  739. int messageType, Icon icon) throws HeadlessException {
  740. return showOptionDialog(parentComponent, message, title, optionType,
  741. messageType, icon, null, null);
  742. }
  743. /**
  744. * Brings up a dialog with a specified icon, where the initial
  745. * choice is determined by the <code>initialValue</code> parameter and
  746. * the number of choices is determined by the <code>optionType</code>
  747. * parameter.
  748. * <p>
  749. * If <code>optionType</code> is <code>YES_NO_OPTION</code>,
  750. * or <code>YES_NO_CANCEL_OPTION</code>
  751. * and the <code>options</code> parameter is <code>null</code>,
  752. * then the options are
  753. * supplied by the look and feel.
  754. * <p>
  755. * The <code>messageType</code> parameter is primarily used to supply
  756. * a default icon from the look and feel.
  757. *
  758. * @param parentComponent determines the <code>Frame</code>
  759. * in which the dialog is displayed; if
  760. * <code>null</code>, or if the
  761. * <code>parentComponent</code> has no
  762. * <code>Frame</code>, a
  763. * default <code>Frame</code> is used
  764. * @param message the <code>Object</code> to display
  765. * @param title the title string for the dialog
  766. * @param optionType an integer designating the options available on the
  767. * dialog: <code>YES_NO_OPTION</code>,
  768. * or <code>YES_NO_CANCEL_OPTION</code>
  769. * @param messageType an integer designating the kind of message this is,
  770. * primarily used to determine the icon from the
  771. * pluggable Look and Feel: <code>ERROR_MESSAGE</code>,
  772. * <code>INFORMATION_MESSAGE</code>,
  773. * <code>WARNING_MESSAGE</code>,
  774. * <code>QUESTION_MESSAGE</code>,
  775. * or <code>PLAIN_MESSAGE</code>
  776. * @param icon the icon to display in the dialog
  777. * @param options an array of objects indicating the possible choices
  778. * the user can make; if the objects are components, they
  779. * are rendered properly; non-<code>String</code>
  780. * objects are
  781. * rendered using their <code>toString</code> methods;
  782. * if this parameter is <code>null</code>,
  783. * the options are determined by the Look and Feel
  784. * @param initialValue the object that represents the default selection
  785. * for the dialog; only meaningful if <code>options</code>
  786. * is used; can be <code>null</code>
  787. * @return an integer indicating the option chosen by the user,
  788. * or <code>CLOSED_OPTION</code> if the user closed
  789. * the dialog
  790. * @exception HeadlessException if
  791. * <code>GraphicsEnvironment.isHeadless</code> returns
  792. * <code>true</code>
  793. * @see java.awt.GraphicsEnvironment#isHeadless
  794. */
  795. public static int showOptionDialog(Component parentComponent,
  796. Object message, String title, int optionType, int messageType,
  797. Icon icon, Object[] options, Object initialValue)
  798. throws HeadlessException {
  799. JOptionPane pane = new JOptionPane(message, messageType,
  800. optionType, icon,
  801. options, initialValue);
  802. pane.setInitialValue(initialValue);
  803. pane.setComponentOrientation(((parentComponent == null) ?
  804. getRootFrame() : parentComponent).getComponentOrientation());
  805. int style = styleFromMessageType(messageType);
  806. JDialog dialog = pane.createDialog(parentComponent, title, style);
  807. pane.selectInitialValue();
  808. dialog.show();
  809. dialog.dispose();
  810. Object selectedValue = pane.getValue();
  811. if(selectedValue == null)
  812. return CLOSED_OPTION;
  813. if(options == null) {
  814. if(selectedValue instanceof Integer)
  815. return ((Integer)selectedValue).intValue();
  816. return CLOSED_OPTION;
  817. }
  818. for(int counter = 0, maxCounter = options.length;
  819. counter < maxCounter; counter++) {
  820. if(options[counter].equals(selectedValue))
  821. return counter;
  822. }
  823. return CLOSED_OPTION;
  824. }
  825. /**
  826. * Creates and returns a new <code>JDialog</code> wrapping
  827. * <code>this</code> centered on the <code>parentComponent</code>
  828. * in the <code>parentComponent</code>'s frame.
  829. * <code>title</code> is the title of the returned dialog.
  830. * The returned <code>JDialog</code> will not be resizable by the
  831. * user, however programs can invoke <code>setResizable</code> on
  832. * the <code>JDialog</code> instance to change this property.
  833. * The returned <code>JDialog</code> will be set up such that
  834. * once it is closed, or the user clicks on one of the buttons,
  835. * the optionpane's value property will be set accordingly and
  836. * the dialog will be closed. Each time the dialog is made visible,
  837. * it will reset the option pane's value property to
  838. * <code>JOptionPane.UNINITIALIZED_VALUE</code> to ensure the
  839. * user's subsequent action closes the dialog properly.
  840. *
  841. * @param parentComponent determines the frame in which the dialog
  842. * is displayed; if the <code>parentComponent</code> has
  843. * no <code>Frame</code>, a default <code>Frame</code> is used
  844. * @param title the title string for the dialog
  845. * @return a new <code>JDialog</code> containing this instance
  846. * @exception HeadlessException if
  847. * <code>GraphicsEnvironment.isHeadless</code> returns
  848. * <code>true</code>
  849. * @see java.awt.GraphicsEnvironment#isHeadless
  850. */
  851. public JDialog createDialog(Component parentComponent, String title)
  852. throws HeadlessException {
  853. int style = styleFromMessageType(getMessageType());
  854. return createDialog(parentComponent, title, style);
  855. }
  856. private JDialog createDialog(Component parentComponent, String title,
  857. int style)
  858. throws HeadlessException {
  859. final JDialog dialog;
  860. Window window = JOptionPane.getWindowForComponent(parentComponent);
  861. if (window instanceof Frame) {
  862. dialog = new JDialog((Frame)window, title, true);
  863. } else {
  864. dialog = new JDialog((Dialog)window, title, true);
  865. }
  866. Container contentPane = dialog.getContentPane();
  867. contentPane.setLayout(new BorderLayout());
  868. contentPane.add(this, BorderLayout.CENTER);
  869. dialog.setResizable(false);
  870. if (JDialog.isDefaultLookAndFeelDecorated()) {
  871. boolean supportsWindowDecorations =
  872. UIManager.getLookAndFeel().getSupportsWindowDecorations();
  873. if (supportsWindowDecorations) {
  874. dialog.setUndecorated(true);
  875. getRootPane().setWindowDecorationStyle(style);
  876. }
  877. }
  878. dialog.pack();
  879. dialog.setLocationRelativeTo(parentComponent);
  880. dialog.addWindowListener(new WindowAdapter() {
  881. private boolean gotFocus = false;
  882. public void windowClosing(WindowEvent we) {
  883. setValue(null);
  884. }
  885. public void windowGainedFocus(WindowEvent we) {
  886. // Once window gets focus, set initial focus
  887. if (!gotFocus) {
  888. selectInitialValue();
  889. gotFocus = true;
  890. }
  891. }
  892. });
  893. dialog.addComponentListener(new ComponentAdapter() {
  894. public void componentShown(ComponentEvent ce) {
  895. // reset value to ensure closing works properly
  896. setValue(JOptionPane.UNINITIALIZED_VALUE);
  897. }
  898. });
  899. addPropertyChangeListener(new PropertyChangeListener() {
  900. public void propertyChange(PropertyChangeEvent event) {
  901. // Let the defaultCloseOperation handle the closing
  902. // if the user closed the window without selecting a button
  903. // (newValue = null in that case). Otherwise, close the dialog.
  904. if(dialog.isVisible() && event.getSource() == JOptionPane.this &&
  905. (event.getPropertyName().equals(VALUE_PROPERTY)) &&
  906. event.getNewValue() != null &&
  907. event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) {
  908. dialog.setVisible(false);
  909. }
  910. }
  911. });
  912. return dialog;
  913. }
  914. /**
  915. * Brings up an internal confirmation dialog panel. The dialog
  916. * is a information-message dialog titled "Message".
  917. *
  918. * @param parentComponent determines the <code>Frame</code>
  919. * in which the dialog is displayed; if <code>null</code>,
  920. * or if the <code>parentComponent</code> has no
  921. * <code>Frame</code>, a default <code>Frame</code> is used
  922. * @param message the object to display
  923. */
  924. public static void showInternalMessageDialog(Component parentComponent,
  925. Object message) {
  926. showInternalMessageDialog(parentComponent, message, UIManager.
  927. getString("OptionPane.messageDialogTitle",
  928. parentComponent), INFORMATION_MESSAGE);
  929. }
  930. /**
  931. * Brings up an internal dialog panel that displays a message
  932. * using a default icon determined by the <code>messageType</code>
  933. * parameter.
  934. *
  935. * @param parentComponent determines the <code>Frame</code>
  936. * in which the dialog is displayed; if <code>null</code>,
  937. * or if the <code>parentComponent</code> has no
  938. * <code>Frame</code>, a default <code>Frame</code> is used
  939. * @param message the <code>Object</code> to display
  940. * @param title the title string for the dialog
  941. * @param messageType the type of message to be displayed:
  942. * <code>ERROR_MESSAGE</code>,
  943. * <code>INFORMATION_MESSAGE</code>,
  944. * <code>WARNING_MESSAGE</code>,
  945. * <code>QUESTION_MESSAGE</code>,
  946. * or <code>PLAIN_MESSAGE</code>
  947. */
  948. public static void showInternalMessageDialog(Component parentComponent,
  949. Object message, String title,
  950. int messageType) {
  951. showInternalMessageDialog(parentComponent, message, title, messageType,null);
  952. }
  953. /**
  954. * Brings up an internal dialog panel displaying a message,
  955. * specifying all parameters.
  956. *
  957. * @param parentComponent determines the <code>Frame</code>
  958. * in which the dialog is displayed; if <code>null</code>,
  959. * or if the <code>parentComponent</code> has no
  960. * <code>Frame</code>, a default <code>Frame</code> is used
  961. * @param message the <code>Object</code> to display
  962. * @param title the title string for the dialog
  963. * @param messageType the type of message to be displayed:
  964. * <code>ERROR_MESSAGE</code>,
  965. * <code>INFORMATION_MESSAGE</code>,
  966. * <code>WARNING_MESSAGE</code>,
  967. * <code>QUESTION_MESSAGE</code>,
  968. * or <code>PLAIN_MESSAGE</code>
  969. * @param icon an icon to display in the dialog that helps the user
  970. * identify the kind of message that is being displayed
  971. */
  972. public static void showInternalMessageDialog(Component parentComponent,
  973. Object message,
  974. String title, int messageType,
  975. Icon icon){
  976. showInternalOptionDialog(parentComponent, message, title, DEFAULT_OPTION,
  977. messageType, icon, null, null);
  978. }
  979. /**
  980. * Brings up an internal dialog panel with the options <i>Yes</i>, <i>No</i>
  981. * and <i>Cancel</i> with the title, <b>Select an Option</b>.
  982. *
  983. * @param parentComponent determines the <code>Frame</code> in
  984. * which the dialog is displayed; if <code>null</code>,
  985. * or if the <code>parentComponent</code> has no
  986. * <code>Frame</code>, a default <code>Frame</code> is used
  987. * @param message the <code>Object</code> to display
  988. * @return an integer indicating the option selected by the user
  989. */
  990. public static int showInternalConfirmDialog(Component parentComponent,
  991. Object message) {
  992. return showInternalConfirmDialog(parentComponent, message,
  993. UIManager.getString("OptionPane.titleText"),
  994. YES_NO_CANCEL_OPTION);
  995. }
  996. /**
  997. * Brings up a internal dialog panel where the number of choices
  998. * is determined by the <code>optionType</code> parameter.
  999. *
  1000. * @param parentComponent determines the <code>Frame</code>
  1001. * in which the dialog is displayed; if <code>null</code>,
  1002. * or if the <code>parentComponent</code> has no
  1003. * <code>Frame</code>, a default <code>Frame</code> is used
  1004. * @param message the object to display in the dialog; a
  1005. * <code>Component</code> object is rendered as a
  1006. * <code>Component</code> a <code>String</code>
  1007. * object is rendered as a string; other objects
  1008. * are converted to a <code>String</code> using the
  1009. * <code>toString</code> method
  1010. * @param title the title string for the dialog
  1011. * @param optionType an integer designating the options
  1012. * available on the dialog: <code>YES_NO_OPTION</code>,
  1013. * or <code>YES_NO_CANCEL_OPTION</code>
  1014. * @return an integer indicating the option selected by the user
  1015. */
  1016. public static int showInternalConfirmDialog(Component parentComponent,
  1017. Object message, String title,
  1018. int optionType) {
  1019. return showInternalConfirmDialog(parentComponent, message, title, optionType,
  1020. QUESTION_MESSAGE);
  1021. }
  1022. /**
  1023. * Brings up an internal dialog panel where the number of choices
  1024. * is determined by the <code>optionType</code> parameter, where
  1025. * the <code>messageType</code> parameter determines the icon to display.
  1026. * The <code>messageType</code> parameter is primarily used to supply
  1027. * a default icon from the Look and Feel.
  1028. *
  1029. * @param parentComponent determines the <code>Frame</code> in
  1030. * which the dialog is displayed; if <code>null</code>,
  1031. * or if the <code>parentComponent</code> has no
  1032. * <code>Frame</code>, a default <code>Frame</code> is used
  1033. * @param message the object to display in the dialog; a
  1034. * <code>Component</code> object is rendered as a
  1035. * <code>Component</code> a <code>String</code>
  1036. * object is rendered as a string; other objects are
  1037. * converted to a <code>String</code> using the
  1038. * <code>toString</code> method
  1039. * @param title the title string for the dialog
  1040. * @param optionType an integer designating the options
  1041. * available on the dialog:
  1042. * <code>YES_NO_OPTION</code>, or <code>YES_NO_CANCEL_OPTION</code>
  1043. * @param messageType an integer designating the kind of message this is,
  1044. * primarily used to determine the icon from the
  1045. * pluggable Look and Feel: <code>ERROR_MESSAGE</code>,
  1046. * <code>INFORMATION_MESSAGE</code>,
  1047. * <code>WARNING_MESSAGE</code>, <code>QUESTION_MESSAGE</code>,
  1048. * or <code>PLAIN_MESSAGE</code>
  1049. * @return an integer indicating the option selected by the user
  1050. */
  1051. public static int showInternalConfirmDialog(Component parentComponent,
  1052. Object message,
  1053. String title, int optionType,
  1054. int messageType) {
  1055. return showInternalConfirmDialog(parentComponent, message, title, optionType,
  1056. messageType, null);
  1057. }
  1058. /**
  1059. * Brings up an internal dialog panel with a specified icon, where
  1060. * the number of choices is determined by the <code>optionType</code>
  1061. * parameter.
  1062. * The <code>messageType</code> parameter is primarily used to supply
  1063. * a default icon from the look and feel.
  1064. *
  1065. * @param parentComponent determines the <code>Frame</code>
  1066. * in which the dialog is displayed; if <code>null</code>,
  1067. * or if the parentComponent has no Frame, a
  1068. * default <code>Frame</code> is used
  1069. * @param message the object to display in the dialog; a
  1070. * <code>Component</code> object is rendered as a
  1071. * <code>Component</code> a <code>String</code>
  1072. * object is rendered as a string; other objects are
  1073. * converted to a <code>String</code> using the
  1074. * <code>toString</code> method
  1075. * @param title the title string for the dialog
  1076. * @param optionType an integer designating the options available
  1077. * on the dialog:
  1078. * <code>YES_NO_OPTION</code>, or
  1079. * <code>YES_NO_CANCEL_OPTION</code.
  1080. * @param messageType an integer designating the kind of message this is,
  1081. * primarily used to determine the icon from the pluggable
  1082. * Look and Feel: <code>ERROR_MESSAGE</code>,
  1083. * <code>INFORMATION_MESSAGE</code>,
  1084. * <code>WARNING_MESSAGE</code>, <code>QUESTION_MESSAGE</code>,
  1085. * or <code>PLAIN_MESSAGE</code>
  1086. * @param icon the icon to display in the dialog
  1087. * @return an integer indicating the option selected by the user
  1088. */
  1089. public static int showInternalConfirmDialog(Component parentComponent,
  1090. Object message,
  1091. String title, int optionType,
  1092. int messageType, Icon icon) {
  1093. return showInternalOptionDialog(parentComponent, message, title, optionType,
  1094. messageType, icon, null, null);
  1095. }
  1096. /**
  1097. * Brings up an internal dialog panel with a specified icon, where
  1098. * the initial choice is determined by the <code>initialValue</code>
  1099. * parameter and the number of choices is determined by the
  1100. * <code>optionType</code> parameter.
  1101. * <p>
  1102. * If <code>optionType</code> is <code>YES_NO_OPTION</code>, or
  1103. * <code>YES_NO_CANCEL_OPTION</code>
  1104. * and the <code>options</code> parameter is <code>null</code>,
  1105. * then the options are supplied by the Look and Feel.
  1106. * <p>
  1107. * The <code>messageType</code> parameter is primarily used to supply
  1108. * a default icon from the look and feel.
  1109. *
  1110. * @param parentComponent determines the <code>Frame</code>
  1111. * in which the dialog is displayed; if <code>null</code>,
  1112. * or if the <code>parentComponent</code> has no
  1113. * <code>Frame</code>, a default <code>Frame</code> is used
  1114. * @param message the object to display in the dialog; a
  1115. * <code>Component</code> object is rendered as a
  1116. * <code>Component</code> a <code>String</code>
  1117. * object is rendered as a string. Other objects are
  1118. * converted to a <code>String</code> using the
  1119. * <code>toString</code> method
  1120. * @param title the title string for the dialog
  1121. * @param optionType an integer designating the options available
  1122. * on the dialog: <code>YES_NO_OPTION</code>,
  1123. * or <code>YES_NO_CANCEL_OPTION</code>
  1124. * @param messageType an integer designating the kind of message this is;
  1125. * primarily used to determine the icon from the
  1126. * pluggable Look and Feel: <code>ERROR_MESSAGE</code>,
  1127. * <code>INFORMATION_MESSAGE</code>,
  1128. * <code>WARNING_MESSAGE</code>, <code>QUESTION_MESSAGE</code>,
  1129. * or <code>PLAIN_MESSAGE</code>
  1130. * @param icon the icon to display in the dialog
  1131. * @param options an array of objects indicating the possible choices
  1132. * the user can make; if the objects are components, they
  1133. * are rendered properly; non-<code>String</code>
  1134. * objects are rendered using their <code>toString</code>
  1135. * methods; if this parameter is <code>null</code>,
  1136. * the options are determined by the Look and Feel
  1137. * @param initialValue the object that represents the default selection
  1138. * for the dialog; only meaningful if <code>options</code>
  1139. * is used; can be <code>null</code>
  1140. * @return an integer indicating the option chosen by the user,
  1141. * or <code>CLOSED_OPTION</code> if the user closed the Dialog
  1142. */
  1143. public static int showInternalOptionDialog(Component parentComponent,
  1144. Object message,
  1145. String title, int optionType,
  1146. int messageType, Icon icon,
  1147. Object[] options, Object initialValue) {
  1148. JOptionPane pane = new JOptionPane(message, messageType,
  1149. optionType, icon,
  1150. options, initialValue);
  1151. Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager().
  1152. getFocusOwner();
  1153. pane.setInitialValue(initialValue);
  1154. JInternalFrame dialog = pane.createInternalFrame(parentComponent, title);
  1155. pane.selectInitialValue();
  1156. dialog.setVisible(true);
  1157. dialog.startModal();
  1158. if (parentComponent instanceof JInternalFrame) {
  1159. try {
  1160. ((JInternalFrame)parentComponent).setSelected(true);
  1161. } catch (java.beans.PropertyVetoException e) {}
  1162. }
  1163. Object selectedValue = pane.getValue();
  1164. if (fo != null && fo.isShowing()) {
  1165. fo.requestFocus();
  1166. }
  1167. if (selectedValue == null) {
  1168. return CLOSED_OPTION;
  1169. }
  1170. if (options == null) {
  1171. if (selectedValue instanceof Integer) {
  1172. return ((Integer)selectedValue).intValue();
  1173. }
  1174. return CLOSED_OPTION;
  1175. }
  1176. for(int counter = 0, maxCounter = options.length;
  1177. counter < maxCounter; counter++) {
  1178. if (options[counter].equals(selectedValue)) {
  1179. return counter;
  1180. }
  1181. }
  1182. return CLOSED_OPTION;
  1183. }
  1184. /**
  1185. * Shows an internal question-message dialog requesting input from
  1186. * the user parented to <code>parentComponent</code>. The dialog
  1187. * is displayed in the <code>Component</code>'s frame,
  1188. * and is usually positioned below the <code>Component</code>.
  1189. *
  1190. * @param parentComponent the parent <code>Component</code>
  1191. * for the dialog
  1192. * @param message the <code>Object</code> to display
  1193. */
  1194. public static String showInternalInputDialog(Component parentComponent,
  1195. Object message) {
  1196. return showInternalInputDialog(parentComponent, message, UIManager.
  1197. getString("OptionPane.inputDialogTitle", parentComponent),
  1198. QUESTION_MESSAGE);
  1199. }
  1200. /**
  1201. * Shows an internal dialog requesting input from the user parented
  1202. * to <code>parentComponent</code> with the dialog having the title
  1203. * <code>title</code> and message type <code>messageType</code>.
  1204. *
  1205. * @param parentComponent the parent <code>Component</code> for the dialog
  1206. * @param message the <code>Object</code> to display
  1207. * @param title the <code>String</code> to display in the
  1208. * dialog title bar
  1209. * @param messageType the type of message that is to be displayed:
  1210. * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
  1211. * QUESTION_MESSAGE, or PLAIN_MESSAGE
  1212. */
  1213. public static String showInternalInputDialog(Component parentComponent,
  1214. Object message, String title, int messageType) {
  1215. return (String)showInternalInputDialog(parentComponent, message, title,
  1216. messageType, null, null, null);
  1217. }
  1218. /**
  1219. * Prompts the user for input in a blocking internal dialog where
  1220. * the initial selection, possible selections, and all other
  1221. * options can be specified. The user will able to choose from
  1222. * <code>selectionValues</code>, where <code>null</code>
  1223. * implies the user can input
  1224. * whatever they wish, usually by means of a <code>JTextField</code>.
  1225. * <code>initialSelectionValue</code> is the initial value to prompt
  1226. * the user with. It is up to the UI to decide how best to represent
  1227. * the <code>selectionValues</code>, but usually a
  1228. * <code>JComboBox</code>, <code>JList</code>, or
  1229. * <code>JTextField</code> will be used.
  1230. *
  1231. * @param parentComponent the parent <code>Component</code> for the dialog
  1232. * @param message the <code>Object</code> to display
  1233. * @param title the <code>String</code> to display in the dialog
  1234. * title bar
  1235. * @param messageType the type of message to be displayed:
  1236. * <code>ERROR_MESSAGE</code>, <code>INFORMATION_MESSAGE</code>,
  1237. * <code>WARNING_MESSAGE</code>,
  1238. * <code>QUESTION_MESSAGE</code>, or <code>PLAIN_MESSAGE</code>
  1239. * @param icon the <code>Icon</code> image to display
  1240. * @param selectionValues an array of <code>Objects</code> that
  1241. * gives the possible selections
  1242. * @param initialSelectionValue the value used to initialize the input
  1243. * field
  1244. * @return user's input, or <code>null</code> meaning the user
  1245. * canceled the input
  1246. */
  1247. public static Object showInternalInputDialog(Component parentComponent,
  1248. Object message, String title, int messageType, Icon icon,
  1249. Object[] selectionValues, Object initialSelectionValue) {
  1250. JOptionPane pane = new JOptionPane(message, messageType,
  1251. OK_CANCEL_OPTION, icon,
  1252. null, null);
  1253. Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager().
  1254. getFocusOwner();
  1255. pane.setWantsInput(true);
  1256. pane.setSelectionValues(selectionValues);
  1257. pane.setInitialSelectionValue(initialSelectionValue);
  1258. JInternalFrame dialog = pane.createInternalFrame(parentComponent, title);
  1259. pane.selectInitialValue();
  1260. dialog.setVisible(true);
  1261. dialog.startModal();
  1262. if (parentComponent instanceof JInternalFrame) {
  1263. try {
  1264. ((JInternalFrame)parentComponent).setSelected(true);
  1265. } catch (java.beans.PropertyVetoException e) {}
  1266. }
  1267. if (fo != null && fo.isShowing()) {
  1268. fo.requestFocus();
  1269. }
  1270. Object value = pane.getInputValue();
  1271. if (value == UNINITIALIZED_VALUE) {
  1272. return null;
  1273. }
  1274. return value;
  1275. }
  1276. /**
  1277. * Creates and returns an instance of <code>JInternalFrame</code>.
  1278. * The internal frame is created with the specified title,
  1279. * and wrapping the <code>JOptionPane</code>.
  1280. * The returned <code>JInternalFrame</code> is
  1281. * added to the <code>JDesktopPane</code> ancestor of
  1282. * <code>parentComponent</code>, or components
  1283. * parent if one its ancestors isn't a <code>JDesktopPane</code>,
  1284. * or if <code>parentComponent</code>
  1285. * doesn't have a parent then a <code>RuntimeException</code> is thrown.
  1286. *
  1287. * @param parentComponent the parent <code>Component</code> for
  1288. * the internal frame
  1289. * @param title the <code>String</code> to display in the
  1290. * frame's title bar
  1291. * @return a <code>JInternalFrame</code> containing a
  1292. * <code>JOptionPane</code>
  1293. * @exception RuntimeException if <code>parentComponent</code> does
  1294. * not have a valid parent
  1295. */
  1296. public JInternalFrame createInternalFrame(Component parentComponent,
  1297. String title) {
  1298. Container parent = JOptionPane.getDesktopPaneForComponent(parentComponent);
  1299. if (parent == null && (parentComponent == null ||
  1300. (parent = parentComponent.getParent()) == null)) {
  1301. throw new RuntimeException("JOptionPane: parentComponent does not have a valid parent");
  1302. }
  1303. // Option dialogs should be closable only
  1304. final JInternalFrame iFrame = new JInternalFrame(title, false, true,
  1305. false, false);
  1306. iFrame.putClientProperty( "JInternalFrame.frameType", "optionDialog" ); //jcs
  1307. iFrame.putClientProperty("JInternalFrame.messageType",
  1308. new Integer(getMessageType()));
  1309. iFrame.addInternalFrameListener(new InternalFrameAdapter() {
  1310. public void internalFrameClosing(InternalFrameEvent e) {
  1311. if (getValue() == UNINITIALIZED_VALUE) {
  1312. setValue(null);
  1313. }
  1314. }
  1315. });
  1316. addPropertyChangeListener(new PropertyChangeListener() {
  1317. public void propertyChange(PropertyChangeEvent event) {
  1318. // Let the defaultCloseOperation handle the closing
  1319. // if the user closed the iframe without selecting a button
  1320. // (newValue = null in that case). Otherwise, close the dialog.
  1321. if (iFrame.isVisible() && event.getSource() == JOptionPane.this &&
  1322. event.getPropertyName().equals(VALUE_PROPERTY)) {
  1323. try {
  1324. iFrame.setClosed(true);
  1325. } catch (java.beans.PropertyVetoException e) {}
  1326. iFrame.setVisible(false);
  1327. iFrame.stopModal();
  1328. }
  1329. }
  1330. });
  1331. iFrame.getContentPane().add(this, BorderLayout.CENTER);
  1332. if (parent instanceof JDesktopPane) {
  1333. parent.add(iFrame, JLayeredPane.MODAL_LAYER);
  1334. } else {
  1335. parent.add(iFrame, BorderLayout.CENTER);
  1336. }
  1337. Dimension iFrameSize = iFrame.getPreferredSize();
  1338. Dimension rootSize = parent.getSize();
  1339. Dimension parentSize = parentComponent.getSize();
  1340. iFrame.setBounds((rootSize.width - iFrameSize.width) / 2,
  1341. (rootSize.height - iFrameSize.height) / 2,
  1342. iFrameSize.width, iFrameSize.height);
  1343. // We want dialog centered relative to its parent component
  1344. Point iFrameCoord =
  1345. SwingUtilities.convertPoint(parentComponent, 0, 0, parent);
  1346. int x = (parentSize.width - iFrameSize.width) / 2 + iFrameCoord.x;
  1347. int y = (parentSize.height - iFrameSize.height) / 2 + iFrameCoord.y;
  1348. // If possible, dialog should be fully visible
  1349. int ovrx = x + iFrameSize.width - rootSize.width;
  1350. int ovry = y + iFrameSize.height - rootSize.height;
  1351. x = Math.max((ovrx > 0? x - ovrx: x), 0);
  1352. y = Math.max((ovry > 0? y - ovry: y), 0);
  1353. iFrame.setBounds(x, y, iFrameSize.width, iFrameSize.height);
  1354. parent.validate();
  1355. try {
  1356. iFrame.setSelected(true);
  1357. } catch (java.beans.PropertyVetoException e) {}
  1358. return iFrame;
  1359. }
  1360. /**
  1361. * Returns the specified component's <code>Frame</code>.
  1362. *
  1363. * @param parentComponent the <code>Component</code> to check for a
  1364. * <code>Frame</code>
  1365. * @return the <code>Frame</code> that contains the component,
  1366. * or the default frame if the component is <code>null</code>,
  1367. * or does not have a valid <code>Frame</code> parent
  1368. * @exception HeadlessException if
  1369. * <code>GraphicsEnvironment.isHeadless</code> returns
  1370. * <code>true</code>
  1371. * @see java.awt.GraphicsEnvironment#isHeadless
  1372. */
  1373. public static Frame getFrameForComponent(Component parentComponent)
  1374. throws HeadlessException {
  1375. if (parentComponent == null)
  1376. return getRootFrame();
  1377. if (parentComponent instanceof Frame)
  1378. return (Frame)parentComponent;
  1379. return JOptionPane.getFrameForComponent(parentComponent.getParent());
  1380. }
  1381. /**
  1382. * Returns the specified component's toplevel <code>Frame</code> or
  1383. * <code>Dialog</code>.
  1384. *
  1385. * @param parentComponent the <code>Component</code> to check for a
  1386. * <code>Frame</code> or <code>Dialog</code>
  1387. * @return the <code>Frame</code> or <code>Dialog</code> that
  1388. * contains the component, or the default
  1389. * frame if the component is <code>null</code>,
  1390. * or does not have a valid
  1391. * <code>Frame</code> or <code>Dialog</code> parent
  1392. * @exception HeadlessException if
  1393. * <code>GraphicsEnvironment.isHeadless</code> returns
  1394. * <code>true</code>
  1395. * @see java.awt.GraphicsEnvironment#isHeadless
  1396. */
  1397. static Window getWindowForComponent(Component parentComponent)
  1398. throws HeadlessException {
  1399. if (parentComponent == null)
  1400. return getRootFrame();
  1401. if (parentComponent instanceof Frame || parentComponent instanceof Dialog)
  1402. return (Window)parentComponent;
  1403. return JOptionPane.getWindowForComponent(parentComponent.getParent());
  1404. }
  1405. /**
  1406. * Returns the specified component's desktop pane.
  1407. *
  1408. * @param parentComponent the <code>Component</code> to check for a
  1409. * desktop
  1410. * @return the <code>JDesktopPane</code> that contains the component,
  1411. * or <code>null</code> if the component is <code>null</code>
  1412. * or does not have an ancestor that is a
  1413. * <code>JInternalFrame</code>
  1414. */
  1415. public static JDesktopPane getDesktopPaneForComponent(Component parentComponent) {
  1416. if(parentComponent == null)
  1417. return null;
  1418. if(parentComponent instanceof JDesktopPane)
  1419. return (JDesktopPane)parentComponent;
  1420. return getDesktopPaneForComponent(parentComponent.getParent());
  1421. }
  1422. private static final Object sharedFrameKey = JOptionPane.class;
  1423. /**
  1424. * Sets the frame to use for class methods in which a frame is
  1425. * not provided.
  1426. *
  1427. * @param newRootFrame the default <code>Frame</code> to use
  1428. */
  1429. public static void setRootFrame(Frame newRootFrame) {
  1430. if (newRootFrame != null) {
  1431. SwingUtilities.appContextPut(sharedFrameKey, newRootFrame);
  1432. } else {
  1433. SwingUtilities.appContextRemove(sharedFrameKey);
  1434. }
  1435. }
  1436. /**
  1437. * Returns the <code>Frame</code> to use for the class methods in
  1438. * which a frame is not provided.
  1439. *
  1440. * @return the default <code>Frame</code> to use
  1441. * @exception HeadlessException if
  1442. * <code>GraphicsEnvironment.isHeadless</code> returns
  1443. * <code>true</code>
  1444. * @see java.awt.GraphicsEnvironment#isHeadless
  1445. */
  1446. public static Frame getRootFrame() throws HeadlessException {
  1447. Frame sharedFrame =
  1448. (Frame)SwingUtilities.appContextGet(sharedFrameKey);
  1449. if (sharedFrame == null) {
  1450. sharedFrame = SwingUtilities.getSharedOwnerFrame();
  1451. SwingUtilities.appContextPut(sharedFrameKey, sharedFrame);
  1452. }
  1453. return sharedFrame;
  1454. }
  1455. /**
  1456. * Creates a <code>JOptionPane</code> with a test message.
  1457. */
  1458. public JOptionPane() {
  1459. this("JOptionPane message");
  1460. }
  1461. /**
  1462. * Creates a instance of <code>JOptionPane</code> to display a
  1463. * message using the
  1464. * plain-message message type and the default options delivered by
  1465. * the UI.
  1466. *
  1467. * @param message the <code>Object</code> to display
  1468. */
  1469. public JOptionPane(Object message) {
  1470. this(message, PLAIN_MESSAGE);
  1471. }
  1472. /**
  1473. * Creates an instance of <code>JOptionPane</code> to display a message
  1474. * with the specified message type and the default options,
  1475. *
  1476. * @param message the <code>Object</code> to display
  1477. * @param messageType the type of message to be displayed:
  1478. * <code>ERROR_MESSAGE</code>,
  1479. * <code>INFORMATION_MESSAGE</code>,
  1480. * <code>WARNING_MESSAGE</code>,
  1481. * <code>QUESTION_MESSAGE</code>,
  1482. * or <code>PLAIN_MESSAGE</code>
  1483. */
  1484. public JOptionPane(Object message, int messageType) {
  1485. this(message, messageType, DEFAULT_OPTION);
  1486. }
  1487. /**
  1488. * Creates an instance of <code>JOptionPane</code> to display a message
  1489. * with the specified message type and options.
  1490. *
  1491. * @param message the <code>Object</code> to display
  1492. * @param messageType the type of message to be displayed:
  1493. * <code>ERROR_MESSAGE</code>,
  1494. * <code>INFORMATION_MESSAGE</code>,
  1495. * <code>WARNING_MESSAGE</code>,
  1496. * <code>QUESTION_MESSAGE</code>,
  1497. * or <code>PLAIN_MESSAGE</code>
  1498. * @param optionType the options to display in the pane:
  1499. * <code>DEFAULT_OPTION</code>, <code>YES_NO_OPTION</code>,
  1500. * <code>YES_NO_CANCEL_OPTION</code>,
  1501. * <code>OK_CANCEL_OPTION</code>
  1502. */
  1503. public JOptionPane(Object message, int messageType, int optionType) {
  1504. this(message, messageType, optionType, null);
  1505. }
  1506. /**
  1507. * Creates an instance of <code>JOptionPane</code> to display a message
  1508. * with the specified message type, options, and icon.
  1509. *
  1510. * @param message the <code>Object</code> to display
  1511. * @param messageType the type of message to be displayed:
  1512. * <code>ERROR_MESSAGE</code>,
  1513. * <code>INFORMATION_MESSAGE</code>,
  1514. * <code>WARNING_MESSAGE</code>,
  1515. * <code>QUESTION_MESSAGE</code>,
  1516. * or <code>PLAIN_MESSAGE</code>
  1517. * @param optionType the options to display in the pane:
  1518. * <code>DEFAULT_OPTION</code>, <code>YES_NO_OPTION</code>,
  1519. * <code>YES_NO_CANCEL_OPTION</code>,
  1520. * <code>OK_CANCEL_OPTION</code>
  1521. * @param icon the <code>Icon</code> image to display
  1522. */
  1523. public JOptionPane(Object message, int messageType, int optionType,
  1524. Icon icon) {
  1525. this(message, messageType, optionType, icon, null);
  1526. }
  1527. /**
  1528. * Creates an instance of <code>JOptionPane</code> to display a message
  1529. * with the specified message type, icon, and options.
  1530. * None of the options is initially selected.
  1531. * <p>
  1532. * The options objects should contain either instances of
  1533. * <code>Component</code>s, (which are added directly) or
  1534. * <code>Strings</code> (which are wrapped in a <code>JButton</code>).
  1535. * If you provide <code>Component</code>s, you must ensure that when the
  1536. * <code>Component</code> is clicked it messages <code>setValue</code>
  1537. * in the created <code>JOptionPane</code>.
  1538. *
  1539. * @param message the <code>Object</code> to display
  1540. * @param messageType the type of message to be displayed:
  1541. * <code>ERROR_MESSAGE</code>,
  1542. * <code>INFORMATION_MESSAGE</code>,
  1543. * <code>WARNING_MESSAGE</code>,
  1544. * <code>QUESTION_MESSAGE</code>,
  1545. * or <code>PLAIN_MESSAGE</code>
  1546. * @param optionType the options to display in the pane:
  1547. * <code>DEFAULT_OPTION</code>,
  1548. * <code>YES_NO_OPTION</code>,
  1549. * <code>YES_NO_CANCEL_OPTION</code>,
  1550. * <code>OK_CANCEL_OPTION</code>
  1551. * @param icon the <code>Icon</code> image to display
  1552. * @param options the choices the user can select
  1553. */
  1554. public JOptionPane(Object message, int messageType, int optionType,
  1555. Icon icon, Object[] options) {
  1556. this(message, messageType, optionType, icon, options, null);
  1557. }
  1558. /**
  1559. * Creates an instance of <code>JOptionPane</code> to display a message
  1560. * with the specified message type, icon, and options, with the
  1561. * initially-selected option specified.
  1562. *
  1563. * @param message the <code>Object</code> to display
  1564. * @param messageType the type of message to be displayed:
  1565. * <code>ERROR_MESSAGE</code>,
  1566. * <code>INFORMATION_MESSAGE</code>,
  1567. * <code>WARNING_MESSAGE</code>,
  1568. * <code>QUESTION_MESSAGE</code>,
  1569. * or <code>PLAIN_MESSAGE</code>
  1570. * @param optionType the options to display in the pane:
  1571. * <code>DEFAULT_OPTION</code>,
  1572. * <code>YES_NO_OPTION</code>,
  1573. * <code>YES_NO_CANCEL_OPTION</code>,
  1574. * <code>OK_CANCEL_OPTION</code>
  1575. * @param icon the Icon image to display
  1576. * @param options the choices the user can select
  1577. * @param initialValue the choice that is initially selected; if
  1578. * <code>null</code>, then nothing will be initially selected;
  1579. * only meaningful if <code>options</code> is used
  1580. */
  1581. public JOptionPane(Object message, int messageType, int optionType,
  1582. Icon icon, Object[] options, Object initialValue) {
  1583. this.message = message;
  1584. this.options = options;
  1585. this.initialValue = initialValue;
  1586. this.icon = icon;
  1587. setMessageType(messageType);
  1588. setOptionType(optionType);
  1589. value = UNINITIALIZED_VALUE;
  1590. inputValue = UNINITIALIZED_VALUE;
  1591. updateUI();
  1592. }
  1593. /**
  1594. * Sets the UI object which implements the L&F for this component.
  1595. *
  1596. * @param ui the <code>OptionPaneUI</code> L&F object
  1597. * @see UIDefaults#getUI
  1598. * @beaninfo
  1599. * bound: true
  1600. * hidden: true
  1601. * description: The UI object that implements the optionpane's LookAndFeel
  1602. */
  1603. public void setUI(OptionPaneUI ui) {
  1604. if ((OptionPaneUI)this.ui != ui) {
  1605. super.setUI(ui);
  1606. invalidate();
  1607. }
  1608. }
  1609. /**
  1610. * Returns the UI object which implements the L&F for this component.
  1611. *
  1612. * @return the <code>OptionPaneUI</code> object
  1613. */
  1614. public OptionPaneUI getUI() {
  1615. return (OptionPaneUI)ui;
  1616. }
  1617. /**
  1618. * Notification from the <code>UIManager</code> that the L&F has changed.
  1619. * Replaces the current UI object with the latest version from the
  1620. * <code>UIManager</code>.
  1621. *
  1622. * @see JComponent#updateUI
  1623. */
  1624. public void updateUI() {
  1625. setUI((OptionPaneUI)UIManager.getUI(this));
  1626. }
  1627. /**
  1628. * Returns the name of the UI class that implements the
  1629. * L&F for this component.
  1630. *
  1631. * @return the string "OptionPaneUI"
  1632. * @see JComponent#getUIClassID
  1633. * @see UIDefaults#getUI
  1634. */
  1635. public String getUIClassID() {
  1636. return uiClassID;
  1637. }
  1638. /**
  1639. * Sets the option pane's message-object.
  1640. * @param newMessage the <code>Object</code> to display
  1641. * @see #getMessage
  1642. *
  1643. * @beaninfo
  1644. * preferred: true
  1645. * bound: true
  1646. * description: The optionpane's message object.
  1647. */
  1648. public void setMessage(Object newMessage) {
  1649. Object oldMessage = message;
  1650. message = newMessage;
  1651. firePropertyChange(MESSAGE_PROPERTY, oldMessage, message);
  1652. }
  1653. /**
  1654. * Returns the message-object this pane displays.
  1655. * @see #setMessage
  1656. *
  1657. * @return the <code>Object</code> that is displayed
  1658. */
  1659. public Object getMessage() {
  1660. return message;
  1661. }
  1662. /**
  1663. * Sets the icon to display. If non-<code>null</code>, the look and feel
  1664. * does not provide an icon.
  1665. * @param newIcon the <code>Icon</code> to display
  1666. *
  1667. * @see #getIcon
  1668. * @beaninfo
  1669. * preferred: true
  1670. * bound: true
  1671. * description: The option pane's type icon.
  1672. */
  1673. public void setIcon(Icon newIcon) {
  1674. Object oldIcon = icon;
  1675. icon = newIcon;
  1676. firePropertyChange(ICON_PROPERTY, oldIcon, icon);
  1677. }
  1678. /**
  1679. * Returns the icon this pane displays.
  1680. * @return the <code>Icon</code> that is displayed
  1681. *
  1682. * @see #setIcon
  1683. */
  1684. public Icon getIcon() {
  1685. return icon;
  1686. }
  1687. /**
  1688. * Sets the value the user has chosen.
  1689. * @param newValue the chosen value
  1690. *
  1691. * @see #getValue
  1692. * @beaninfo
  1693. * preferred: true
  1694. * bound: true
  1695. * description: The option pane's value object.
  1696. */
  1697. public void setValue(Object newValue) {
  1698. Object oldValue = value;
  1699. value = newValue;
  1700. firePropertyChange(VALUE_PROPERTY, oldValue, value);
  1701. }
  1702. /**
  1703. * Returns the value the user has selected. <code>UNINITIALIZED_VALUE</code>
  1704. * implies the user has not yet made a choice, <code>null</code> means the
  1705. * user closed the window with out choosing anything. Otherwise
  1706. * the returned value will be one of the options defined in this
  1707. * object.
  1708. *
  1709. * @return the <code>Object</code> chosen by the user,
  1710. * <code>UNINITIALIZED_VALUE</code>
  1711. * if the user has not yet made a choice, or <code>null</code> if
  1712. * the user closed the window without making a choice
  1713. *
  1714. * @see #setValue
  1715. */
  1716. public Object getValue() {
  1717. return value;
  1718. }
  1719. /**
  1720. * Sets the options this pane displays. If an element in
  1721. * <code>newOptions</code> is a <code>Component</code>
  1722. * it is added directly to the pane,
  1723. * otherwise a button is created for the element.
  1724. *
  1725. * @param newOptions an array of <code>Objects</code> that create the
  1726. * buttons the user can click on, or arbitrary
  1727. * <code>Components</code> to add to the pane
  1728. *
  1729. * @see #getOptions
  1730. * @beaninfo
  1731. * bound: true
  1732. * description: The option pane's options objects.
  1733. */
  1734. public void setOptions(Object[] newOptions) {
  1735. Object[] oldOptions = options;
  1736. options = newOptions;
  1737. firePropertyChange(OPTIONS_PROPERTY, oldOptions, options);
  1738. }
  1739. /**
  1740. * Returns the choices the user can make.
  1741. * @return the array of <code>Objects</code> that give the user's choices
  1742. *
  1743. * @see #setOptions
  1744. */
  1745. public Object[] getOptions() {
  1746. if(options != null) {
  1747. int optionCount = options.length;
  1748. Object[] retOptions = new Object[optionCount];
  1749. System.arraycopy(options, 0, retOptions, 0, optionCount);
  1750. return retOptions;
  1751. }
  1752. return options;
  1753. }
  1754. /**
  1755. * Sets the initial value that is to be enabled -- the
  1756. * <code>Component</code>
  1757. * that has the focus when the pane is initially displayed.
  1758. *
  1759. * @param newInitialValue the <code>Object</code> that gets the initial
  1760. * keyboard focus
  1761. *
  1762. * @see #getInitialValue
  1763. * @beaninfo
  1764. * preferred: true
  1765. * bound: true
  1766. * description: The option pane's initial value object.
  1767. */
  1768. public void setInitialValue(Object newInitialValue) {
  1769. Object oldIV = initialValue;
  1770. initialValue = newInitialValue;
  1771. firePropertyChange(INITIAL_VALUE_PROPERTY, oldIV, initialValue);
  1772. }
  1773. /**
  1774. * Returns the initial value.
  1775. *
  1776. * @return the <code>Object</code> that gets the initial keyboard focus
  1777. *
  1778. * @see #setInitialValue
  1779. */
  1780. public Object getInitialValue() {
  1781. return initialValue;
  1782. }
  1783. /**
  1784. * Sets the option pane's message type.
  1785. * The message type is used by the Look and Feel to determine the
  1786. * icon to display (if not supplied) as well as potentially how to
  1787. * lay out the <code>parentComponent</code>.
  1788. * @param newType an integer specifying the kind of message to display:
  1789. * <code>ERROR_MESSAGE</code>, <code>INFORMATION_MESSAGE</code>,
  1790. * <code>WARNING_MESSAGE</code>,
  1791. * <code>QUESTION_MESSAGE</code>, or <code>PLAIN_MESSAGE</code>
  1792. * @exception RuntimeException if <code>newType</code> is not one of the
  1793. * legal values listed above
  1794. * @see #getMessageType
  1795. * @beaninfo
  1796. * preferred: true
  1797. * bound: true
  1798. * description: The option pane's message type.
  1799. */
  1800. public void setMessageType(int newType) {
  1801. if(newType != ERROR_MESSAGE && newType != INFORMATION_MESSAGE &&
  1802. newType != WARNING_MESSAGE && newType != QUESTION_MESSAGE &&
  1803. newType != PLAIN_MESSAGE)
  1804. throw new RuntimeException("JOptionPane: type must be one of JOptionPane.ERROR_MESSAGE, JOptionPane.INFORMATION_MESSAGE, JOptionPane.WARNING_MESSAGE, JOptionPane.QUESTION_MESSAGE or JOptionPane.PLAIN_MESSAGE");
  1805. int oldType = messageType;
  1806. messageType = newType;
  1807. firePropertyChange(MESSAGE_TYPE_PROPERTY, oldType, messageType);
  1808. }
  1809. /**
  1810. * Returns the message type.
  1811. *
  1812. * @return an integer specifying the message type
  1813. *
  1814. * @see #setMessageType
  1815. */
  1816. public int getMessageType() {
  1817. return messageType;
  1818. }
  1819. /**
  1820. * Sets the options to display.
  1821. * The option type is used by the Look and Feel to
  1822. * determine what buttons to show (unless options are supplied).
  1823. * @param newType an integer specifying the options the L&F is to display:
  1824. * <code>DEFAULT_OPTION</code>,
  1825. * <code>YES_NO_OPTION</code>,
  1826. * <code>YES_NO_CANCEL_OPTION</code>,
  1827. * or <code>OK_CANCEL_OPTION</code>
  1828. * @exception RuntimeException if <code>newType</code> is not one of
  1829. * the legal values listed above
  1830. *
  1831. * @see #getOptionType
  1832. * @see #setOptions
  1833. * @beaninfo
  1834. * preferred: true
  1835. * bound: true
  1836. * description: The option pane's option type.
  1837. */
  1838. public void setOptionType(int newType) {
  1839. if(newType != DEFAULT_OPTION && newType != YES_NO_OPTION &&
  1840. newType != YES_NO_CANCEL_OPTION && newType != OK_CANCEL_OPTION)
  1841. throw new RuntimeException("JOptionPane: option type must be one of JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION, JOptionPane.YES_NO_CANCEL_OPTION or JOptionPane.OK_CANCEL_OPTION");
  1842. int oldType = optionType;
  1843. optionType = newType;
  1844. firePropertyChange(OPTION_TYPE_PROPERTY, oldType, optionType);
  1845. }
  1846. /**
  1847. * Returns the type of options that are displayed.
  1848. *
  1849. * @return an integer specifying the user-selectable options
  1850. *
  1851. * @see #setOptionType
  1852. */
  1853. public int getOptionType() {
  1854. return optionType;
  1855. }
  1856. /**
  1857. * Sets the input selection values for a pane that provides the user
  1858. * with a list of items to choose from. (The UI provides a widget
  1859. * for choosing one of the values.) A <code>null</code> value
  1860. * implies the user can input whatever they wish, usually by means
  1861. * of a <code>JTextField</code>.
  1862. * <p>
  1863. * Sets <code>wantsInput</code> to true. Use
  1864. * <code>setInitialSelectionValue</code> to specify the initially-chosen
  1865. * value. After the pane as been enabled, <code>inputValue</code> is
  1866. * set to the value the user has selected.
  1867. * @param newValues an array of <code>Objects</code> the user to be
  1868. * displayed
  1869. * (usually in a list or combo-box) from which
  1870. * the user can make a selection
  1871. * @see #setWantsInput
  1872. * @see #setInitialSelectionValue
  1873. * @see #getSelectionValues
  1874. * @beaninfo
  1875. * bound: true
  1876. * description: The option pane's selection values.
  1877. */
  1878. public void setSelectionValues(Object[] newValues) {
  1879. Object[] oldValues = selectionValues;
  1880. selectionValues = newValues;
  1881. firePropertyChange(SELECTION_VALUES_PROPERTY, oldValues, newValues);
  1882. if(selectionValues != null)
  1883. setWantsInput(true);
  1884. }
  1885. /**
  1886. * Returns the input selection values.
  1887. *
  1888. * @return the array of <code>Objects</code> the user can select
  1889. * @see #setSelectionValues
  1890. */
  1891. public Object[] getSelectionValues() {
  1892. return selectionValues;
  1893. }
  1894. /**
  1895. * Sets the input value that is initially displayed as selected to the user.
  1896. * Only used if <code>wantsInput</code> is true.
  1897. * @param newValue the initially selected value
  1898. * @see #setSelectionValues
  1899. * @see #getInitialSelectionValue
  1900. * @beaninfo
  1901. * bound: true
  1902. * description: The option pane's initial selection value object.
  1903. */
  1904. public void setInitialSelectionValue(Object newValue) {
  1905. Object oldValue = initialSelectionValue;
  1906. initialSelectionValue = newValue;
  1907. firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, oldValue,
  1908. newValue);
  1909. }
  1910. /**
  1911. * Returns the input value that is displayed as initially selected to the user.
  1912. *
  1913. * @return the initially selected value
  1914. * @see #setInitialSelectionValue
  1915. * @see #setSelectionValues
  1916. */
  1917. public Object getInitialSelectionValue() {
  1918. return initialSelectionValue;
  1919. }
  1920. /**
  1921. * Sets the input value that was selected or input by the user.
  1922. * Only used if <code>wantsInput</code> is true. Note that this method
  1923. * is invoked internally by the option pane (in response to user action)
  1924. * and should generally not be called by client programs. To set the
  1925. * input value initially displayed as selected to the user, use
  1926. * <code>setInitialSelectionValue</code>.
  1927. *
  1928. * @param newValue the <code>Object</code> used to set the
  1929. * value that the user specified (usually in a text field)
  1930. * @see #setSelectionValues
  1931. * @see #setInitialSelectionValue
  1932. * @see #setWantsInput
  1933. * @see #getInputValue
  1934. * @beaninfo
  1935. * preferred: true
  1936. * bound: true
  1937. * description: The option pane's input value object.
  1938. */
  1939. public void setInputValue(Object newValue) {
  1940. Object oldValue = inputValue;
  1941. inputValue = newValue;
  1942. firePropertyChange(INPUT_VALUE_PROPERTY, oldValue, newValue);
  1943. }
  1944. /**
  1945. * Returns the value the user has input, if <code>wantsInput</code>
  1946. * is true.
  1947. *
  1948. * @return the <code>Object</code> the user specified,
  1949. * if it was one of the objects, or a
  1950. * <code>String</code> if it was a value typed into a
  1951. * field
  1952. * @see #setSelectionValues
  1953. * @see #setWantsInput
  1954. * @see #setInputValue
  1955. */
  1956. public Object getInputValue() {
  1957. return inputValue;
  1958. }
  1959. /**
  1960. * Returns the maximum number of characters to place on a line in a
  1961. * message. Default is to return <code>Integer.MAX_VALUE</code>.
  1962. * The value can be
  1963. * changed by overriding this method in a subclass.
  1964. *
  1965. * @return an integer giving the maximum number of characters on a line
  1966. */
  1967. public int getMaxCharactersPerLineCount() {
  1968. return Integer.MAX_VALUE;
  1969. }
  1970. /**
  1971. * Sets the <code>wantsInput</code> property.
  1972. * If <code>newValue</code> is true, an input component
  1973. * (such as a text field or combo box) whose parent is
  1974. * <code>parentComponent</code> is provided to
  1975. * allow the user to input a value. If <code>getSelectionValues</code>
  1976. * returns a non-<code>null</code> array, the input value is one of the
  1977. * objects in that array. Otherwise the input value is whatever
  1978. * the user inputs.
  1979. * <p>
  1980. * This is a bound property.
  1981. *
  1982. * @see #setSelectionValues
  1983. * @see #setInputValue
  1984. * @beaninfo
  1985. * preferred: true
  1986. * bound: true
  1987. * description: Flag which allows the user to input a value.
  1988. */
  1989. public void setWantsInput(boolean newValue) {
  1990. boolean oldValue = wantsInput;
  1991. wantsInput = newValue;
  1992. firePropertyChange(WANTS_INPUT_PROPERTY, oldValue, newValue);
  1993. }
  1994. /**
  1995. * Returns the value of the <code>wantsInput</code> property.
  1996. *
  1997. * @return true if an input component will be provided
  1998. * @see #setWantsInput
  1999. */
  2000. public boolean getWantsInput() {
  2001. return wantsInput;
  2002. }
  2003. /**
  2004. * Requests that the initial value be selected, which will set
  2005. * focus to the initial value. This method
  2006. * should be invoked after the window containing the option pane
  2007. * is made visible.
  2008. */
  2009. public void selectInitialValue() {
  2010. OptionPaneUI ui = getUI();
  2011. if (ui != null) {
  2012. ui.selectInitialValue(this);
  2013. }
  2014. }
  2015. private static int styleFromMessageType(int messageType) {
  2016. switch (messageType) {
  2017. case ERROR_MESSAGE:
  2018. return JRootPane.ERROR_DIALOG;
  2019. case QUESTION_MESSAGE:
  2020. return JRootPane.QUESTION_DIALOG;
  2021. case WARNING_MESSAGE:
  2022. return JRootPane.WARNING_DIALOG;
  2023. case INFORMATION_MESSAGE:
  2024. return JRootPane.INFORMATION_DIALOG;
  2025. case PLAIN_MESSAGE:
  2026. default:
  2027. return JRootPane.PLAIN_DIALOG;
  2028. }
  2029. }
  2030. // Serialization support.
  2031. private void writeObject(ObjectOutputStream s) throws IOException {
  2032. Vector values = new Vector();
  2033. s.defaultWriteObject();
  2034. // Save the icon, if its Serializable.
  2035. if(icon != null && icon instanceof Serializable) {
  2036. values.addElement("icon");
  2037. values.addElement(icon);
  2038. }
  2039. // Save the message, if its Serializable.
  2040. if(message != null && message instanceof Serializable) {
  2041. values.addElement("message");
  2042. values.addElement(message);
  2043. }
  2044. // Save the treeModel, if its Serializable.
  2045. if(options != null) {
  2046. Vector serOptions = new Vector();
  2047. for(int counter = 0, maxCounter = options.length;
  2048. counter < maxCounter; counter++)
  2049. if(options[counter] instanceof Serializable)
  2050. serOptions.addElement(options[counter]);
  2051. if(serOptions.size() > 0) {
  2052. int optionCount = serOptions.size();
  2053. Object[] arrayOptions = new Object[optionCount];
  2054. serOptions.copyInto(arrayOptions);
  2055. values.addElement("options");
  2056. values.addElement(arrayOptions);
  2057. }
  2058. }
  2059. // Save the initialValue, if its Serializable.
  2060. if(initialValue != null && initialValue instanceof Serializable) {
  2061. values.addElement("initialValue");
  2062. values.addElement(initialValue);
  2063. }
  2064. // Save the value, if its Serializable.
  2065. if(value != null && value instanceof Serializable) {
  2066. values.addElement("value");
  2067. values.addElement(value);
  2068. }
  2069. // Save the selectionValues, if its Serializable.
  2070. if(selectionValues != null) {
  2071. boolean serialize = true;
  2072. for(int counter = 0, maxCounter = selectionValues.length;
  2073. counter < maxCounter; counter++) {
  2074. if(selectionValues[counter] != null &&
  2075. !(selectionValues[counter] instanceof Serializable)) {
  2076. serialize = false;
  2077. break;
  2078. }
  2079. }
  2080. if(serialize) {
  2081. values.addElement("selectionValues");
  2082. values.addElement(selectionValues);
  2083. }
  2084. }
  2085. // Save the inputValue, if its Serializable.
  2086. if(inputValue != null && inputValue instanceof Serializable) {
  2087. values.addElement("inputValue");
  2088. values.addElement(inputValue);
  2089. }
  2090. // Save the initialSelectionValue, if its Serializable.
  2091. if(initialSelectionValue != null &&
  2092. initialSelectionValue instanceof Serializable) {
  2093. values.addElement("initialSelectionValue");
  2094. values.addElement(initialSelectionValue);
  2095. }
  2096. s.writeObject(values);
  2097. }
  2098. private void readObject(ObjectInputStream s)
  2099. throws IOException, ClassNotFoundException {
  2100. s.defaultReadObject();
  2101. Vector values = (Vector)s.readObject();
  2102. int indexCounter = 0;
  2103. int maxCounter = values.size();
  2104. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  2105. equals("icon")) {
  2106. icon = (Icon)values.elementAt(++indexCounter);
  2107. indexCounter++;
  2108. }
  2109. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  2110. equals("message")) {
  2111. message = values.elementAt(++indexCounter);
  2112. indexCounter++;
  2113. }
  2114. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  2115. equals("options")) {
  2116. options = (Object[])values.elementAt(++indexCounter);
  2117. indexCounter++;
  2118. }
  2119. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  2120. equals("initialValue")) {
  2121. initialValue = values.elementAt(++indexCounter);
  2122. indexCounter++;
  2123. }
  2124. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  2125. equals("value")) {
  2126. value = values.elementAt(++indexCounter);
  2127. indexCounter++;
  2128. }
  2129. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  2130. equals("selectionValues")) {
  2131. selectionValues = (Object[])values.elementAt(++indexCounter);
  2132. indexCounter++;
  2133. }
  2134. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  2135. equals("inputValue")) {
  2136. inputValue = values.elementAt(++indexCounter);
  2137. indexCounter++;
  2138. }
  2139. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  2140. equals("initialSelectionValue")) {
  2141. initialSelectionValue = values.elementAt(++indexCounter);
  2142. indexCounter++;
  2143. }
  2144. if (getUIClassID().equals(uiClassID)) {
  2145. byte count = JComponent.getWriteObjCounter(this);
  2146. JComponent.setWriteObjCounter(this, --count);
  2147. if (count == 0 && ui != null) {
  2148. ui.installUI(this);
  2149. }
  2150. }
  2151. }
  2152. /**
  2153. * Returns a string representation of this <code>JOptionPane</code>.
  2154. * This method
  2155. * is intended to be used only for debugging purposes, and the
  2156. * content and format of the returned string may vary between
  2157. * implementations. The returned string may be empty but may not
  2158. * be <code>null</code>.
  2159. *
  2160. * @return a string representation of this <code>JOptionPane</code>
  2161. */
  2162. protected String paramString() {
  2163. String iconString = (icon != null ?
  2164. icon.toString() : "");
  2165. String initialValueString = (initialValue != null ?
  2166. initialValue.toString() : "");
  2167. String messageString = (message != null ?
  2168. message.toString() : "");
  2169. String messageTypeString;
  2170. if (messageType == ERROR_MESSAGE) {
  2171. messageTypeString = "ERROR_MESSAGE";
  2172. } else if (messageType == INFORMATION_MESSAGE) {
  2173. messageTypeString = "INFORMATION_MESSAGE";
  2174. } else if (messageType == WARNING_MESSAGE) {
  2175. messageTypeString = "WARNING_MESSAGE";
  2176. } else if (messageType == QUESTION_MESSAGE) {
  2177. messageTypeString = "QUESTION_MESSAGE";
  2178. } else if (messageType == PLAIN_MESSAGE) {
  2179. messageTypeString = "PLAIN_MESSAGE";
  2180. } else messageTypeString = "";
  2181. String optionTypeString;
  2182. if (optionType == DEFAULT_OPTION) {
  2183. optionTypeString = "DEFAULT_OPTION";
  2184. } else if (optionType == YES_NO_OPTION) {
  2185. optionTypeString = "YES_NO_OPTION";
  2186. } else if (optionType == YES_NO_CANCEL_OPTION) {
  2187. optionTypeString = "YES_NO_CANCEL_OPTION";
  2188. } else if (optionType == OK_CANCEL_OPTION) {
  2189. optionTypeString = "OK_CANCEL_OPTION";
  2190. } else optionTypeString = "";
  2191. String wantsInputString = (wantsInput ?
  2192. "true" : "false");
  2193. return super.paramString() +
  2194. ",icon=" + iconString +
  2195. ",initialValue=" + initialValueString +
  2196. ",message=" + messageString +
  2197. ",messageType=" + messageTypeString +
  2198. ",optionType=" + optionTypeString +
  2199. ",wantsInput=" + wantsInputString;
  2200. }
  2201. ///////////////////
  2202. // Accessibility support
  2203. ///////////////////
  2204. /**
  2205. * Returns the <code>AccessibleContext</code> associated with this JOptionPane.
  2206. * For option panes, the <code>AccessibleContext</code> takes the form of an
  2207. * <code>AccessibleJOptionPane</code>.
  2208. * A new <code>AccessibleJOptionPane</code> instance is created if necessary.
  2209. *
  2210. * @return an AccessibleJOptionPane that serves as the
  2211. * AccessibleContext of this AccessibleJOptionPane
  2212. * @beaninfo
  2213. * expert: true
  2214. * description: The AccessibleContext associated with this option pane
  2215. */
  2216. public AccessibleContext getAccessibleContext() {
  2217. if (accessibleContext == null) {
  2218. accessibleContext = new AccessibleJOptionPane();
  2219. }
  2220. return accessibleContext;
  2221. }
  2222. /**
  2223. * This class implements accessibility support for the
  2224. * <code>JOptionPane</code> class. It provides an implementation of the
  2225. * Java Accessibility API appropriate to option pane user-interface
  2226. * elements.
  2227. * <p>
  2228. * <strong>Warning:</strong>
  2229. * Serialized objects of this class will not be compatible with
  2230. * future Swing releases. The current serialization support is
  2231. * appropriate for short term storage or RMI between applications running
  2232. * the same version of Swing. As of 1.4, support for long term storage
  2233. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  2234. * has been added to the <code>java.beans</code> package.
  2235. * Please see {@link java.beans.XMLEncoder}.
  2236. */
  2237. protected class AccessibleJOptionPane extends AccessibleJComponent {
  2238. /**
  2239. * Get the role of this object.
  2240. *
  2241. * @return an instance of AccessibleRole describing the role of the object
  2242. * @see AccessibleRole
  2243. */
  2244. public AccessibleRole getAccessibleRole() {
  2245. return AccessibleRole.OPTION_PANE;
  2246. }
  2247. } // inner class AccessibleJOptionPane
  2248. }