1

Currently I have a live active while loop running and it does what I want my only problem is closing the frame (exit the app) while the while loop is still active.

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

I tried using the if statement to terminate the loop

if (frame.getDefaultCloseOperation() == 3){
    running = false;
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

Unfortunately as soon as the app runs automatically gets set to 3 emmidiately and there for this process does not work.

How can I set running = false; by clicking on the frame to be closed.

I want to simply close the app by just clicking on X in the connour, is that possible when having a active loop running?

spongebob
  • 8,370
  • 15
  • 50
  • 83
  • Maybe try starting another thread that listens for the close event – brso05 Dec 18 '14 at 13:22
  • 1
    `if (frame.getDefaultCloseOperation() == 3)` - don't use magic numbers. Nobody knows (or cares) what 3 is. Use the field names provided by the API. If you can use `JFrame.EXIT_ON_CLOSE` to set the property, you can use the appropriate variable name to test for the property. – camickr Dec 18 '14 at 15:29

4 Answers4

2

There are several ways to achieve what you want.

  1. Run the loop in a daemon thread. Such a thread won't stop the application from stopping. Just be careful when you call swing code from such a thread.

  2. This question explains how to show a confirm dialog when the user tries to close the window: Java - Message when closing JFrame Window

    Instead of the dialog, just put running = false; in there.

    Note: If you run your loop in the Swing thread, then Swing can't respond to the event. Use solution #1 in this case.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    1+ for daemon threads, really interesting topic. Another approach could be using a SwingWorker to perform the `while` loop in a background thread. It gives the possibility to stop (cancel) the SwingWorker on a WINDOW_CLOSING event and do whatever that should be done (i.e.: free resources, close db connections if open, etc.) before exit the application. In other words, it gives you another chance to properly close the application. But, as you've said, there are several ways to achieve this goal. – dic19 Dec 18 '14 at 14:12
1

just set frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); in your declaration it will close the loop automatically

Zion
  • 723
  • 3
  • 7
  • 24
0

You are setting the default close operation. however in your code this

if (frame.getDefaultCloseOperation() == 3){

only checks if the close operation is still set to JFrame.EXIT_ON_CLOSE

what you want is to respond to the close event. I suspect you need this answer

when you set up your frame, add some code to the initialization block

addWindowListener(this);

and then implement the onclosing window listener int he same class:

public void windowClosing(WindowEvent event) {
    running = false;
}
Community
  • 1
  • 1
Joeblade
  • 1,735
  • 14
  • 22
0

remove frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);, add windows listener and close application manually.

frame.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            running = false;
        }

        @Override
        public void windowClosed(WindowEvent e) {
           System.exit(0);
        }
});
Adem
  • 9,402
  • 9
  • 43
  • 58