Hunter
Hunter

Reputation: 5

How do I call the JFrame (the class itself) inside a windowslistener method?

I want to reference the JFrame (which is the class itself) inside a WindowsListener method. Is there any way to do this?

    diag_ap.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            this.setEnabled(true); //does not work
        }
    }); 

I expect to call the class frame and disable it so that the only thing that can be pressed is the JDialog box.

Upvotes: 0

Views: 89

Answers (1)

kumarchandresh
kumarchandresh

Reputation: 590

Using this keyword inside new WindowAdapter().windowClosing(event) method refers to the WindowAdapter object that you created.

To refer the object of the JFrame inside WindowAdapter, you should use MyJFrame.this. So, the code should be,

diag_ap.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            MyJFrame.this.setEnabled(true); // replace MyJFrame with name of your JFrame
        }
    });

Upvotes: 1

Related Questions