soapergem
soapergem

Reputation: 10019

Why is my Swing application minimizing when I create a dialog?

I have a simple Swing application which runs in full screen mode. My application instantiates a JFrame, which in turn instantiates a JPanel, where all of the drawing is done in the paintComponent method. I also have a MouseListener watching for certain events.

One of those events (clicking on a certain area of the screen) triggers a prompt. Here is my code to show the prompt (keep in mind this comes from within my class that is extending JPanel).

int choice = JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?", "Quit?", JOptionPane.YES_NO_OPTION);
if ( choice == JOptionPane.OK_OPTION ) {
    dialogOpen = false;
    System.exit(0);
} else {
    dialogOpen = false;
    repaint();
}

The only trouble is... as soon as this code runs, the entire window disappears. More specifically, it looks like it minimizes itself, because I can click on the Java icon in the taskbar and it pops right back up. But how do I stop it from minimizing itself? All I want to do is display a simple prompt!

Upvotes: 1

Views: 720

Answers (1)

soapergem
soapergem

Reputation: 10019

I figured it out. Thanks to everyone for their helpful comments (what is it with people and commenting here, by the way? how come people write everything that could go in an answer in the comments instead?)

Rather than calling .showConfirmDialog(this, ...) (where this is my JPanel subclass), I needed to call .showConfirmDialog(parent, ...) (where parent is my JFrame subclass). Then, I added a WindowStateListener on the JFrame to detect state changes and reset the window state.

Upvotes: 1

Related Questions