How do you close a window in Java?

Status
Not open for further replies.
This is probably a very simple question, but I cannot seem to find the anser anywhere. I am unable to do this in a few places in my program, but the most basic is this. I have an applet, a window with a stock ticker, a scroll bar, and some buttons. One of the buttons is "exit". All I want this button to do is close the window, basically do the same thing as the "X" button does in all windows programs. How can I do this?
 
I tried the this.dispose() just now, and I keep getting this error:

Ticker.java [243] cannot resolve symbol
symbol : method dispose ()
location: class Ticker.ButtonHandler
this.dispose();
^
Note: c:\documents and settings\rob\java files\sampledir\Ticker.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
1 error
Errors compiling actionPerformed.

What does this mean?
 

Nimdok

Ars Scholae Palatinae
1,214
Maybe you are trying to call dispose() from a static inner class? Posting some code would help narrow down the problem.

My guess is you have something like:

Code:
public class Ticker {


  protected static class ButtonHandler implements ActionListener {

    public void actionPerformed() {
      this.dispose();
    }

  }


}

Try:

Code:
public class Ticker {


  protected class ButtonHandler implements ActionListener {

    public void actionPerformed() {
      dispose();
    }

  }

}
 

Freebird

Wise, Aged Ars Veteran
136
Subscriptor++
I suspect Nimdok is right that your issue is related to the scope of "this". i don't know if just dispose() by itself will work, but doing variableNameOfWindow.dispose(); might work.
also if you already have a method that handles when the X is clicked, why not just have the buttonActionPerformed method call that one.

as a side note, setDefaultCloseOperation only changes what happens when the X is clicked, it doesn't actually invoke a close operation.

Jason
 
Status
Not open for further replies.