JOptionPane window forced in foreground

G

giangiammy

Hi all,

I'm using
JOptionPane.showMessageDialog(frame, s);
to show a message, and let the user press the OK button.
The behaviour I'd like is that the pop up window remaind
in front of all the current windows, and if I click on a background
window is cannot hide my optionpane window.

I have not find an option to do this, but I think it should be
present!
Any idea how I can do this?
(or link to examples)

thanks
giammy
 
V

Vincent van Beveren

JOptionPane.showMessageDialog(frame, s);
to show a message, and let the user press the OK button.
The behaviour I'd like is that the pop up window remaind
in front of all the current windows, and if I click on a background
window is cannot hide my optionpane window.

I have not find an option to do this, but I think it should be
present!
Any idea how I can do this?
(or link to examples)

It should actually work like that. Are you sure you specified the
correct frame argument? When you specify the frame argument correctly
its behavour is like you specified. If that doesn't work, you either
pass null or the wrong window in the frame argument.

Vincent
 
G

giangiammy

hi,

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?

thanks again
giammy


Vincent van Beveren ha scritto:
 
V

Vincent van Beveren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top