Java Swing. Windows in front launched from JDialog - java

The thing I want to implement is the next UI hierarchy in my Swing application.
Main Window (JFrame)
Modal dialog (JDialog) in front of this window. It's opened by clicking on the button in main window.
The set of windows in front of modal dialog (2). They should be independent from each other and non-blocking for modal jdialog (2).
How can I code this?

Specify the owner when you create the JDialogs. Read the JDialog API for the proper constructor to use. I would guse the owner of the dialog in point 3 would be the dialog in point 2.

Related

Access a modeless JFrame while a modal JDialog is visible?

Is it possible to access a modeless JFrame while a modal JDialog is visible?
I have a JFrame which shows my log lines. When i display a modal JDialog, for example to login, the user can't click the JFrame.
The JFrame isn't a parent/owner of the JDialog.
The JFrame is the first JFrame being created.
Note that when i do something in the JDialog that causes a log line to be added to the log i can see it appear in the JFrame.
Is this how it supposed to work or is it possible to let the user click the JFrame while the modal JDialog is visible?
If the dialog is application modal, e.g., ModalityType.APPLICATION_MODAL, then it will block user interaction with all other top-level windows in the application while the dialog is visible. This does not prevent the application itself from changing the other top level windows states, including any text that they display.
It seems that using ModalityType.DOCUMENT_MODAL is the answer.
Give the JDialog ModalityType.DOCUMENT_MODAL (setModalityType) and make sure setModal is false. The JDialog should also have owner/parent Window.
The JFrame that should always be accessible should have no relation to the JDialog, so don't use it as owner/parent for the JDialog.
Now the JDialog blocks the owner/parent Window but the JFrame is still accessible while the modal JDialog is displayed.
Thanks go out to #Hovercraft Full Of Eels for pointing me to ModalityType.

I need to make a JFrame or a JDialog that even if you change window it stays focused so that the keylistener will listen

I need to have a frame or dialog that stays focused even if I change window.
I am building an AutoClicker and it needs while it's clicking in another window to listen to my keyListener so that the user will be able to start/stop it.
d.setModalityType(ModalityType.APPLICATION_MODAL);// d is my JDialog
d.addKeyListener(this);
d.setAlwaysOnTop(true);
d.toFront();
d.requestFocus();
d.setFocusableWindowState(true);
d.setFocusable(true);

Focussing in Java swing

I'm developing an application using java swing. When I click a button, I want another window to be opened. This works fine. But it is possible to alt+tab to the original window and then interact with it even after the new window is open. Is there any way not to let the user focus the original window after the new window appears? By window I'm referring to Jframe/Jdialog.
Assuming the instance of your main JFrame window is called mainWindow:, the following code will prevent switching the focus.
// the second parameter makes the dialog modal and will prevent
// switching the focus to the mainWindow
JDialog dialog = new JDialog(mainWindow, true);
...
dialog.setVisible(true);
Documentation on JDialog: http://docs.oracle.com/javase/6/docs/api/javax/swing/JDialog.html
You may try to use a JDialog instead of JFrame and pass the instance of JFrame to JDialog constructor
You may also try to check
frame.setAlwaysOnTop(true);
or may be like this:-
frame.toFront();
frame.setState(Frame.NORMAL);

dialog inside of a JInternalFrame

I have a JInternalFrame and I need to get some Information from user (a JFileChooser & two JTextfield).
Using another JInternalFrame, application continues running and doesn't wait for input.
How can I open a JDialog as a internal dialog?
Dialogs that are internal frames should be implemented using JInternalFrame (or JOptionPane) not JDialog

JDialog box not gaining focus

I have a modeless dialog box being generated which prompts users to open a new window. The box can be opened in two ways, either directly from the file menu for the frame I'm writing or indirectly via the framework my panel is plugging into.
When I make the call directly via the file menu the dialog box comes up with focus exactly as I want. But when I have the framework indirectly open the dialog box it does not have focus as it should.
There doesn't seem to be a difference between the two methods of opening the dialog, in both cases a load function is called and it's not until 5 method calls later the dialog box is opened. In both cases the frame which generates the dialog box is realized at the time the box is generated. I've tried calling requestFocus after making the dialog box visible but it doesn't seem to do anything.
Any suggestion why the dialog box wouldn't have focus, or how I can give it focus as a separate window from the window that usually has focus?
in some cases is hard to set Focus to the expected top-level container as are demonstrated here , but for excelent workaround would be better look at camickr's Dialog Focus
When you create the dialog, try setting the main GUI as parent of the dialog.
In the first case, when you click from menu, it automatically sets the main GUI as the parent of the dialog, but it doesnt in the second case.
So make sure when you create the dialog, you are setting the main GUI/ window as parent always.
It should help most times.

Categories