Unable to focus JOptionPane on foreground with default frame - java

I have a piece of code in my program where in I need to display an error message. Code:
String ErrorMsg=" Error to be Diplayed ";
JOptionPane.showMessageDialog(null, ErrorMsg, "Failure", JOptionPane.ERROR_MESSAGE);
Note: Default frame is used.
The message is successfully displayed, but before acknowledging by pressing "OK" button if I try any other successful flow the message box control is lost and message box won't be on foreground blocking even the successive flows.
I want the Message Box to be on the foreground always until the user presses "OK" button, rather losing focus and getting hidden. How to do that?

If you want JOptionPane to behave as it would in a full-fledged GUI, then first create a full-fledged Swing GUI. Forget using "default" frames or whatever you're using (the console perhaps). You are desiring GUI behavior, and so to get this you must create a GUI by displaying your application in a JFrame and have the JFrame launch the JOptionPane.

Pass reference to the parent frame instead of the null (first param).

I want the Message Box to be on the foreground always until the user presses "OK" button, rather losing focus and getting hidden.
Use a JFrame and setAlwaysOnTop(true). You will need to display your own message and button.
A JOptionPane uses a JDialog behind the scenes. A JDialog does not support this property.
Edit:
To get the icon used by the option pane you can use:
Icon icon = UIManager.getIcon("OptionPane.errorIcon");
For a list of the other icons see: UIManager Defaults.

The below code is enough to make a JoptionPane message with default frame to be set on top .
JDialog dialog = new JOptionPane("ErrorMsg",JOptionPane.ERROR_MESSAGE,JOptionPane.DEFAULT_OPTION).createDialog(" Failure");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
dialog.dispose();

Related

How to make main window inactive after error JFrame pops

So I have a main frame that sometimes produces an error frame by pressing on one of the JButtons. I want to make it so when the error frame pops, main frame is inactive and you are unable to press any buttons. Is there an easy way for that?
What you are referring to is modality1, for which case a modal JDialog is more appropriate than a JFrame. See more at How to Use Dialogs
Also, if this is a simple message popup, a simple JOptionPane would be more suitable. You can also see how to use JOptionPanes in the link above.
Here's a simple JOPtionPane example. Though I don't use a frame in the example, if you do have a frame, the JOptionPane will block any user interaction.
import javax.swing.JOptionPane;
public class SimpleErrorMessage {
public static void main(String[] args) {
JOptionPane.showMessageDialog(
null, "Error!", "Error Message", JOptionPane.ERROR_MESSAGE);
}
}
See JOptionPane API for more dialog options.
1. A dialog box can be either modeless or modal. A modal dialog box is one that blocks input to some other top-level windows in the application, except for any windows created with the dialog box as their owner. The modal dialog box captures the window focus until it is closed, usually in response to a button press
You can use a glass pane to make main frame "inactive": http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html

Java jbutton msg box conflict

I have two jbutton in a jpanel and when you click the first button you see a msg box saying "good morning" and when you click the second you see a "good night" msg box . But when i click on the first button and not closing the msg box first i can not click the second button to see the message.
Can anyone help me?
thanks.
This would suggest two basic things, you are using either a JOptionPane or modal JDialog to display your messages.
In either case, a modal dialog blocks the parent window until it is dismissed. You need to make the dialog modeless instead.
Take a look at How to Make Dialogs

i have displayed a JDialog using JOptionPane.showOptionDialog , can someone tell me how to set it inVisible or Dispose it?

i have displyed this JDialog , and have passed an Object which is a JPanel on it , thus my JDialog displays my JPanel on it when Invoked as required.
and on this JPanel I have a JButton, on pressing i want some operations to happen which i have written in it's ActionListener and in the end i have to dispose that JDialog, but i have no clue how to do it !!
This s my JDialog Statement and help me with HOW TO EVEN REMOVE AN ICON from JDialog as even after keeping the ICON PARAMETER NULL it displays the ICON.
JOptionPane.showOptionDialog(null, "SELECT ITEM AND THEN EDIT THE DETAILS.",
"EDIT ITEM DETAILS", int1, int2 , null, objEditMorePane, null);
It sounds like you want to make it a JOptionPane.PLAIN_MESSAGE. That is what you need to put instead of whatever int2 is. I was going to link you to the tutorial but Space Pope already did that. You don't need to create a custom dialog to change the default icon, just change the message type to a plain message. The tutorial covers all this stuff.
You'll need to keep a reference to the dialog if you want to close it yourself. See the Oracle tutorial on custom Dialogs. The constructor you're using also puts in an icon by default; if you make your own dialog, you can control that part too.
JOptionPane closes its dialog when its value property changes. So, you can obtain the parent JOptionPane and set its value to close the window:
JOptionPane optionPane = (JOptionPane)
SwingUtilities.getAncestorOfClass(JOptionPane.class, button);
optionPane.setValue(JOptionPane.CLOSED_OPTION);
If the window wasn't created by JOptionPane, you can use the getTopLevelAncestor method of JComponent to obtain the parent window:
Window window = (Window) button.getTopLevelAncestor();
window.dispose();

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

how to set position of JOptionPane

I'm creating this JOptionPane
JOptionPane.showMessageDialog(this, "File was saved", "Save",
JOptionPane.INFORMATION_MESSAGE);
but my JFrame is big so it is scrollable. When I call this command, a window is created in the bottom right corner and I can only see the header. How I can change the position of this JOptionPane?
According to the api 1.6:
the first parameter is parentComponent:
Defines the Component that is to be the parent of this dialog box. It is used in two ways: the Frame that contains it is used as the Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be null, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).
So there isn't no parameter to set the position of the JOptionPane, but you could at least pass null as first parameter to be sure your JOptionPane is well visible and centered.
You could create a JDialog out of a JOptionPane (see the JOptionPane API to see how to do this), and then display it anywhere you'd like as you can with any JDialog. By the way, perhaps you want to make your JFrame smaller by using JTabbedPanes or CardLayout so you don't have this problem.

Categories

Resources