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