Stop execution of code during JFrame [duplicate] - java

How can I make my application pause when I open custom JDialog and after the dialog is closed to make continue again.

Simply use:
setModal(true);
I usually call it from within the constructor of the JDialog.
See the Javadocs on setModal(boolean).
http://java.sun.com/javase/6/docs/api/java/awt/Dialog.html#setModal(boolean)
That will cause execution to block on the current thread until the dialog box closes.
Alternatively, you can use:
setModalityType(Dialog.DEFAULT_MODALITY_TYPE);
It is equivalent to setModal(true) and technically the correct way to do it.

See the constructor of JDialog. You can set the modality of this dialog. Setting modal=true will pause your application. You can also use the method setModal.

Related

Difference between dispose and exit on close in java

I have a single frame created using Netbeans GUI builder when I view the frame properties one of the first options is default close operation the options listed are: DISPOSE_ON_CLOSE, HIDE_ON_CLOSE, DO_NOTHING_ON_CLOSE & EXIT_ON_CLOSE I understand the middle two but, whats the difference between DISPOSE_ON_CLOSE and EXIT_ON_CLOSE ? I have tried testing both but to me they do the same thing to me
EXIT_ON_CLOSE will terminate the program.
DISPOSE_ON_CLOSE will call dispose() on the frame, which will make it disappear and remove the resources it is using. You cannot bring it back, unlike hiding it.
See aslo JFrame.dispose() vs System.exit()
If you have a few JFrames open and you close the one that is set to EXIT_ON_CLOSEthen all of the frames will be closed.
The opposite applies to the one with the DISPOSE_ON_CLOSE i.e. only it will be closed
DISPOSE_ON_CLOSE - Disposes the window when it's closed. You cannot redisplay the window, although the object window is still available in the memory

CustomDialog, modality and dispose on close

I have a CustomDialog that extends JDialog.
In its constructor I have
setModalityType(ModalityType.APPLICATION_MODAL);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
They dont seem to work well together: i think the Modality stucks the defaultcloseoperation, and in the end i have to click twice the X in order to get the CustomDialog closed.
How should i act to obtain both
1- always on-top visualization (i use application_modal for this)
2- dispose on close
It works for me:
dialog.setModal(true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
I answer to provide people who have the same "click the X twice in order to close" behaviour a future reference.
My issue was related to a double setVisible(true), one in the constructor and one in a "initializer" function.
With only 1 setVisible(true), the issue is gone

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

Swing application - resource clean up should be done in windowClosing or windowClosed

In Swing application, I was wondering all the resource cleanup jobs, like : network resource shutdown, stopping thread, closing file handles...
Should it be done in windowClosed or windowClosing ?
Thanks.
Use windowClosed, because it's invoked when the window has been closed. windowClosing gets invoked whenever the "X" in the window corner is clicked, even if you had defined something like DO_NOTHING_ON_CLOSE as the closing action.
A common thing to do is to set the default closing action to DO_NOTHING_ON_CLOSE, add a WindowClosingListener to the window, and there show some kind of "Do you really want to quit?" message box. If yes, then dispose the window, if no, then do nothing.

Progress Dialog in Swing

How can I make a modal JDialog without buttons appear for the duration it takes a Runnable instance to complete and have that instance update a progress bar/message on that dialog?
Clearly spaghetti code might work, but I'm looking for a clean design if one exists.
You might want to look into ProgressMonitor. It automatically pops up a dialog with a progress bar if the operation is taking a long time; see How to Use Progress Monitors.
Look at the project described at the following URL:
http://digilander.libero.it/carlmila/compmon/main.html
It is a free Java library alternative to the Swing ProgressMonitor.
Use a monitor class whit a global instance and which your code keeps up to date (I'm starting, I'm working, I'm at xxx%, I'm done).
The monitor can then decide to show the dialog and keep it current. Later, you can simply replace the dialog with a progress bar in the status bar, for example. Use an interface for the monitor (methods: start(), update(), end(), error(), isAborted()) so you can replace it with something else, too.
You can extend the monitor to wait for 500ms after start() if there is an end() and not show the dialog in this case, etc.
This is how Eclipse does it and it works well.

Categories

Resources