3

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();
}
Community
  • 1
  • 1
GregH
  • 5,125
  • 8
  • 55
  • 109
  • as a temporary fix, I have setVisible to false and setDefaultCloseOperation to JFrame.DISPOSE_ON_CLOSE however this seems bad practice as I am creating an invisible, unused JFrame – GregH Dec 15 '15 at 22:07
  • What do you want to happen? Just the JFrame and The JOptionPane? – Kebab Programmer Dec 15 '15 at 22:16
  • Post a screenshot of your working code in your question – Kebab Programmer Dec 15 '15 at 22:16
  • The problem you seem to be describing is actually an issue with your OS, I know, annoying :P – MadProgrammer Dec 15 '15 at 22:32
  • *"The message is not always on top of other windows"* What is the parent component of the option pane? 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Dec 16 '15 at 10:33

2 Answers2

1

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);
}
Hari Kiran
  • 188
  • 13
0

Try to put setVisible() after dispose(), exchange them.

Arducode
  • 15
  • 6