initOwner role - JavaFX - java

I am wondering why there is a need to initOwner() while creating a Alert? The both codes - with and without initOwner() work. Could anyone tell me why I should use the initOwner() method?

In addition to the different look and behavior as pointed out by Jan's answer, the window ownership hierarchy effects window modality.
For a window modal dialog, if you set an owner, the dialog will block input for the owner stage and the user won't be able to close the owner stage without first closing the child.
If you have a window modal dialog with an owner, the user won't be able to focus on a field in the owner dialog until the child dialog is closed.
If the window modal dialog has no owner, the user can switch focus between a field in the owner and the child.
For non-modal windows with an owner, ownership also effects closure of windows. If the user closes the parent window, the child will also close.

Depending on the OS, the dialog might be displayed differently depending on whether or not it has an owner. If you look at the OS X example of a file open dialog in the picture below, the dialog is "slided out" of the owner windows top if specified. If there is no owner window specified, the dialog will be displayed as a regular "detached" dialog window.

Related

how to make pop up overlay with my screen using java

Im trying to make a java program that will popup/notify me to stop what I am currently doing and do something. more like a reminder. My question is how will I make a pop up in java.
I found this docs, but dont know how to implement for the parent component.
JOptionPane.showMessageDialog(??,"this is a modal dialog.");
The answer depends, do you have a parent component (like a JFrame or other component) or are you simply display the JOptionPane independently?
If you are simply displaying the JOptionPane independently, you can simply pass it null.
If you are displaying the JOptionPane as part of a large application, with windows and other components, you can simply pass it a reference of what ever component you want the JOptionPane to displayed relative to, such as the window or most accessible container/component
The parent argument (if supplied) simply allows the dialog to act as a modal (blocking) dialog for the window which contains the supplied component. This requires the user to have to dismiss the dialog before they can continue to interact with the parent window/component
Take a closer look at How to Make Dialogs for more details

How to create a common static focusable window in Java?

I am creating a Transliterating tool in Java. It's almost complete.
Here is the screenshot.
I am using JWindow for dropdown, which must be focusable for some reason.
As, user can write in one input only at one time. I've created this window static, so all Text components use the same instance instead of creating new one.
Problem occurs, when I work in more than one window. It works fine unless both window are showing on screen. But when owner window of this dropdown window is closed, dropdown window is not focusable anymore.
As Javadoc of JWindow(Window owner) constructor says:
Creates a window with the specified owner window. This window will not be focusable unless its owner is showing on the screen. If owner is null, the shared owner will be used and this window will not be focusable.
So, how can I create a static, focusable window, that is shared by all components in different windows.
Don't use a JWindow.
Instead you can use a non decorated JDialog. Then you won't have the focus problem.
Edit:
You can prevent the dialog from initially getting focus when you make it visible by using code like:
dialog.setWindowFocusableState(false);
dialog.setVisible(true);
dialog.setWindowFocusableState(true);

A dialog that must be finished before anything else takes place

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.
You can easily do this using a JDialog
JDialog dialog = new JDialog(Frame owner, "My modal dialog", **true**)
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);

SWT disable window causes lose focus

I'm making custom dialogs that I want to pop up and disable the main shell behind it so that it cannot be clicked while the dialog is active.
My initial plan was something like as follows:
shell.setEnabled(false);
doDialogStuff();
shell.setEnabled(true);
this worked but as I close the dialog, it loses focus of the shell that was open before the dialog. I managed to sort of fix it by adding
shell.setFocus();
after the last line but this is messy and causes the screen to flicker as the window loses and then gains focus in a split second, also, it sometimes doesn't regain focus and I can't understand why :/
Is there a better way to disable the background window without it losing focus.
Thanks in advance SO peeps
You should create a custom dialog based on this tutorial.
This way you just have to set the modality of the dialog to whatever you need exactly and the dialog will take care of the rest for you.
This should be helpful as well (Javadoc of Shell):
The modality of an instance may be specified using style bits. The modality style bits are used to determine whether input is blocked for other shells on the display. The PRIMARY_MODAL style allows an instance to block input to its parent. The APPLICATION_MODAL style allows an instance to block input to every other shell in the display. The SYSTEM_MODAL style allows an instance to block input to all shells, including shells belonging to different applications.
The proper thing to do is create the dialog as a modal window. When you create the dialog's shell you should do something like
dialogShell = new Shell(mainShell, PRIMARY_MODAL | DIALOG_TRIM);

modal parentless JDialog does not grab focus

We have an application which, as its first UI action, displays a modal JDialog without a parent frame.
public LoginDialog(Frame owner, Config config, Object... params) {
super((Frame)null, true);
It unfortunately has the annoying characteristic that when it appears, although it comes to the front, it does not grab the focus.
So the user, after launching the application by double-clicking on the start menu, has to use the mouse to select the "login" dialog and type in information.
Is there a way to make this JDialog grab the focus when it appears in the front?
I've tried calls to "requestFocus" before, after and via invokeLater "during" the call to setVisible(true) - but none of these seems to have any effect.
How do we make a modal dialog grab the focus?
UPDATE: The issue was the code used to try to present a background "wait window". This window was displayed "behind" the login dialog as a hack so that when the dialog disappeared the user would see the "Please wait" message. As it was the first window created by the application, it got the focus. I am not sure if there would have been a way to make the dialog gain the focus again inside the event dispatch thread with this hack - but I fixed it by un-hacking it and doing things properly.
First, it a little strange that modal dialog is parent-less. The point in modal dialog is that it is displayed on its parent and does not allow to access parent.
So, the first recommendation is to make it non-modal. I believe it will work.
BTW I have just tried to create such dialog and have not problems with focus. Try probably to simplify your code:
JDialog d = new JDialog();
d.setSize(200, 200);
d.setVisible(true);
This works for me and I believe will work for you. Now start changing this simple code towords your real application code. At some point it will stop working and you will see where the problem is.
If nothing helps try to use the trick I described in this article. Look for title "Portable window activation". I hope it will help.
See Dialog Focus for a potential fix using a RequestFocusListener. I have used it successfully for setting focus in JOptionPane dialogs.
1) you have to create JDialog contents and showing container wrapped inside invokeLater()
or best and safiest way is
2) you have to set for ModalityTypes or Modal for parent
3) only one from containers could be Modal in applications lifecycle

Categories

Resources