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