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.
Related
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);
I want one JFrame to have a method like this:
private void someEvent(java.awt.event.ActionEvent evt){
//initialize another JFrame
//set the new JFrame to be visible
//set this JFrame to be disabled
}
Which is possible, but I also want the main JFrame to perform something when the newly created JFrame is disposed. I don't want to pass in the main JFrame to the new JFrame however. Is this possible?
Instead, use CardLayout to switch between the two desired content panes. There's an example here.
Don't have one JFrame create and display another JFrame. Instead the second window should be a JDialog, either modal if you want the first window frozen until the second has been dealt with or non-modal if otherwise. If modal then the first window's code will resume once the JDialog has been disposed of, and the code flow will start right after the setVisible(true) call on the dialog. If non-Modal, you'll likely want to add a WindowListener to the dialog.
For example, please check out my code here, here, and here.
I am stuck at a point where I need a Timer which displays some time remaining when i click a button on the Jframe. But the timer frame should be a different frame and should be unclosable. Is there a way to do it?
different frame and should be unclosable.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
The same can be done for a JDialog.
In both cases the close button will appear on the window, it just won't do anything.
Using a dialog is better than a frame because in general applications should only have a single JFrame and the child dialogs for other windows.
How about creating an undecorated non-modal JDialog?
I have two JFrames. Both are visible at same time.
One JFrame takes the whole screen..its just plain white. (it is acting as a background). And other JFrame is a small box with buttons/texts and other swing components.
The problem I get is when I click the big JFrame area, the JFrame box minimizes. So how do I specify java to make sure the JFrame box is always on top of the JFrame background?
Use a JInternalFrame
Make the JFrame box a JPanel box.
Your application should only have one JFrame.
JFrame is a TopLevel Component and therefore usually you don't put a JFrame into another. If you want to put your smaller jframe into your bigger I would subclass either JDialog or a JPanel.
In general, an application should only have a single JFrame. Other windows should be dialogs.
The problem I get is when I click the big JFrame area, the JFrame box minimizes.
When you use the dialog make sure you specify the frame as the owner of the dialog:
JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame);
If the main frame is ever minimized, the dialog will also be minimized. When the frame is restored the dialog will always display on top of the frame.
use Jdialog with setModal(false) for your small window ,
probably you want something similar to gimp
look at gimp toolbox , only X at title , means its a Dialog.
hope that's help
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.