1. /*
  2. * @(#)JDialog.java 1.71 03/01/28
  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.*;
  9. import java.awt.event.*;
  10. import java.beans.PropertyChangeListener;
  11. import java.util.Locale;
  12. import java.util.Vector;
  13. import java.io.Serializable;
  14. import javax.accessibility.*;
  15. import java.applet.Applet;
  16. /**
  17. * The main class for creating a dialog window. You can use this class
  18. * to create a custom dialog, or invoke the many class methods
  19. * in {@link JOptionPane} to create a variety of standard dialogs.
  20. * For information about creating dialogs, see
  21. * <em>The Java Tutorial</em> section
  22. * <a
  23. href="http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html">How
  24. * to Make Dialogs</a>.
  25. *
  26. * <p>
  27. *
  28. * The <code>JDialog</code> component contains a <code>JRootPane</code>
  29. * as its only child.
  30. * The <code>contentPane</code> should be the parent of any children of the
  31. * <code>JDialog</code>. From the older <code>java.awt.Window</code> object
  32. * you would normally do something like this:
  33. * <PRE>
  34. * dialog.add(child);
  35. * </PRE>
  36. * Using <code>JDialog</code> the proper semantic is:
  37. * <PRE>
  38. * dialog.getContentPane().add(child);
  39. * </PRE>
  40. * The same principle holds true for setting layout managers, removing
  41. * components, listing children, etc. All these methods should normally be
  42. * sent to the <code>contentPane</code> instead of to the <code>JDialog</code>.
  43. * The <code>contentPane</code> is always non-<code>null</code>.
  44. * Attempting to set it to <code>null</code> generates an exception.
  45. * The default <code>contentPane</code> has a <code>BorderLayout</code>
  46. * manager set on it.
  47. * <p>
  48. * Please see the <code>JRootPane</code> documentation for a complete
  49. * description of the <code>contentPane</code>, <code>glassPane</code>,
  50. * and <code>layeredPane</code> components.
  51. * <p>
  52. * In a multi-screen environment, you can create a <code>JDialog</code>
  53. * on a different screen device than its owner. See {@link java.awt.Frame} for
  54. * more information.
  55. * <p>
  56. * For the keyboard keys used by this component in the standard Look and
  57. * Feel (L&F) renditions, see the
  58. * <a href="doc-files/Key-Index.html#JDialog"><code>JDialog</code> key assignments</a>.
  59. * <p>
  60. * <strong>Warning:</strong>
  61. * Serialized objects of this class will not be compatible with
  62. * future Swing releases. The current serialization support is
  63. * appropriate for short term storage or RMI between applications running
  64. * the same version of Swing. As of 1.4, support for long term storage
  65. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  66. * has been added to the <code>java.beans</code> package.
  67. * Please see {@link java.beans.XMLEncoder}.
  68. *
  69. * @see JOptionPane
  70. * @see JRootPane
  71. *
  72. * @beaninfo
  73. * attribute: isContainer true
  74. * attribute: containerDelegate getContentPane
  75. * description: A toplevel window for creating dialog boxes.
  76. *
  77. * @version 1.71 01/28/03
  78. * @author David Kloba
  79. * @author James Gosling
  80. * @author Scott Violet
  81. */
  82. public class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneContainer
  83. {
  84. /**
  85. * Key into the AppContext, used to check if should provide decorations
  86. * by default.
  87. */
  88. private static final Object defaultLookAndFeelDecoratedKey =
  89. new StringBuffer("JDialog.defaultLookAndFeelDecorated");
  90. private int defaultCloseOperation = HIDE_ON_CLOSE;
  91. /**
  92. * @see #getRootPane
  93. * @see #setRootPane
  94. */
  95. protected JRootPane rootPane;
  96. /**
  97. * @see #isRootPaneCheckingEnabled
  98. * @see #setRootPaneCheckingEnabled
  99. */
  100. protected boolean rootPaneCheckingEnabled = false;
  101. /**
  102. * Creates a non-modal dialog without a title and without a specified
  103. * <code>Frame</code> owner. A shared, hidden frame will be
  104. * set as the owner of the dialog.
  105. * <p>
  106. * This constructor sets the component's locale property to the value
  107. * returned by <code>JComponent.getDefaultLocale</code>.
  108. *
  109. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  110. * returns true.
  111. * @see java.awt.GraphicsEnvironment#isHeadless
  112. * @see JComponent#getDefaultLocale
  113. */
  114. public JDialog() throws HeadlessException {
  115. this((Frame)null, false);
  116. }
  117. /**
  118. * Creates a non-modal dialog without a title with the
  119. * specified <code>Frame</code> as its owner. If <code>owner</code>
  120. * is <code>null</code>, a shared, hidden frame will be set as the
  121. * owner of the dialog.
  122. * <p>
  123. * This constructor sets the component's locale property to the value
  124. * returned by <code>JComponent.getDefaultLocale</code>.
  125. *
  126. * @param owner the <code>Frame</code> from which the dialog is displayed
  127. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  128. * returns true.
  129. * @see java.awt.GraphicsEnvironment#isHeadless
  130. * @see JComponent#getDefaultLocale
  131. */
  132. public JDialog(Frame owner) throws HeadlessException {
  133. this(owner, false);
  134. }
  135. /**
  136. * Creates a modal or non-modal dialog without a title and
  137. * with the specified owner <code>Frame</code>. If <code>owner</code>
  138. * is <code>null</code>, a shared, hidden frame will be set as the
  139. * owner of the dialog.
  140. * <p>
  141. * This constructor sets the component's locale property to the value
  142. * returned by <code>JComponent.getDefaultLocale</code>.
  143. *
  144. * @param owner the <code>Frame</code> from which the dialog is displayed
  145. * @param modal true for a modal dialog, false for one that allows
  146. * others windows to be active at the same time
  147. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  148. * returns true.
  149. * @see java.awt.GraphicsEnvironment#isHeadless
  150. * @see JComponent#getDefaultLocale
  151. */
  152. public JDialog(Frame owner, boolean modal) throws HeadlessException {
  153. this(owner, null, modal);
  154. }
  155. /**
  156. * Creates a non-modal dialog with the specified title and
  157. * with the specified owner frame. If <code>owner</code>
  158. * is <code>null</code>, a shared, hidden frame will be set as the
  159. * owner of the dialog.
  160. * <p>
  161. * This constructor sets the component's locale property to the value
  162. * returned by <code>JComponent.getDefaultLocale</code>.
  163. *
  164. * @param owner the <code>Frame</code> from which the dialog is displayed
  165. * @param title the <code>String</code> to display in the dialog's
  166. * title bar
  167. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  168. * returns true.
  169. * @see java.awt.GraphicsEnvironment#isHeadless
  170. * @see JComponent#getDefaultLocale
  171. */
  172. public JDialog(Frame owner, String title) throws HeadlessException {
  173. this(owner, title, false);
  174. }
  175. /**
  176. * Creates a modal or non-modal dialog with the specified title
  177. * and the specified owner <code>Frame</code>. If <code>owner</code>
  178. * is <code>null</code>, a shared, hidden frame will be set as the
  179. * owner of this dialog. All constructors defer to this one.
  180. * <p>
  181. * NOTE: Any popup components (<code>JComboBox</code>,
  182. * <code>JPopupMenu</code>, <code>JMenuBar</code>)
  183. * created within a modal dialog will be forced to be lightweight.
  184. * <p>
  185. * This constructor sets the component's locale property to the value
  186. * returned by <code>JComponent.getDefaultLocale</code>.
  187. *
  188. * @param owner the <code>Frame</code> from which the dialog is displayed
  189. * @param title the <code>String</code> to display in the dialog's
  190. * title bar
  191. * @param modal true for a modal dialog, false for one that allows
  192. * other windows to be active at the same time
  193. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  194. * returns true.
  195. * @see java.awt.GraphicsEnvironment#isHeadless
  196. * @see JComponent#getDefaultLocale
  197. */
  198. public JDialog(Frame owner, String title, boolean modal)
  199. throws HeadlessException {
  200. super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner,
  201. title, modal);
  202. dialogInit();
  203. }
  204. /**
  205. * Creates a modal or non-modal dialog with the specified title,
  206. * owner <code>Frame</code>, and <code>GraphicsConfiguration</code>.
  207. *
  208. * <p>
  209. * NOTE: Any popup components (<code>JComboBox</code>,
  210. * <code>JPopupMenu</code>, <code>JMenuBar</code>)
  211. * created within a modal dialog will be forced to be lightweight.
  212. * <p>
  213. * This constructor sets the component's locale property to the value
  214. * returned by <code>JComponent.getDefaultLocale</code>.
  215. *
  216. * @param owner the <code>Frame</code> from which the dialog is displayed
  217. * @param title the <code>String</code> to display in the dialog's
  218. * title bar
  219. * @param modal true for a modal dialog, false for one that allows
  220. * other windows to be active at the same time
  221. * @param gc the <code>GraphicsConfiguration</code>
  222. * of the target screen device. If <code>gc</code> is
  223. * <code>null</code>, the same
  224. * <code>GraphicsConfiguration</code> as the owning Frame is used.
  225. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  226. * returns true.
  227. * @see java.awt.GraphicsEnvironment#isHeadless
  228. * @see JComponent#getDefaultLocale
  229. * @since 1.4
  230. */
  231. public JDialog(Frame owner, String title, boolean modal,
  232. GraphicsConfiguration gc) {
  233. super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner,
  234. title, modal, gc);
  235. dialogInit();
  236. }
  237. /**
  238. * Creates a non-modal dialog without a title with the
  239. * specified <code>Dialog</code> as its owner.
  240. * <p>
  241. * This constructor sets the component's locale property to the value
  242. * returned by <code>JComponent.getDefaultLocale</code>.
  243. *
  244. * @param owner the non-null <code>Dialog</code> from which the dialog is displayed
  245. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  246. * returns true.
  247. * @see java.awt.GraphicsEnvironment#isHeadless
  248. * @see JComponent#getDefaultLocale
  249. */
  250. public JDialog(Dialog owner) throws HeadlessException {
  251. this(owner, false);
  252. }
  253. /**
  254. * Creates a modal or non-modal dialog without a title and
  255. * with the specified owner dialog.
  256. * <p>
  257. * This constructor sets the component's locale property to the value
  258. * returned by <code>JComponent.getDefaultLocale</code>.
  259. *
  260. * @param owner the non-null <code>Dialog</code> from which the dialog is displayed
  261. * @param modal true for a modal dialog, false for one that allows
  262. * other windows to be active at the same time
  263. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  264. * returns true.
  265. * @see java.awt.GraphicsEnvironment#isHeadless
  266. * @see JComponent#getDefaultLocale
  267. */
  268. public JDialog(Dialog owner, boolean modal) throws HeadlessException {
  269. this(owner, null, modal);
  270. }
  271. /**
  272. * Creates a non-modal dialog with the specified title and
  273. * with the specified owner dialog.
  274. * <p>
  275. * This constructor sets the component's locale property to the value
  276. * returned by <code>JComponent.getDefaultLocale</code>.
  277. *
  278. * @param owner the non-null <code>Dialog</code> from which the dialog is displayed
  279. * @param title the <code>String</code> to display in the dialog's
  280. * title bar
  281. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  282. * returns true.
  283. * @see java.awt.GraphicsEnvironment#isHeadless
  284. * @see JComponent#getDefaultLocale
  285. */
  286. public JDialog(Dialog owner, String title) throws HeadlessException {
  287. this(owner, title, false);
  288. }
  289. /**
  290. * Creates a modal or non-modal dialog with the specified title
  291. * and the specified owner frame.
  292. * <p>
  293. * This constructor sets the component's locale property to the value
  294. * returned by <code>JComponent.getDefaultLocale</code>.
  295. *
  296. * @param owner the non-null <code>Dialog</code> from which the dialog is displayed
  297. * @param title the <code>String</code> to display in the dialog's
  298. * title bar
  299. * @param modal true for a modal dialog, false for one that allows
  300. * other windows to be active at the same time
  301. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  302. * returns true.
  303. * @see java.awt.GraphicsEnvironment#isHeadless
  304. * @see JComponent#getDefaultLocale
  305. */
  306. public JDialog(Dialog owner, String title, boolean modal)
  307. throws HeadlessException {
  308. super(owner, title, modal);
  309. dialogInit();
  310. }
  311. /**
  312. * Creates a modal or non-modal dialog with the specified title,
  313. * owner <code>Dialog</code>, and <code>GraphicsConfiguration</code>.
  314. *
  315. * <p>
  316. * NOTE: Any popup components (<code>JComboBox</code>,
  317. * <code>JPopupMenu</code>, <code>JMenuBar</code>)
  318. * created within a modal dialog will be forced to be lightweight.
  319. * <p>
  320. * This constructor sets the component's locale property to the value
  321. * returned by <code>JComponent.getDefaultLocale</code>.
  322. *
  323. * @param owner the <code>Dialog</code> from which the dialog is displayed
  324. * @param title the <code>String</code> to display in the dialog's
  325. * title bar
  326. * @param modal true for a modal dialog, false for one that allows
  327. * other windows to be active at the same time
  328. * @param gc the <code>GraphicsConfiguration</code>
  329. * of the target screen device. If <code>gc</code> is
  330. * <code>null</code>, the same
  331. * <code>GraphicsConfiguration</code> as the owning Dialog is used.
  332. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  333. * @see java.awt.GraphicsEnvironment#isHeadless
  334. * @see JComponent#getDefaultLocale
  335. * returns true.
  336. * @since 1.4
  337. */
  338. public JDialog(Dialog owner, String title, boolean modal,
  339. GraphicsConfiguration gc) throws HeadlessException {
  340. super(owner, title, modal, gc);
  341. dialogInit();
  342. }
  343. /**
  344. * Called by the constructors to init the <code>JDialog</code> properly.
  345. */
  346. protected void dialogInit() {
  347. enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
  348. setLocale( JComponent.getDefaultLocale() );
  349. setRootPane(createRootPane());
  350. setRootPaneCheckingEnabled(true);
  351. if (JDialog.isDefaultLookAndFeelDecorated()) {
  352. boolean supportsWindowDecorations =
  353. UIManager.getLookAndFeel().getSupportsWindowDecorations();
  354. if (supportsWindowDecorations) {
  355. setUndecorated(true);
  356. getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  357. }
  358. }
  359. setFocusTraversalPolicy(KeyboardFocusManager.
  360. getCurrentKeyboardFocusManager().
  361. getDefaultFocusTraversalPolicy());
  362. }
  363. /**
  364. * Called by the constructor methods to create the default
  365. * <code>rootPane</code>.
  366. */
  367. protected JRootPane createRootPane() {
  368. return new JRootPane();
  369. }
  370. /**
  371. * Handles window events depending on the state of the
  372. * <code>defaultCloseOperation</code> property.
  373. *
  374. * @see #setDefaultCloseOperation
  375. */
  376. protected void processWindowEvent(WindowEvent e) {
  377. super.processWindowEvent(e);
  378. if (e.getID() == WindowEvent.WINDOW_CLOSING) {
  379. switch(defaultCloseOperation) {
  380. case HIDE_ON_CLOSE:
  381. setVisible(false);
  382. break;
  383. case DISPOSE_ON_CLOSE:
  384. setVisible(false);
  385. dispose();
  386. break;
  387. case DO_NOTHING_ON_CLOSE:
  388. default:
  389. break;
  390. }
  391. }
  392. }
  393. /**
  394. * Sets the operation which will happen by default when
  395. * the user initiates a "close" on this dialog.
  396. * The possible choices are:
  397. * <ul>
  398. * <li><code>DO_NOTHING_ON_CLOSE</code> - do not do anything - require the
  399. * program to handle the operation in the <code>windowClosing</code>
  400. * method of a registered <code>WindowListener</code> object.
  401. * <li><code>HIDE_ON_CLOSE</code> - automatically hide the dialog after
  402. * invoking any registered <code>WindowListener</code> objects
  403. * <li><code>DISPOSE_ON_CLOSE</code> - automatically hide and dispose the
  404. * dialog after invoking any registered <code>WindowListener</code> objects
  405. * </ul>
  406. * <p>
  407. * The value is set to <code>HIDE_ON_CLOSE</code> by default.
  408. * <p>
  409. * <b>Note</b>: When the last displayable window within the
  410. * Java virtual machine (VM) is disposed of, the VM may
  411. * terminate. See <a href="../../java/awt/doc-files/AWTThreadIssues.html">
  412. * AWT Threading Issues</a> for more information.
  413. * @see #addWindowListener
  414. * @see #getDefaultCloseOperation
  415. *
  416. * @beaninfo
  417. * preferred: true
  418. * description: The dialog's default close operation.
  419. */
  420. public void setDefaultCloseOperation(int operation) {
  421. this.defaultCloseOperation = operation;
  422. }
  423. /**
  424. * Returns the operation which occurs when the user
  425. * initiates a "close" on this dialog.
  426. *
  427. * @return an integer indicating the window-close operation
  428. * @see #setDefaultCloseOperation
  429. */
  430. public int getDefaultCloseOperation() {
  431. return defaultCloseOperation;
  432. }
  433. /**
  434. * Calls <code>paint(g)</code>. This method was overridden to
  435. * prevent an unnecessary call to clear the background.
  436. *
  437. * @param g the <code>Graphics</code> context in which to paint
  438. */
  439. public void update(Graphics g) {
  440. paint(g);
  441. }
  442. /**
  443. * Sets the menubar for this dialog.
  444. *
  445. * @param menu the menubar being placed in the dialog
  446. *
  447. * @see #getJMenuBar
  448. *
  449. * @beaninfo
  450. * hidden: true
  451. * description: The menubar for accessing pulldown menus from this dialog.
  452. */
  453. public void setJMenuBar(JMenuBar menu) {
  454. getRootPane().setMenuBar(menu);
  455. }
  456. /**
  457. * Returns the menubar set on this dialog.
  458. *
  459. * @see #setJMenuBar
  460. */
  461. public JMenuBar getJMenuBar() {
  462. return getRootPane().getMenuBar();
  463. }
  464. /**
  465. * Returns true if the methods <code>add</code> and <code>setLayout</code>
  466. * should be checked.
  467. *
  468. * @return true if <code>add</code> and <code>setLayout</code> should
  469. * be checked
  470. * @see #addImpl
  471. * @see #setLayout
  472. * @see #setRootPaneCheckingEnabled
  473. */
  474. protected boolean isRootPaneCheckingEnabled() {
  475. return rootPaneCheckingEnabled;
  476. }
  477. /**
  478. * If true then calls to <code>add</code> and <code>setLayout</code>
  479. * will cause an exception to be thrown.
  480. *
  481. * @see #addImpl
  482. * @see #setLayout
  483. * @see #isRootPaneCheckingEnabled
  484. * @beaninfo
  485. * hidden: true
  486. * description: Whether the add and setLayout methods throw exceptions when invoked.
  487. */
  488. protected void setRootPaneCheckingEnabled(boolean enabled) {
  489. rootPaneCheckingEnabled = enabled;
  490. }
  491. /**
  492. * Creates a message that can be used as a runtime exception. The
  493. * message will look like the following:
  494. * <pre>
  495. * "Do not use JDialog.add() use JDialog.getContentPane().add() instead"
  496. * </pre>
  497. * @param op a <code>String</code> containing the attempted operation
  498. * @return an <code>Error</code> containing the constructed string
  499. */
  500. private Error createRootPaneException(String op) {
  501. String type = getClass().getName();
  502. return new Error(
  503. "Do not use " + type + "." + op + "() use "
  504. + type + ".getContentPane()." + op + "() instead");
  505. }
  506. /**
  507. * By default, children may not be added directly to this component,
  508. * they must be added to its <code>contentPane</code> instead.
  509. * For example:
  510. * <pre>
  511. * thisComponent.getContentPane().add(child)
  512. * </pre>
  513. * An attempt to add to directly to this component will cause an
  514. * runtime exception to be thrown if <code>rootPaneCheckingEnabled</code>
  515. * is true. Subclasses can disable this behavior.
  516. *
  517. * @param comp the <code>Component</code> to be enhanced
  518. * @param constraints the constraints to be respected
  519. * @param index the index (an integer)
  520. * @see #setRootPaneCheckingEnabled
  521. * @exception Error if called with rootPaneCheckingEnabled true
  522. */
  523. protected void addImpl(Component comp, Object constraints, int index)
  524. {
  525. if(isRootPaneCheckingEnabled()) {
  526. throw createRootPaneException("add");
  527. }
  528. else {
  529. super.addImpl(comp, constraints, index);
  530. }
  531. }
  532. /**
  533. * Removes the specified component from this container.
  534. *
  535. * @param comp the component to be removed
  536. * @see #add
  537. */
  538. public void remove(Component comp) {
  539. if (comp == rootPane) {
  540. super.remove(comp);
  541. } else {
  542. // Client mistake, but we need to handle it to avoid a
  543. // common object leak in client applications.
  544. getContentPane().remove(comp);
  545. }
  546. }
  547. /**
  548. * By default the layout of this component may not be set,
  549. * the layout of its <code>contentPane</code> should be set instead.
  550. * For example:
  551. * <pre>
  552. * thisComponent.getContentPane().setLayout(new GridLayout(1, 2))
  553. * </pre>
  554. * An attempt to set the layout of this component will cause an
  555. * runtime exception to be thrown if <code>rootPaneCheckingEnabled</code>
  556. * is true. Subclasses can disable this behavior.
  557. *
  558. * @see #setRootPaneCheckingEnabled
  559. * @param manager the <code>LayoutManager</code>
  560. * @exception Error if called with rootPaneChecking true
  561. */
  562. public void setLayout(LayoutManager manager) {
  563. if(isRootPaneCheckingEnabled()) {
  564. throw createRootPaneException("setLayout");
  565. }
  566. else {
  567. super.setLayout(manager);
  568. }
  569. }
  570. /**
  571. * Returns the <code>rootPane</code> object for this dialog.
  572. *
  573. * @see #setRootPane
  574. * @see RootPaneContainer#getRootPane
  575. */
  576. public JRootPane getRootPane() {
  577. return rootPane;
  578. }
  579. /**
  580. * Sets the <code>rootPane</code> property.
  581. * This method is called by the constructor.
  582. *
  583. * @param root the <code>rootPane</code> object for this dialog
  584. *
  585. * @see #getRootPane
  586. *
  587. * @beaninfo
  588. * hidden: true
  589. * description: the RootPane object for this dialog.
  590. */
  591. protected void setRootPane(JRootPane root) {
  592. if(rootPane != null) {
  593. remove(rootPane);
  594. }
  595. rootPane = root;
  596. if(rootPane != null) {
  597. boolean checkingEnabled = isRootPaneCheckingEnabled();
  598. try {
  599. setRootPaneCheckingEnabled(false);
  600. add(rootPane, BorderLayout.CENTER);
  601. }
  602. finally {
  603. setRootPaneCheckingEnabled(checkingEnabled);
  604. }
  605. }
  606. }
  607. /**
  608. * Returns the <code>contentPane</code> object for this dialog.
  609. *
  610. * @return the <code>contentPane</code> property
  611. *
  612. * @see #setContentPane
  613. * @see RootPaneContainer#getContentPane
  614. */
  615. public Container getContentPane() {
  616. return getRootPane().getContentPane();
  617. }
  618. /**
  619. * Sets the <code>contentPane</code> property.
  620. * This method is called by the constructor.
  621. * <p>
  622. * Swing's painting architecture requires an opaque <code>JComponent</code>
  623. * in the containment hiearchy. This is typically provided by the
  624. * content pane. If you replace the content pane it is recommended you
  625. * replace it with an opaque <code>JComponent</code>.
  626. * @see JRootPane
  627. *
  628. * @param contentPane the <code>contentPane</code> object for this dialog
  629. *
  630. * @exception java.awt.IllegalComponentStateException (a runtime
  631. * exception) if the content pane parameter is <code>null</code>
  632. * @see #getContentPane
  633. * @see RootPaneContainer#setContentPane
  634. *
  635. * @beaninfo
  636. * hidden: true
  637. * description: The client area of the dialog where child
  638. * components are normally inserted.
  639. */
  640. public void setContentPane(Container contentPane) {
  641. getRootPane().setContentPane(contentPane);
  642. }
  643. /**
  644. * Returns the <code>layeredPane</code> object for this dialog.
  645. *
  646. * @return the <code>layeredPane</code> property
  647. *
  648. * @see #setLayeredPane
  649. * @see RootPaneContainer#getLayeredPane
  650. */
  651. public JLayeredPane getLayeredPane() {
  652. return getRootPane().getLayeredPane();
  653. }
  654. /**
  655. * Sets the <code>layeredPane</code> property.
  656. * This method is called by the constructor.
  657. *
  658. * @param layeredPane the new <code>layeredPane</code> property
  659. *
  660. * @exception java.awt.IllegalComponentStateException (a runtime
  661. * exception) if the layered pane parameter is null
  662. * @see #getLayeredPane
  663. * @see RootPaneContainer#setLayeredPane
  664. *
  665. * @beaninfo
  666. * hidden: true
  667. * description: The pane which holds the various dialog layers.
  668. */
  669. public void setLayeredPane(JLayeredPane layeredPane) {
  670. getRootPane().setLayeredPane(layeredPane);
  671. }
  672. /**
  673. * Returns the <code>glassPane</code> object for this dialog.
  674. *
  675. * @return the <code>glassPane</code> property
  676. *
  677. * @see #setGlassPane
  678. * @see RootPaneContainer#getGlassPane
  679. */
  680. public Component getGlassPane() {
  681. return getRootPane().getGlassPane();
  682. }
  683. /**
  684. * Sets the <code>glassPane</code> property.
  685. * This method is called by the constructor.
  686. *
  687. * @param glassPane the <code>glassPane</code> object for this dialog
  688. * @see #getGlassPane
  689. * @see RootPaneContainer#setGlassPane
  690. *
  691. * @beaninfo
  692. * hidden: true
  693. * description: A transparent pane used for menu rendering.
  694. */
  695. public void setGlassPane(Component glassPane) {
  696. getRootPane().setGlassPane(glassPane);
  697. }
  698. /**
  699. * Provides a hint as to whether or not newly created <code>JDialog</code>s
  700. * should have their Window decorations (such as borders, widgets to
  701. * close the window, title...) provided by the current look
  702. * and feel. If <code>defaultLookAndFeelDecorated</code> is true,
  703. * the current <code>LookAndFeel</code> supports providing window
  704. * decorations, and the current window manager supports undecorated
  705. * windows, then newly created <code>JDialog</code>s will have their
  706. * Window decorations provided by the current <code>LookAndFeel</code>.
  707. * Otherwise, newly created <code>JDialog</code>s will have their
  708. * Window decorations provided by the current window manager.
  709. * <p>
  710. * You can get the same effect on a single JDialog by doing the following:
  711. * <pre>
  712. * JDialog dialog = new JDialog();
  713. * dialog.setUndecorated(true);
  714. * dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  715. * </pre>
  716. *
  717. * @param defaultLookAndFeelDecorated A hint as to whether or not current
  718. * look and feel should provide window decorations
  719. * @see javax.swing.LookAndFeel#getSupportsWindowDecorations
  720. * @since 1.4
  721. */
  722. public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) {
  723. if (defaultLookAndFeelDecorated) {
  724. SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE);
  725. } else {
  726. SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE);
  727. }
  728. }
  729. /**
  730. * Returns true if newly created <code>JDialog</code>s should have their
  731. * Window decorations provided by the current look and feel. This is only
  732. * a hint, as certain look and feels may not support this feature.
  733. *
  734. * @return true if look and feel should provide Window decorations.
  735. * @since 1.4
  736. */
  737. public static boolean isDefaultLookAndFeelDecorated() {
  738. Boolean defaultLookAndFeelDecorated =
  739. (Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey);
  740. if (defaultLookAndFeelDecorated == null) {
  741. defaultLookAndFeelDecorated = Boolean.FALSE;
  742. }
  743. return defaultLookAndFeelDecorated.booleanValue();
  744. }
  745. /**
  746. * Returns a string representation of this <code>JDialog</code>.
  747. * This method
  748. * is intended to be used only for debugging purposes, and the
  749. * content and format of the returned string may vary between
  750. * implementations. The returned string may be empty but may not
  751. * be <code>null</code>.
  752. *
  753. * @return a string representation of this <code>JDialog</code>.
  754. */
  755. protected String paramString() {
  756. String defaultCloseOperationString;
  757. if (defaultCloseOperation == HIDE_ON_CLOSE) {
  758. defaultCloseOperationString = "HIDE_ON_CLOSE";
  759. } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
  760. defaultCloseOperationString = "DISPOSE_ON_CLOSE";
  761. } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
  762. defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
  763. } else defaultCloseOperationString = "";
  764. String rootPaneString = (rootPane != null ?
  765. rootPane.toString() : "");
  766. String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
  767. "true" : "false");
  768. return super.paramString() +
  769. ",defaultCloseOperation=" + defaultCloseOperationString +
  770. ",rootPane=" + rootPaneString +
  771. ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
  772. }
  773. /////////////////
  774. // Accessibility support
  775. ////////////////
  776. protected AccessibleContext accessibleContext = null;
  777. /**
  778. * Gets the AccessibleContext associated with this JDialog.
  779. * For JDialogs, the AccessibleContext takes the form of an
  780. * AccessibleJDialog.
  781. * A new AccessibleJDialog instance is created if necessary.
  782. *
  783. * @return an AccessibleJDialog that serves as the
  784. * AccessibleContext of this JDialog
  785. */
  786. public AccessibleContext getAccessibleContext() {
  787. if (accessibleContext == null) {
  788. accessibleContext = new AccessibleJDialog();
  789. }
  790. return accessibleContext;
  791. }
  792. /**
  793. * This class implements accessibility support for the
  794. * <code>JDialog</code> class. It provides an implementation of the
  795. * Java Accessibility API appropriate to dialog user-interface
  796. * elements.
  797. */
  798. protected class AccessibleJDialog extends AccessibleAWTDialog {
  799. // AccessibleContext methods
  800. //
  801. /**
  802. * Get the accessible name of this object.
  803. *
  804. * @return the localized name of the object -- can be null if this
  805. * object does not have a name
  806. */
  807. public String getAccessibleName() {
  808. if (accessibleName != null) {
  809. return accessibleName;
  810. } else {
  811. if (getTitle() == null) {
  812. return super.getAccessibleName();
  813. } else {
  814. return getTitle();
  815. }
  816. }
  817. }
  818. /**
  819. * Get the state of this object.
  820. *
  821. * @return an instance of AccessibleStateSet containing the current
  822. * state set of the object
  823. * @see AccessibleState
  824. */
  825. public AccessibleStateSet getAccessibleStateSet() {
  826. AccessibleStateSet states = super.getAccessibleStateSet();
  827. if (isResizable()) {
  828. states.add(AccessibleState.RESIZABLE);
  829. }
  830. if (getFocusOwner() != null) {
  831. states.add(AccessibleState.ACTIVE);
  832. }
  833. if (isModal()) {
  834. states.add(AccessibleState.MODAL);
  835. }
  836. return states;
  837. }
  838. } // inner class AccessibleJDialog
  839. }