javaswingwindowlistener

How do I access this keyword In a different class


am having a problem trying to access this keyword in a different class using Java programming. I have tried Context, class.this but no help yet...

I have created a project using NetBeans gui builder, I want when i click button the form to get disposed...

Main class contains the click event for disposing the JFrame Form BestQSystems.java:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          CloseWindow.closeWindow(); 

   }  

Class to close the JFrame: CloseWindow.java

import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import javax.naming.Context;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Benson
 */
public class CloseWindow {
    public  static void closeWindow(){
        WindowEvent widnowEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(widnowEvent);
    }
}

Am having an error in this line WindowEvent widnowEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING); Please advise me on how to access this keyword in a different class.


Solution

  • You can pass a reference to this to the other method. For example:

    BestQSystems.java

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          CloseWindow.closeWindow(this); 
    }
    

    and in the CloseWindow.java

    public class CloseWindow {
        public  static void closeWindow(BestQSystems ref){
            WindowEvent widnowEvent = new WindowEvent(ref, WindowEvent.WINDOW_CLOSING);
        }
    }