1

I want to close JFrame with click on close button. The frame closed but Window related class is still running.

How can I close the frame so that the whole class is stopped?

public class Game {

public Game2() throws InterruptedException, IOException {        
        new TimerDemo(30,2);
        while (flag) {
            play();
        }
    }
private void play() throws InterruptedException, IOException {

        NewJFrame fr = new NewJFrame();
        fr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        fr.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {     
                e.getWindow().dispose();
                System.out.println("JFrame Closed!");
            }
        });
    }
}

In TimerDemo(30,2), after 30 seconds, play() must be stop.

How to destroy completely class without using System.exit(0)?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mng97
  • 75
  • 9
  • You have a non daemon thread running which is keeping the jvm running, you need terminate them. – MadProgrammer Aug 11 '18 at 04:42
  • You “could” use EXIT_ON_CLOSE, but that terminate the jvm when the first frame is closed - also, your while loop is likely crater LOTS of windows – MadProgrammer Aug 11 '18 at 04:46
  • [Example of idle dialog with timeout](https://stackoverflow.com/questions/31932602/jdialogs-timeout-when-idle/31932893#31932893) but some more context might help provide a more suitable solution – MadProgrammer Aug 11 '18 at 04:49
  • Thanks. Is there a code to destroy the class? (without thread) – mng97 Aug 11 '18 at 05:43
  • I just want to point out that comment doesn't make sense – MadProgrammer Aug 11 '18 at 05:56
  • @MadProgrammer. I write a game that has a menu with 4 items (the first window). If the user selects game1 (the second window) and it's over, all windows are closed. while I wanna just are closed and finished the second window not first. So must be just closed class2. – mng97 Aug 11 '18 at 07:56
  • So, as the liked example demonstrates, simply associate the concept of the timeout with the specific frame/class itself – MadProgrammer Aug 11 '18 at 07:59

1 Answers1

0

Here is an example of using Timer to close a Jframe (in this case) after a certain period :

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

public class DelayedCloseWindow extends JFrame {

    private JLabel counter;
    private int seconds;
    public DelayedCloseWindow(int seconds)  {

        this.seconds = seconds;
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        counter = new JLabel(String.valueOf(seconds));
        counter.setHorizontalAlignment(SwingConstants.CENTER);
        add(counter, BorderLayout.NORTH);
        JButton button = new JButton("Close");
        button.addActionListener(a -> startCountDown());
        add(button, BorderLayout.SOUTH);
        pack();
        setVisible(true);
    }

    private void startCountDown() {
        new Timer(1000, e  -> countDown() ).start();
    }

    private void countDown() {

        if( seconds > 1) {
            counter.setText(String.valueOf(seconds--));
        } else { // time is over
            dispose();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new DelayedCloseWindow(30);
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65