0

I'm writing a GUI. I want to print something after I close. But the WindowListener does not work. I write a window then I want to have a boolean to mark the windows is closed. So after I can use that boolean in if clause to write the next statement which should be executed after windows close.

public class MyFrame extends JFrame implements  WindowListener {

    private boolean close=false;

    MyFrame() {
        this.close=false;
        setSize(300,250);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        addWindowListener(this);
           // after help, I've added addWindowListener although still don't 
            //work

    }

    public void windowOpened()/windowIconified()/windowDeiconified()...
    public void windowClosing(WindowEvent e) {close=true;}
    public void windowClosed(WindowEvent e) {close=true;}
}

public class MyFrameControl {
    MyFrame setframe() {
        MyFrame fr = new MyFrame();
        fr.setVisible(true);
        return fr;
    }
}

public class test {
    public static void main(String args[]) {
        MyFrameControl frameCtrl= new MyFrameControl();
        MyFrame tmpFrame = frameCtrl.setframe(); 

        if(tmpFrame.close==true) {
            System.out.println("close is true");
        }
    }
}

When I close the JFrame/MyFrame, isn't the boolean close supposed to be true when the print line is executed? Still can't work under JFrame after adding addWindowListener, is there any solution under JFrame other than switching to JDialogue?

Owen Wong
  • 1
  • 2
  • Instead the eclipse didn't print anything. – Owen Wong May 22 '19 at 06:54
  • 1
    I don't see `addWindowListener` anywhere in the code you posted. You need to call that method on your `JFrame`. This is known as registering a [window] listener. – Abra May 22 '19 at 07:00
  • So even though my jjj has implemented WindowListener , I have to addWindowListener? – Owen Wong May 22 '19 at 07:22
  • Yes, as well as implementing `WindowListener` interface, you also need to register your `WindowListener` with the "window" that you want to listen to. As I wrote in my previous comment, this is called registering a listener. Perhaps you should search the Internet for some examples of using `WindowListener`. – Abra May 22 '19 at 08:11
  • That's the issue. Even after I add the addWindowListener (), it still doesn't work. All the examples online are trying to execute the codes within the overridden functions while I'm trying to execute the statements outside the function after the window closes. As SirFartALot suggested, I have to switch to JDialog. I wonder if there is anything I can do under JFrame? – Owen Wong May 22 '19 at 08:25
  • Method `setVisible()` in class `javax.swing.JFrame` launches the _Event Dispatch Thread_ (EDT) if it hasn't already been launched. Your method `main()` in class `test` runs in a different thread. So if you want to test the value of a variable in one thread while you update that same variable's value in another thread, you need to add code to coordinate between the two threads. – Abra May 22 '19 at 16:01
  • _the next statement which should be executed after windows close_ Usually when the top-level window of a GUI application closes, the application terminates. If you want your application to continue after closing the top-level window, maybe you should rethink your application architecture. Perhaps opening a `JDialog` window from your `JFrame` - as @SirFartALot suggested in his answer - is more appropriate. But since it is unclear to me what your application is supposed to do and what its architecture is, I can only guess what may help you. – Abra May 22 '19 at 16:11

1 Answers1

0

No.

Your code is not waiting for the JFrame to be closed again and like @Abra already mentioned, you never add the WindowListener to the JFrame.

Your k.setVisible(true) returns immediately and your frame remains visible.

So no windowClosing()/windowClosed() is called at the time the condition is checked.

Maybe you could transform your JFrame to a JDialog which can be set to modal (setModal()/setModalityType()). A modal dialog's setVisible() returns after the dialog has been closed.

To simulate this with a JFrame you have somehow to wait for the close to occur. You could put your System.out... in a thread (Runnable) which waits until close==true.

A JFrame is not modal by definition (How to make a JFrame Modal in Swing java)

SirFartALot
  • 1,215
  • 5
  • 25