0

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!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MdaCosta
  • 49
  • 1
  • 10
  • Does the message dialog appear when you try to close the window? – VGR Jul 28 '21 at 00:17
  • @VGR no , absolutely nothing . The window still working normally . – MdaCosta Jul 28 '21 at 06:11
  • Please edit your question, and provide code for a [mre] —a complete program which we can compile and run ourselves in order to see the problem happening. – VGR Jul 28 '21 at 12:02
  • I already resolved, exported runnable jar in other computer with eclipse , and I replaced timeElapsed.toSeconds() by timeElapsed.toMinutes(), furthermore I defined close operations as setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE), however I don't believe that this close operation makes difference. Thank you guys for your help. – MdaCosta Jul 28 '21 at 23:09

0 Answers0