In the constructor of a JFrame Mygraph i invoke some work (retrieving remote data for a graph and painting it) to be done via invokeLater in the constructor:
SwingUtilities.invokeLater(doWorkRunnable);
This is all fine, however i sometimes want to display an dialog box at the end of the process to indicate that the requested set is truncated etc:
JOptionPane aMsg = Meldingen.Instance().getConclusie();
JDialog aDialog = aMsg.createDialog(null, "Meldingen");
//aDialog.setModal(false);
aDialog.setVisible(true);
When i click 'OK' on this dialog it also closes the parent frame. When i run it in a non-modal fashion it's ok as no other frames are closed. I cannot find which mechanism in the Swing Event universe causes the unrelated JFrame Mygraph to close. Can anyone point me in the right direction here?
Related
I have a Swing application the main window of which inherits JFrame class. This app is on transition to JavaFX so there is a lot on JavaFX components including control panels.
I need to show native file chooser from non UI thread in modal way relatively to main JFrame.
This could be achieved by setting an owner of FileChooser class, but it requires javafx.stage.Window to be the owner.
Is there some hack to set JFrame as owner of javafx FileChooser or Stage?
The solution may be to disable selecting your JFrame somehow until the FileChooser closes.
Here's some pseudocode, since I don't know swing all that well:
remove focus from the JFrame somehow (perhaps using setFocusableWindowState(false)?)
open the FileChooser on the FXApplication thread
after that finishes (done using Thread.join() or however you want to manage your threads), restore focus to the JFrame
Actually it was like just:
JFrame frame = // get window
frame.setEnabled(false); // emulate window modality
File file = fileChooser.showOpenDialog(null);
if (file == null || !openFile(file)) { // return true if file was opened correctly
frame.setEnabled(true);
frame.requestFocus(); // window looses focus after enabling
}
So FileChooser will behave like modal window here. The downside is there's need to control all paths to enable window back so it will not stay disabled forever.
So I'm fairly new to Java Swing, and I stumbled upon a certain difficulty. I have a main Frame running (main part of the application that is visible throughout the app's execution) which has a button that once clicked, invokes a popup Frame (window) to collect user's information, and that frame has some Components. The problem being is that I don't really know the right approach to invoking the popup window and the main window freezing the execution and waiting until OK, or cancel button is clicked on the popup. Once this happens the main window code collects the returned values from the popup and resumes. I tried using synchronization to accomplish this, however the popup components don't even load, just the JFrame and JPanel (white background) and the popup freezes up on the wait() condition. I know that there is a way of doing it with JDialog and others, my main concern however, is to discover why the popup frame doesn't load the components and freezes up before the wait() condition. (when I get rid of wait() everything loads properly).
//in Main window Class:
frame.setEnabled(false);
Test test = getNewTest(); //should freeze on wait() in popup window
frame.setEnabled(true);
//in Popup Window Class
public Test getNewTest() {
addPanel.setVisible(true);
addFrame.setVisible(true);
synchronized(flag) {
try {
flag.wait();
} catch (InterruptedException e) {}
}
addPanel.setVisible(false);
addFrame.setVisible(false);
if(success)
return new Test(testName, date);
else return null;
}
//When OK or Cancel button clicked appropriate handler sets
//success value and invokes flag.notify();
Get all that synchronized and wait stuff out of your code. All that will do is freeze the Swing event thread, rendering your application useless.
You don't want to use a second JFrame, since an application typically has only one main window or JFrame, not multiple.
You want instead to use a modal dialog such as a JOptionPane (which can hold complex GUI's), or a modal JDialog (which also can hold a complex GUI). Be sure to associate the modal dialog with the parent JFrame. The Swing GUI library will then freeze the main window until the dialog has been dealt with, and the code from the main GUI will resume from the place the dialog was made visible after the dialog is no longer visible.
I have a JOptionPane which appears on top of it's parent JFrame window, but when the application is minimized and restored, only the JOptionPane will show and not the parent JFrame.
How to fix this bug?
JOptionPane is a modal dialog box.
It means first you need to handle/close this dialog box then you will be able to access your main window.
So when you minimize everything and then restore it, it firstly shows the JOptionPane when you will close it or what it is supposed to do ONLY then you will get the main window.
It's not a bug. It is just how modal things work.
You will not be able to even Minimize the main window from icon when JOptionPane is up. You can minimize everything like with Window + D key or Window + M key in Windows PCs.
I am running Windows. When you run an application on Windows, you get a button task bar where you can click it to maximize and minimize it. Is it possible to create a JFrame without this or some other component that has the functionality of a JFrame but without adding it to the task bar.
Use a JDialog instead of a JFrame. On a JDialog, you can set the 'modal' property, which means no 'upper bar' or anything is displayed.
Do make sure the JDialog has no parent frame or anything though: a modal JDialog will block the GUI of any parent GUI component. But if you just use it as your main component there is no problem :)
I want to create in Java Swing a JDialog which, when it's open, its parent window cannot be accessed (just like when you open the file explorer dialog in Microsoft Word). Is there any method in the JDialog class that provides this behaviour?
use JDialog.setModal(true) before setting dialog visible
JDialog yourdialog = ...
yourdialog.setModal(true);
...
yourdialog.setVisible(true);
You have two options:
Use the static methods in JOptionPane. These will create modal dialogs by default:
Window parentWindow = SwingUtilities.getWindowAncestor(parentPanel);
JOptionPane.showMessageDialog(parentWindow, "Hello, World); // Create modal dialog aligned with parent window.
Create a JDialog explicitly:
Window parentWindow = SwingUtilities.getWindowAncestor(parentPanel);
JDialog dlg = new JDialog(parentWindow, ModalityType.APPLICATION_MODAL);
The first option is far simpler and I tend to prefer it particularly with modal dialogs.
Adamski and Jan both have the correct answers already, but I wanted to just make sure that the concept of a modal window is explained.
The OP asked about a dialog that blocks access to the parent. This is called a modal dialog (or a modal window). Wikipedia gives this definition:
In user interface design, a modal window is a child window that requires users to interact with it before they can return to operating the parent application
So, "modal" means that it will block parent windows (users cannot interact with any window besides the modal window), while "non-modal", or "modeless" means that the child and parent windows will be accessible at the same time.
This is a concept that exists in GUI frameworks in general, not just the Swing framework. In any GUI framework that you use, you can probably find this kind of functionality by looking for a modal property.
how about to lock JDialog inside his JFrame Parent ?
it's true that using JDialog.setModal capable to making jdialog just like dialog on other application. stopped all frame bofore jDialog closed