Java Swing: JOptionPane appears by itself without parent window when restored? - java

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.

Related

JAVA - How do I make the user be able to utilize only the frame on top?

When two JFrames are enabled, I want the user to be able to use only one in the top (think of that like an error pop up on your screen, when you can't press anything but the popup itself). I am aware of the class JInternalFrame and I chose not to use it for my program. Thanks in advance :)
Use JDialog, you can set your main frame as the JDialog's parent frame, so that whenever your main frame and JDialog will display, you will be able to click only the JDialog, not your main frame.
You want a modal behaviour, then. I think you can try using JDialog instead of JFrame, something like:
JDialog dialog = new JDialog(parentFrame, title, true); //parameters: owner, title and modal
dialog.getContentPane().add(somePanel);
dialog.pack();
dialog.setVisible(true);
You can read more about it here: http://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html

JDialog over Desktop area

I am trying to use a JDialog at the right of the screen, everything is almost perfect, but, if someone press the button on ther right end of the TaskBar, click on "Show Desktop area" my JDialog disappears I have to use ALT + TAB to get it back in in front. I can't set it AlwaysOnTop because I use other 3rd party programs that are fullscreen.
I tried:
jdigCentral.setAutoRequestFocus(true);
jdigCentral.setAlwaysOnTop(true);
and others, but without success
How can I have my JDialog to stay over just the Desktop Area?
Is jdigCentral your dialog box? If so then try this:
jdigCentral.setModal(true);
That will keep the dialog box on top of the parent JFrame. But I'm confused about the use of the word Desktop Area. Do you mean you want to keep the dialog box on top of the parent frame, or do you want it to always show on top of all other programs running on your desktop?

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

Java: How can I disable clicking on a panel while showing dialog?

I want to disable clicking on the background panel or frame while showing a dialogue. And I want the dialogue to appear on top of this panel or frame constantly until it is closed.
How can I do this?
Make Dialog/JDialog modal by calling dialog.setModal(true);. This will solve both issues of clicking background panel and remaining on top of panel.
It seems like this method is obsolete so better you should use dialog.setModalityType(Dialog.ModalityType type)
You can use JOptionPane for the message dialog.

Question about JFrames

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 :)

Categories

Resources