FileDocCategorySizeDatePackage
Dialog.javaAPI DocJava SE 5 API28117Fri Aug 26 14:56:44 BST 2005java.awt

Dialog

public class Dialog extends Window
A Dialog is a top-level window with a title and a border that is typically used to take some form of input from the user. The size of the dialog includes any area designated for the border. The dimensions of the border area can be obtained using the getInsets method, however, since these dimensions are platform-dependent, a valid insets value cannot be obtained until the dialog is made displayable by either calling pack or show. Since the border area is included in the overall size of the dialog, the border effectively obscures a portion of the dialog, constraining the area available for rendering and/or displaying subcomponents to the rectangle which has an upper-left corner location of (insets.left, insets.top), and has a size of width - (insets.left + insets.right) by height - (insets.top + insets.bottom).

The default layout for a dialog is BorderLayout.

A dialog may have its native decorations (i.e. Frame & Titlebar) turned off with setUndecorated. This can only be done while the dialog is not {@link Component#isDisplayable() displayable}.

A dialog must have either a frame or another dialog defined as its owner when it's constructed. When the owner window of a visible dialog is minimized, the dialog will automatically be hidden from the user. When the owner window is subsequently restored, the dialog is made visible to the user again.

In a multi-screen environment, you can create a Dialog on a different screen device than its owner. See {@link java.awt.Frame} for more information.

A dialog can be either modeless (the default) or modal. A modal dialog is one which blocks input to all other toplevel windows in the application, except for any windows created with the dialog as their owner.

Dialogs are capable of generating the following WindowEvents: WindowOpened, WindowClosing, WindowClosed, WindowActivated, WindowDeactivated, WindowGainedFocus, WindowLostFocus.

see
WindowEvent
see
Window#addWindowListener
version
1.99, 05/18/04
author
Sami Shaio
author
Arthur van Hoff
since
JDK1.0

Fields Summary
boolean
resizable
A dialog's resizable property. Will be true if the Dialog is to be resizable, otherwise it will be false.
boolean
undecorated
This field indicates whether the dialog is undecorated. This property can only be changed while the dialog is not displayable. undecorated will be true if the dialog is undecorated, otherwise it will be false.
boolean
modal
Will be true if the Dialog is modal, otherwise the dialog will be modeless. A modal Dialog grabs all the input to the owner frame from the user.
String
title
Specifies the title of the Dialog. This field can be null.
private transient boolean
keepBlocking
private static final String
base
private static int
nameCounter
private static final long
serialVersionUID
private transient AppContext
showAppContext
Stores the app context on which event dispatch thread the dialog is being shown. Initialized in show(), used in hideAndDisposeHandler()
Constructors Summary
public Dialog(Frame owner)
Constructs an initially invisible, non-modal Dialog with an empty title and the specified owner frame.

param
owner the owner of the dialog
exception
IllegalArgumentException if the owner's GraphicsConfiguration is not from a screen device
exception
java.lang.IllegalArgumentException if owner is null; this exception is always thrown when GraphicsEnvironment.isHeadless returns true
see
java.awt.GraphicsEnvironment#isHeadless
see
Component#setSize
see
Component#setVisible


                                                                             
       
	this(owner, "", false);
    
public Dialog(Frame owner, boolean modal)
Constructs an initially invisible Dialog with an empty title, the specified owner frame and modality.

param
owner the owner of the dialog
param
modal if true, dialog blocks input to other app windows when shown
exception
IllegalArgumentException if the owner's GraphicsConfiguration is not from a screen device
exception
java.lang.IllegalArgumentException if owner is null; this exception is always thrown when GraphicsEnvironment.isHeadless returns true
see
java.awt.GraphicsEnvironment#isHeadless

	this(owner, "", modal);
    
public Dialog(Frame owner, String title)
Constructs an initially invisible, non-modal Dialog with the specified owner frame and title.

param
owner the owner of the dialog
param
title the title of the dialog; a null value will be accepted without causing a NullPointerException to be thrown
exception
IllegalArgumentException if the owner's GraphicsConfiguration is not from a screen device
exception
java.lang.IllegalArgumentException if owner is null; this exception is always thrown when GraphicsEnvironment.isHeadless returns true
see
java.awt.GraphicsEnvironment#isHeadless
see
Component#setSize
see
Component#setVisible

	this(owner, title, false);
    
public Dialog(Frame owner, String title, boolean modal)
Constructs an initially invisible Dialog with the specified owner frame, title, and modality.

param
owner the owner of the dialog
param
title the title of the dialog; a null value will be accepted without causing a NullPointerException to be thrown
param
modal if true, dialog blocks input to other app windows when shown
exception
IllegalArgumentException if the owner's GraphicsConfiguration is not from a screen device
exception
java.lang.IllegalArgumentException if owner is null. This exception is always thrown when GraphicsEnvironment.isHeadless returns true
see
java.awt.GraphicsEnvironment#isHeadless
see
Component#setSize
see
Component#setVisible

	super(owner);

	this.title = title;
	this.modal = modal;
        SunToolkit.checkAndSetPolicy(this, false);
    
public Dialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc)
Constructs an initially invisible Dialog with the specified owner frame, title, modality, and GraphicsConfiguration.

param
owner the owner of the dialog
param
title the title of the dialog. A null value will be accepted without causing a NullPointerException to be thrown.
param
modal if true, dialog blocks input to other app windows when shown
param
gc the GraphicsConfiguration of the target screen device. If gc is null, the same GraphicsConfiguration as the owning Frame is used.
exception
java.lang.IllegalArgumentException if owner is null. This exception is always thrown when GraphicsEnvironment.isHeadless() returns true
see
java.awt.GraphicsEnvironment#isHeadless
see
Component#setSize
see
Component#setVisible
since
1.4

        super(owner, gc);

        this.title = title;
        this.modal = modal;
        SunToolkit.checkAndSetPolicy(this, false);
    
public Dialog(Dialog owner)
Constructs an initially invisible, non-modal Dialog with an empty title and the specified owner dialog.

param
owner the owner of the dialog
exception
java.lang.IllegalArgumentException if owner is null. This exception is always thrown when GraphicsEnvironment.isHeadless() returns true
see
java.awt.GraphicsEnvironment#isHeadless
since
1.2

	this(owner, "", false);
    
public Dialog(Dialog owner, String title)
Constructs an initially invisible, non-modal Dialog with the specified owner dialog and title.

param
owner the owner of the dialog
param
title the title of the dialog. A null value will be accepted without causing a NullPointerException to be thrown.
exception
java.lang.IllegalArgumentException if owner is null. This exception is always thrown when GraphicsEnvironment.isHeadless() returns true
see
java.awt.GraphicsEnvironment#isHeadless
since
1.2

	this(owner, title, false);
    
public Dialog(Dialog owner, String title, boolean modal)
Constructs an initially invisible Dialog with the specified owner dialog, title, and modality.

param
owner the owner of the dialog
exception
IllegalArgumentException if the owner's GraphicsConfiguration is not from a screen device
param
title the title of the dialog; a null value will be accepted without causing a NullPointerException to be thrown
param
modal if true, dialog blocks input to other app windows when shown
exception
java.lang.IllegalArgumentException if owner is null; this exception is always thrown when GraphicsEnvironment.isHeadless returns true
see
java.awt.GraphicsEnvironment#isHeadless
since
1.2

	super(owner);

	this.title = title;
	this.modal = modal;
        SunToolkit.checkAndSetPolicy(this, false);
    
public Dialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc)
Constructs an initially invisible Dialog with the specified owner dialog, title, modality, and GraphicsConfiguration.

param
owner the owner of the dialog
param
title the title of the dialog; a null value will be accepted without causing a NullPointerException to be thrown
param
modal if true, dialog blocks input to other app windows when shown
param
gc the GraphicsConfiguration of the target screen device; if gc is null, the same GraphicsConfiguration as the owning Dialog is used
exception
IllegalArgumentException if the owner's GraphicsConfiguration is not from a screen device
exception
java.lang.IllegalArgumentException if owner is null; this exception is always thrown when GraphicsEnvironment.isHeadless returns true
see
java.awt.GraphicsEnvironment#isHeadless
see
Component#setSize
see
Component#setVisible
since
1.4

	super(owner, gc);
	 
	this.title = title;
	this.modal = modal;
        SunToolkit.checkAndSetPolicy(this, false);
    
Methods Summary
public voidaddNotify()
Makes this Dialog displayable by connecting it to a native screen resource. Making a dialog displayable will cause any of its children to be made displayable. This method is called internally by the toolkit and should not be called directly by programs.

see
Component#isDisplayable
see
#removeNotify

	synchronized (getTreeLock()) {
	    if (parent != null && parent.getPeer() == null) {
                parent.addNotify();
	    }

	    if (peer == null) {
	        peer = getToolkit().createDialog(this);
	    }
	    super.addNotify();
	}
    
private booleanconditionalShow()

return
true if we actually showed, false if we just called toFront()

        boolean retval;

        synchronized (getTreeLock()) {
            if (peer == null) {
                addNotify();
            }
            validate();
            if (visible) {
                toFront();
                retval = false;
            } else {
                visible = retval = true;
                peer.show(); // now guaranteed never to block
		createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
				      this, parent,
				      HierarchyEvent.SHOWING_CHANGED,
                                      Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
            }
	    if (retval && (componentListener != null ||
			   (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
                           Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK))) {
	        ComponentEvent e =
		    new ComponentEvent(this, ComponentEvent.COMPONENT_SHOWN);
		Toolkit.getEventQueue().postEvent(e);
	    }
        }

        if (retval && (state & OPENED) == 0) {
            postWindowEvent(WindowEvent.WINDOW_OPENED);
            state |= OPENED;
        }

        return retval;
    
java.lang.StringconstructComponentName()
Construct a name for this component. Called by getName() when the name is null.

        synchronized (getClass()) {
	    return base + nameCounter++;
	}
    
voiddoDispose()
Disposes the Dialog and then causes show() to return if it is currently blocked.

        super.doDispose();
        hideAndDisposeHandler();
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this Dialog. For dialogs, the AccessibleContext takes the form of an AccessibleAWTDialog. A new AccessibleAWTDialog instance is created if necessary.

return
an AccessibleAWTDialog that serves as the AccessibleContext of this Dialog

        if (accessibleContext == null) {
            accessibleContext = new AccessibleAWTDialog();
        }
        return accessibleContext;
    
public java.lang.StringgetTitle()
Gets the title of the dialog. The title is displayed in the dialog's border.

return
the title of this dialog window. The title may be null.
see
java.awt.Dialog#setTitle

	return title;
    
public voidhide()

deprecated
As of JDK version 1.5, replaced by {@link Component#setVisible(boolean) Component.setVisible(boolean)}.

        super.hide();
        hideAndDisposeHandler();
    
private voidhideAndDisposeHandler()

        if (keepBlocking) {
            synchronized (getTreeLock()) {
                keepBlocking = false;

                if (showAppContext != null) {
                    // Wake up event dispatch thread on which the dialog was 
                    // initially shown
                    SunToolkit.postEvent(showAppContext, 
                                         new PeerEvent(this, 
                                                       new WakingRunnable(), 
                                                       PeerEvent.PRIORITY_EVENT));
                    showAppContext = null;
                }
                EventQueue.invokeLater(new WakingRunnable());
                getTreeLock().notifyAll();
            }
        }
    
private static native voidinitIDs()
Initialize JNI field and method IDs

voidinterruptBlocking()

        if (modal) {
            disposeImpl();
        } else if (windowClosingException != null) {
            windowClosingException.fillInStackTrace();
            windowClosingException.printStackTrace();
            windowClosingException = null;
        }
    
public booleanisModal()
Indicates whether the dialog is modal. When a modal Dialog is made visible, user input will be blocked to the other windows in the application, except for any windows created with this dialog as their owner.

return
true if this dialog window is modal; false otherwise.
see
java.awt.Dialog#setModal

	return modal;
    
public booleanisResizable()
Indicates whether this dialog is resizable by the user.

return
true if the user can resize the dialog; false otherwise.
see
java.awt.Dialog#setResizable

	return resizable;
    
public booleanisUndecorated()
Indicates whether this dialog is undecorated. By default, all dialogs are initially decorated.

return
true if dialog is undecorated; false otherwise.
see
java.awt.Dialog#setUndecorated
since
1.4

	return undecorated;
    
protected java.lang.StringparamString()
Returns a string representing the state of this dialog. 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.

return
the parameter string of this dialog window.

	String str = super.paramString() + (modal ? ",modal" : ",modeless");
	if (title != null) {
	    str += ",title=" + title;
	}
	return str;
    
public voidsetModal(boolean b)
Specifies whether this dialog should be modal.

see
java.awt.Dialog#isModal
since
JDK1.1

	this.modal = b;
    
public voidsetResizable(boolean resizable)
Sets whether this dialog is resizable by the user.

param
resizable true if the user can resize this dialog; false otherwise.
see
java.awt.Dialog#isResizable

        boolean testvalid = false;

        synchronized (this) {
            this.resizable = resizable;
            DialogPeer peer = (DialogPeer)this.peer;
            if (peer != null) {
                peer.setResizable(resizable);
                testvalid = true;
            }
        }

        // On some platforms, changing the resizable state affects
        // the insets of the Dialog. If we could, we'd call invalidate()
        // from the peer, but we need to guarantee that we're not holding
        // the Dialog lock when we call invalidate().
        if (testvalid && valid) {
            invalidate();
        }
    
public voidsetTitle(java.lang.String title)
Sets the title of the Dialog.

param
title the title displayed in the dialog's border; a null value results in an empty title
see
#getTitle

        String oldTitle = this.title;

        synchronized(this) {
            this.title = title;
            DialogPeer peer = (DialogPeer)this.peer;
            if (peer != null) {
                peer.setTitle(title);
            }
        }
        firePropertyChange("title", oldTitle, title);
    
public voidsetUndecorated(boolean undecorated)
Disables or enables decorations for this dialog. This method can only be called while the dialog is not displayable.

param
undecorated true if no dialog decorations are to be enabled; false if dialog decorations are to be enabled.
throws
IllegalComponentStateException if the dialog is displayable.
see
#isUndecorated
see
Component#isDisplayable
since
1.4

        /* Make sure we don't run in the middle of peer creation.*/
        synchronized (getTreeLock()) {
            if (isDisplayable()) {
                throw new IllegalComponentStateException("The dialog is displayable.");
            }
            this.undecorated = undecorated;
        }
    
public voidshow()

deprecated
As of JDK version 1.5, replaced by {@link Component#setVisible(boolean) Component.setVisible(boolean)}.

        beforeFirstShow = false;
        if (!isModal()) {
            conditionalShow();
        } else {
            // Set this variable before calling conditionalShow(). That
            // way, if the Dialog is hidden right after being shown, we
            // won't mistakenly block this thread.
            keepBlocking = true;

            // Store the app context on which this dialog is being shown.
            // Event dispatch thread of this app context will be sleeping until
            // we wake it by any event from hideAndDisposeHandler().
            showAppContext = AppContext.getAppContext();

            // keep the KeyEvents from being dispatched
            // until the focus has been transfered
            long time = Toolkit.getEventQueue().getMostRecentEventTimeEx();
            Component predictedFocusOwner = getMostRecentFocusOwner();
            KeyboardFocusManager.getCurrentKeyboardFocusManager().
                enqueueKeyEvents(time, predictedFocusOwner); 

            try {
                if (conditionalShow()) {
                    // We have two mechanisms for blocking: 1. If we're on the
                    // EventDispatchThread, start a new event pump. 2. If we're
                    // on any other thread, call wait() on the treelock.

                    Runnable pumpEventsForHierarchy = new Runnable() {
                            public void run() {
                                EventDispatchThread dispatchThread =
                                    (EventDispatchThread)Thread.currentThread();
                                dispatchThread.pumpEventsForHierarchy(new Conditional() {
                                        public boolean evaluate() {
                                            return keepBlocking && windowClosingException == null;
                                        }
                                    }, Dialog.this);
                            }
                        };

                    if (EventQueue.isDispatchThread()) {
                        /*
                         * dispose SequencedEvent we are dispatching on current
                         * AppContext, to prevent us from hang.
                     *
                     * BugId 4531693 (son@sparc.spb.su)
                     */
                        SequencedEvent currentSequencedEvent = KeyboardFocusManager.
                            getCurrentKeyboardFocusManager().getCurrentSequencedEvent();
                        if (currentSequencedEvent != null) {
                            currentSequencedEvent.dispose();
                        }

                        pumpEventsForHierarchy.run();
                    } else {
                        synchronized (getTreeLock()) {
                            Toolkit.getEventQueue().
                                postEvent(new PeerEvent(this,
                                                        pumpEventsForHierarchy,
                                                        PeerEvent.PRIORITY_EVENT));
                            while (keepBlocking && windowClosingException == null) {
                                try {
                                    getTreeLock().wait();
                                } catch (InterruptedException e) {
                                    break;
                                }
                            }
                        }
                    }
                    if (windowClosingException != null) {
                        windowClosingException.fillInStackTrace();
                        throw windowClosingException;
                    }
                }
            } finally {
                // Restore normal key event dispatching
                KeyboardFocusManager.getCurrentKeyboardFocusManager().
                    dequeueKeyEvents(time, predictedFocusOwner);
            }
        }