Runnable jar doesn't close with WindowListener and method windowClosing

73 Views Asked by At

I am trying to add a JOptionPane Message Dialog when I click on red x of the window, however the window doesn't close and still open without changes.

Example of my code :

Class Janela extends JFrame implements ActionListener{

public Janela() {
        constructWindow();
        
    }
void constructWindow(){
    ...

    setVisible (true);

    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
      addWindowListener(new java.awt.event.WindowAdapter() {
                @Override
           public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                    end = Instant.now();
                    Duration timeElapsed = Duration.between(start, end); 
                    JOptionPane.showMessageDialog(
                            null, 
                            "Text Example",
                            JOptionPane.INFORMATION_MESSAGE, 
                            img_welcome_icon); 
                    System.exit(0);
           }
     });
...
}

On the other hand, if I use the default close operation (setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)) the window close normally, with no problems.

UPDATE: Running on Eclipse, the window runs very well and when closed with WindowListener and WindowClosing method show Joption Message Dialog.... But on the runnable jar is happening what I describe above.

Anyone can help me on this weird situation?

Thank you very much!

0

There are 0 best solutions below