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
Related
I'd like to call a GUI from a swing component JButton. There will already be a GUI running at this time, but I'd like to execute another one through the use of a JButton.
Any help would be greatly appreciated.
P.S: I've tried the .show() method, and it takes over from the current GUI. This is not what I would like it to do.
I've tried the .show() method
Don't use the show() method, it is deprecated. To make a window visible you use setVisible(true).
it takes over from the current GUI. This is not what I would like it to do.
Sounds like you want to prevent the child window from gaining focus. If so then the basic code you use is:
JDialog dialog = new JDialog();
dialog.add(...);
dialog.pack();
dialog.setFocusableWindowState( false );
dialog.setVisible( true );
You should use a JDialog for a child window (not a JFrame) since an application should only contain a single JFrame.
All You need is a JDialog which is here for short user interactions.
The easier way out is JOptionPane which one overload lets you show a component.
I've got a JDialog that's created and set to visible whenever the button is clicked.
My problem is that the button keeps the focus and doesn't give it to the JDialog.
Is this a normal behaviour or there's something wrong going on ?
JDialogs aren't modal (are "modeless") by default:
Creates a modeless dialog without a title and without a specified Frame owner.
Try constructing it as:
new JDialog(owner, title, ModalityType.APPLICATION_MODAL);
(Or the equivalent super() call if you're subclassing JDialog. Or whichever modality type you want.)
Try using dialog.requestFocus() if dialog is the newly created JDialog.
See requestFocus() or requestFocusInWindow() for more information.
Good day!
I'm developping a small Java Application with NetBeans IDE that extends JFrame. I implemented several ways to close the App, for example pressing Ctrl-Q and pressing the X of the JFrame.
But before the actual closing I'd like the program to execute some additional code for me, to save some objects the application needs to reuse the next time it starts up.
What is the best way to change the entire programs closing behavior?
I'm thinking about overriding/replacing the defaultCloseOperation, something like this:
setDefaultCloseOperation( myOwnCloseOperation );
Thanks for any help!
setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE )
Add a WindowListener or WindowAdapter - overriding either the windowClosing() or windowClosed() methods (I forget which) to catch the closing event.
Call the save functionality from the window method.
Call setVisible(false) or dispose() (as discussed in the comments) at the end.
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
In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true). But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).
Is there any special method(s) to do this? How can I do it?
Consider using CardLayout instead of hunting for how many JFrames there are. Then..
only one JFrame would be needed
any of Next/Back Actions will be only switching between cards
There are lots of examples in this forum - e.g. as shown here.
That is an odd & quirky GUI. I suggest instead to run a JFrame for the main GUI, and when the user wants to search, pop a JOptionPane (or modal JDialog) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.
If you want to destroy a JFrame releasing all associated resources you shold call dispose() method on it.
You may place your JFrames on a list data structure and keep a reference to current position according to the window you are displaying. In that way it will be easy to move to next and previous. But note that each frame added to the list will use memory and will have its state as you placed it in to the list.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
create the instance of your main window in next() window.. and use same method which you chosed befoe to hide your main window, for example if your main window is named as gui then what we have to do is.
gui obj = new gui();
and if you click on back button now than do these also
this.setVisibility(false);
obj.setVisibility(true);
that's all you need.
good luck.