Reputation: 5457
I currently am using a JOptionPane
to pop up a message and display it to the user. The message is not always on top of other windows so I put it in a dummy JFrame
(errorFrame
) and set errorFrame
to always be on top. This works for keeping the errorFrame
always on top, but it creates a second, empty frame in the upper right of the screen. The option pane appears at 100,100 just like I am setting the location of the dummy frame which contains it. Why is this second frame being created? All help is appreciated
UPDATE: I got the idea to put the JOptionPane
inside of a JFrame
from this post:
JOptionPane won't show its dialog on top of other windows
try {
JFrame errorFrame = new JFrame();
errorFrame.setVisible(true);
errorFrame.setAlwaysOnTop(true);
if (true) {
JOptionPane.showMessageDialog(errorFrame,
"blah blah",
"blahblahblah",
JOptionPane.WARNING_MESSAGE);
return true;
}
errorFrame.dispose();
}
Upvotes: 3
Views: 335
Reputation: 208
Instead of creating a dummy frame, set the parent frame to be temporarily on top
JFrame frame = new JFrame("Parent");
if(test condition){
frame.setAlwaysOnTop(true);
int choice = JOptionPane.showMessageDialog(frame,
"blah blah","blahblahblah",JOptionPane.WARNING_MESSAGE);
if(choice!=null)
frame.setAlwaysOnTop(true);
}
Upvotes: 1