Terminate running threads on JFrame close
Asked Answered
D

3

8

How do I invoke extra operations when the user closes a JFrame window? I have to stop existing threads.

As I understand it, setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); causes the frame to be closed and its thread to be stopped. Should threads be closed after JFrame.EXIT_ON_CLOSE?

Client:

static boolean TERMINATE = false;
public static void main(String[] args) {
// some threads created
 while(true) {

                if(TERMINATE){
                         // do before frame closed
                    break;
                         }
              }

}
    private static JPanel startGUI(){
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel gui = new JPanel();
             f.add( gui);
             f.setSize(500,500);
             f.setVisible(true);
             return gui;
        }

I need to close sockets the thread's working with. What is the best practice to do that?

Delaunay answered 3/6, 2012 at 20:22 Comment(1)
See also §12.8. Program Exit.Olvera
E
11

Using JFrame.EXIT_ON_CLOSE actually terminates the JVM (System.exit). All running threads will automatically be stopped.

If you want to perform some action when a JFrame is about to close, use a WindowListener.

JFrame frame = ...
frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        // close sockets, etc
    }
});
Earthworm answered 3/6, 2012 at 20:24 Comment(1)
This is true if the user presses the window close icon at the top left of the JFrame, but isn't true if the JFrame closes by calling dispose() on it, not if a non-daemon thread is running.Triumphal
I
3
  • You have to add a WindowListener to the JFrame.

  • Inside the windowClosing method, you can provide required code.

For example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ClosingFrame extends JFrame {

    private JMenuBar MenuBar = new JMenuBar();
    private JFrame frame = new JFrame();
    private static final long serialVersionUID = 1L;
    private JMenu File = new JMenu("File");
    private JMenuItem Exit = new JMenuItem("Exit");

    public ClosingFrame() {
        File.add(Exit);
        MenuBar.add(File);
        Exit.addActionListener(new ExitListener());
        WindowListener exitListener = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                int confirm = JOptionPane.showOptionDialog(frame,
                        "Are You Sure to Close this Application?",
                        "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, null, null);
                if (confirm == 0) {
                    System.exit(1);
                }
            }
        };
        frame.addWindowListener(exitListener);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setJMenuBar(MenuBar);
        frame.setPreferredSize(new Dimension(400, 300));
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
    }

    private class ExitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int confirm = JOptionPane.showOptionDialog(frame,
                    "Are You Sure to Close this Application?",
                    "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, null, null);
            if (confirm == 0) {
                System.exit(1);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ClosingFrame cf = new ClosingFrame();
            }
        });
    }
}
Insentient answered 3/6, 2012 at 20:26 Comment(0)
P
1

You can set the default close operation on the JFrame

JFrame frame = new JFrame("My Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Pteranodon answered 23/6, 2016 at 17:6 Comment(1)
this method don't kill the thread, they still stay aliveElectroballistics

© 2022 - 2024 — McMap. All rights reserved.