Re: JOptionPane window forced in foreground
I'm almost a newbie in java: the code I used is
the following:
void showWarning(String s) {
java.awt.Frame frame = new java.awt.Frame();
JOptionPane.showMessageDialog(frame, s);
}
so, my error is in creating the frame?
how should I create it?
Yes, this will cause the problem you describe. The frame you specify is
not the parent frame, but a newly created instance that is not visible.
You will not be able to focus on the newly created invisible frame....
unfortunatly no one will notice since its invisible... You must pass the
actual parent frame of the component (button/menu item?) that caused the
message.
If the showWarning(String s) method is defined in a JFrame (of JDialog)
class you can do the following:
void showWarning(String s) {
JOptionPane.showMessageDialog(this, s);
}
If the method is defined outside a frame, you must pass it as an argument:
void showWarning(JFrame frame, String s) {
JOptionPane.showMessageDialog(frame, s);
}
Somewhere else you can then:
try {
doSomething();
} catch (SomeException e) {
someOtherObject.showWarning(this, e);
}
If its in an event, you can use the SwingUtilities to get the window:
void actionPerformed(ActionEvent e) {
JFrame frame = (JFrame)SwingUtilities.windowForComponent(
(Component)e.getSource());
showWarning(frame, "Don't press this button!");
}
Something like that.
Good luck,
Vincent