JDialogpublic class JDialog extends Dialog implements WindowConstants, RootPaneContainer, AccessibleThe main class for creating a dialog window. You can use this class
to create a custom dialog, or invoke the many class methods
in {@link JOptionPane} to create a variety of standard dialogs.
For information about creating dialogs, see
The Java Tutorial section
How
to Make Dialogs.
The JDialog component contains a JRootPane
as its only child.
The contentPane should be the parent of any children of the
JDialog .
As a conveniance add and its variants, remove and
setLayout have been overridden to forward to the
contentPane as necessary. This means you can write:
dialog.add(child);
And the child will be added to the contentPane.
The contentPane is always non-null .
Attempting to set it to null generates an exception.
The default contentPane has a BorderLayout
manager set on it.
Refer to {@link javax.swing.RootPaneContainer}
for details on adding, removing and setting the LayoutManager
of a JDialog .
Please see the JRootPane documentation for a complete
description of the contentPane , glassPane ,
and layeredPane components.
In a multi-screen environment, you can create a JDialog
on a different screen device than its owner. See {@link java.awt.Frame} for
more information.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans package.
Please see {@link java.beans.XMLEncoder}. |
Fields Summary |
---|
private static final Object | defaultLookAndFeelDecoratedKeyKey into the AppContext, used to check if should provide decorations
by default. | private int | defaultCloseOperation | protected JRootPane | rootPane | protected boolean | rootPaneCheckingEnabledIf true then calls to add and setLayout
will be forwarded to the contentPane . This is initially
false, but is set to true when the JDialog is constructed. | protected AccessibleContext | accessibleContext |
Constructors Summary |
---|
public JDialog()Creates a non-modal dialog without a title and without a specified
Frame owner. A shared, hidden frame will be
set as the owner of the dialog.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
this((Frame)null, false);
| public JDialog(Dialog owner, String title, boolean modal)Creates a modal or non-modal dialog with the specified title
and the specified owner frame.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
super(owner, title, modal);
dialogInit();
| public JDialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc)Creates a modal or non-modal dialog with the specified title,
owner Dialog , and GraphicsConfiguration .
NOTE: Any popup components (JComboBox ,
JPopupMenu , JMenuBar )
created within a modal dialog will be forced to be lightweight.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
super(owner, title, modal, gc);
dialogInit();
| public JDialog(Frame owner)Creates a non-modal dialog without a title with the
specified Frame as its owner. If owner
is null , a shared, hidden frame will be set as the
owner of the dialog.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
this(owner, false);
| public JDialog(Frame owner, boolean modal)Creates a modal or non-modal dialog without a title and
with the specified owner Frame . If owner
is null , a shared, hidden frame will be set as the
owner of the dialog.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
this(owner, null, modal);
| public JDialog(Frame owner, String title)Creates a non-modal dialog with the specified title and
with the specified owner frame. If owner
is null , a shared, hidden frame will be set as the
owner of the dialog.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
this(owner, title, false);
| public JDialog(Frame owner, String title, boolean modal)Creates a modal or non-modal dialog with the specified title
and the specified owner Frame . If owner
is null , a shared, hidden frame will be set as the
owner of this dialog. All constructors defer to this one.
NOTE: Any popup components (JComboBox ,
JPopupMenu , JMenuBar )
created within a modal dialog will be forced to be lightweight.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner,
title, modal);
if (owner == null) {
WindowListener ownerShutdownListener =
(WindowListener)SwingUtilities.getSharedOwnerFrameShutdownListener();
addWindowListener(ownerShutdownListener);
}
dialogInit();
| public JDialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc)Creates a modal or non-modal dialog with the specified title,
owner Frame , and GraphicsConfiguration .
NOTE: Any popup components (JComboBox ,
JPopupMenu , JMenuBar )
created within a modal dialog will be forced to be lightweight.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner,
title, modal, gc);
if (owner == null) {
WindowListener ownerShutdownListener =
(WindowListener)SwingUtilities.getSharedOwnerFrameShutdownListener();
addWindowListener(ownerShutdownListener);
}
dialogInit();
| public JDialog(Dialog owner)Creates a non-modal dialog without a title with the
specified Dialog as its owner.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
this(owner, false);
| public JDialog(Dialog owner, boolean modal)Creates a modal or non-modal dialog without a title and
with the specified owner dialog.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
this(owner, null, modal);
| public JDialog(Dialog owner, String title)Creates a non-modal dialog with the specified title and
with the specified owner dialog.
This constructor sets the component's locale property to the value
returned by JComponent.getDefaultLocale .
this(owner, title, false);
|
Methods Summary |
---|
protected void | addImpl(java.awt.Component comp, java.lang.Object constraints, int index)Adds the specified child Component .
This method is overridden to conditionally forwad calls to the
contentPane .
By default, children are added to the contentPane instead
of the frame, refer to {@link javax.swing.RootPaneContainer} for
details.
if(isRootPaneCheckingEnabled()) {
getContentPane().add(comp, constraints, index);
}
else {
super.addImpl(comp, constraints, index);
}
| protected javax.swing.JRootPane | createRootPane()Called by the constructor methods to create the default
rootPane .
JRootPane rp = new JRootPane();
// NOTE: this uses setOpaque vs LookAndFeel.installProperty as there
// is NO reason for the RootPane not to be opaque. For painting to
// work the contentPane must be opaque, therefor the RootPane can
// also be opaque.
rp.setOpaque(true);
return rp;
| protected void | dialogInit()Called by the constructors to init the JDialog properly.
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
setLocale( JComponent.getDefaultLocale() );
setRootPane(createRootPane());
setRootPaneCheckingEnabled(true);
if (JDialog.isDefaultLookAndFeelDecorated()) {
boolean supportsWindowDecorations =
UIManager.getLookAndFeel().getSupportsWindowDecorations();
if (supportsWindowDecorations) {
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
}
}
sun.awt.SunToolkit.checkAndSetPolicy(this, true);
| public javax.accessibility.AccessibleContext | getAccessibleContext()Gets the AccessibleContext associated with this JDialog.
For JDialogs, the AccessibleContext takes the form of an
AccessibleJDialog.
A new AccessibleJDialog instance is created if necessary.
if (accessibleContext == null) {
accessibleContext = new AccessibleJDialog();
}
return accessibleContext;
| public java.awt.Container | getContentPane()Returns the contentPane object for this dialog.
return getRootPane().getContentPane();
| public int | getDefaultCloseOperation()Returns the operation which occurs when the user
initiates a "close" on this dialog.
return defaultCloseOperation;
| public java.awt.Component | getGlassPane()Returns the glassPane object for this dialog.
return getRootPane().getGlassPane();
| public javax.swing.JMenuBar | getJMenuBar()Returns the menubar set on this dialog.
return getRootPane().getMenuBar();
| public javax.swing.JLayeredPane | getLayeredPane()Returns the layeredPane object for this dialog.
return getRootPane().getLayeredPane();
| public javax.swing.JRootPane | getRootPane()Returns the rootPane object for this dialog.
return rootPane;
| public static boolean | isDefaultLookAndFeelDecorated()Returns true if newly created JDialog s should have their
Window decorations provided by the current look and feel. This is only
a hint, as certain look and feels may not support this feature.
Boolean defaultLookAndFeelDecorated =
(Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey);
if (defaultLookAndFeelDecorated == null) {
defaultLookAndFeelDecorated = Boolean.FALSE;
}
return defaultLookAndFeelDecorated.booleanValue();
| protected boolean | isRootPaneCheckingEnabled()Returns whether calls to add and
setLayout are forwarded to the contentPane .
return rootPaneCheckingEnabled;
| protected java.lang.String | paramString()Returns a string representation of this JDialog .
This method
is intended to be used only for debugging purposes, and the
content and format of the returned string may vary between
implementations. The returned string may be empty but may not
be null .
String defaultCloseOperationString;
if (defaultCloseOperation == HIDE_ON_CLOSE) {
defaultCloseOperationString = "HIDE_ON_CLOSE";
} else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
defaultCloseOperationString = "DISPOSE_ON_CLOSE";
} else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
} else defaultCloseOperationString = "";
String rootPaneString = (rootPane != null ?
rootPane.toString() : "");
String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
"true" : "false");
return super.paramString() +
",defaultCloseOperation=" + defaultCloseOperationString +
",rootPane=" + rootPaneString +
",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
| protected void | processWindowEvent(java.awt.event.WindowEvent e)Handles window events depending on the state of the
defaultCloseOperation property.
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
switch(defaultCloseOperation) {
case HIDE_ON_CLOSE:
setVisible(false);
break;
case DISPOSE_ON_CLOSE:
setVisible(false);
dispose();
break;
case DO_NOTHING_ON_CLOSE:
default:
break;
}
}
| public void | remove(java.awt.Component comp)Removes the specified component from the container. If
comp is not the rootPane , this will forward
the call to the contentPane . This will do nothing if
comp is not a child of the JDialog or
contentPane .
if (comp == rootPane) {
super.remove(comp);
} else {
getContentPane().remove(comp);
}
| public void | setContentPane(java.awt.Container contentPane)Sets the contentPane property.
This method is called by the constructor.
Swing's painting architecture requires an opaque JComponent
in the containment hiearchy. This is typically provided by the
content pane. If you replace the content pane it is recommended you
replace it with an opaque JComponent .
getRootPane().setContentPane(contentPane);
| public void | setDefaultCloseOperation(int operation)Sets the operation which will happen by default when
the user initiates a "close" on this dialog.
The possible choices are:
DO_NOTHING_ON_CLOSE - do not do anything - require the
program to handle the operation in the windowClosing
method of a registered WindowListener object.
HIDE_ON_CLOSE - automatically hide the dialog after
invoking any registered WindowListener objects
DISPOSE_ON_CLOSE - automatically hide and dispose the
dialog after invoking any registered WindowListener objects
The value is set to HIDE_ON_CLOSE by default.
Note: When the last displayable window within the
Java virtual machine (VM) is disposed of, the VM may
terminate. See
AWT Threading Issues for more information.
this.defaultCloseOperation = operation;
| public static void | setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)Provides a hint as to whether or not newly created JDialog s
should have their Window decorations (such as borders, widgets to
close the window, title...) provided by the current look
and feel. If defaultLookAndFeelDecorated is true,
the current LookAndFeel supports providing window
decorations, and the current window manager supports undecorated
windows, then newly created JDialog s will have their
Window decorations provided by the current LookAndFeel .
Otherwise, newly created JDialog s will have their
Window decorations provided by the current window manager.
You can get the same effect on a single JDialog by doing the following:
JDialog dialog = new JDialog();
dialog.setUndecorated(true);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
if (defaultLookAndFeelDecorated) {
SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE);
} else {
SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE);
}
| public void | setGlassPane(java.awt.Component glassPane)Sets the glassPane property.
This method is called by the constructor.
getRootPane().setGlassPane(glassPane);
| public void | setJMenuBar(javax.swing.JMenuBar menu)Sets the menubar for this dialog.
getRootPane().setMenuBar(menu);
| public void | setLayeredPane(javax.swing.JLayeredPane layeredPane)Sets the layeredPane property.
This method is called by the constructor.
getRootPane().setLayeredPane(layeredPane);
| public void | setLayout(java.awt.LayoutManager manager)Sets the LayoutManager .
Overridden to conditionally forward the call to the
contentPane .
Refer to {@link javax.swing.RootPaneContainer} for
more information.
if(isRootPaneCheckingEnabled()) {
getContentPane().setLayout(manager);
}
else {
super.setLayout(manager);
}
| protected void | setRootPane(javax.swing.JRootPane root)Sets the rootPane property.
This method is called by the constructor.
if(rootPane != null) {
remove(rootPane);
}
rootPane = root;
if(rootPane != null) {
boolean checkingEnabled = isRootPaneCheckingEnabled();
try {
setRootPaneCheckingEnabled(false);
add(rootPane, BorderLayout.CENTER);
}
finally {
setRootPaneCheckingEnabled(checkingEnabled);
}
}
| protected void | setRootPaneCheckingEnabled(boolean enabled)Sets whether calls to add and
setLayout are forwarded to the contentPane .
rootPaneCheckingEnabled = enabled;
| public void | update(java.awt.Graphics g)Calls paint(g) . This method was overridden to
prevent an unnecessary call to clear the background.
paint(g);
|
|