Reputation: 7148
Is there a simple way in Java to create a dialog that will not let you change focus from it until it has been closed? Such as the windows dialogs that gray out the entire screen and only let you interact with it until you satisfy it.
Upvotes: 2
Views: 103
Reputation: 2534
You can make your dialog modal which blocks user input. From Oracle modality tutorial:
Modal dialog box — A dialog box that blocks input to some other top-level windows in the application, except for windows created with the dialog box as their owner. The modal dialog box captures the window focus until it is closed, usually in response to a button press.
There are four types of Modality (again from tutorial):
- Modeless type — A modeless dialog box does not block any other window while it is visible.
- Document-modal type — A document-modal dialog box blocks all windows from the same document, except windows from its child hierarchy. In this context, a document is a hierarchy of windows that share a common ancestor, called the document root, which is the closest ancestor window without an owner.
- Application-modal type — An application-modal dialog box blocks all windows from the same application, except windows from its child
hierarchy. If several applets are launched in a browser environment,
the browser is allowed to treat them either as separate applications
or as a single application. This behavior is
implementation-dependent.- Toolkit-modal type — A toolkit-modal dialog box blocks all windows that run in the same toolkit, except windows from its child
hierarchy. If several applets are launched, all of them run with the
same toolkit. Hence, a toolkit-modal dialog box shown from an applet
may affect other applets and all windows of the browser instance that embeds the Java runtime environment for this toolkit.
You can use JDialog to create your dialog. Just use one of the constructors that takes the modal flag and set modal to true. If you want you can specify one of the types above, but by default it will be APPLICATION_MODAL
.
This is a simple constructor you can use:
public JDialog(Dialog owner, String title, boolean modal)
So you just add
JDialog dialog = new JDialog(owner, "My test modal dialog", true);
Upvotes: 2
Reputation: 613
You can easily do this using a JDialog
JDialog dialog = new JDialog(Frame owner, "My modal dialog", **true**)
Upvotes: 6