We help IT Professionals succeed at work.

Close (X) button in a Frame, addWindowListener not working

jkeebler
jkeebler asked
on
1,321 Views
Last Modified: 2012-06-27
I'm trying to activate the close button (the "X") in a Frame my applet creates.  Everywhere I look I see people using addWindowListener on the Frame to create a windowClosing event handler.  However, when I use the same code and run the applet nothing happens in the applet window (gray box), I get the status bar message "applet not initialized" and the error message (Capture is the applet class):
     java.lang.NoClassDefFoundError: Capture$1
        at Capture.init(Capture.java:92)
        at sun.applet.AppletPanel.run(AppletPanel.java:347)
        at java.lang.Thread.run(Thread.java:536)

What is going on?  Below is the basic code I use to create the frame itself including the addWindowListener bit.  Am I missing something?  Thanks for your help.

popupWin = new Frame("MyScreenshot");
popupWin.add(snapitButton);
popupWin.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
              System.exit(0);
          }
      });                        
popupWin.pack();
popupWin.validate();
popupWin.show();
Comment
Watch Question

in the close window actionlistener are you using setVisible(false);

here is an example to close the window,

// use in the method where you needed
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
  public void windowClosing(java.awt.event.WindowEvent evt) {
 closeDialog(evt);
  }
});

// seperate method
private void closeDialog(java.awt.event.WindowEvent evt) {
setVisible(false);
dispose();
 }
Top Expert 2006

Commented:
you cannot use System.exit() on an applet.
what you can do is Frame.setVisible(false).
note that your frame needs to be declared "final" to be used in the anonymous class.

final popupWin = new Frame("MyScreenshot");
popupWin.add(snapitButton);
popupWin.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
              popupWin.setVisible(false);
          }
      });                    
popupWin.pack();
popupWin.validate();
popupWin.show();
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
If you want to *close* the frame (meaning that perhaps you want to dispose it), then you should call dispose () instead of setVisible (false) because setVisible () will not free all resources allocated to it. But if you don't want to use the frame anymore, you might as well use dispose ().

>> popupWin.setVisible(false);

becomes:

popupWin.dispose () ;
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
rajuvegesna: >> setDefaultCloseOperation

- does not apply to Frames and Applets. You can use it in a JFrame:

setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) ; // or what-ever
Top Expert 2004
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
rajuvegesna>> mayankeagle is right, those methods are just for JFrames.

GrandSchtroumpf>> I understand what you're saying about using the exit.  However, I don't think that's the problem because the applet won't even load.  I tried adding "final" to that line and I got the compile error:
Capture.java:86: <identifier> expected
                final popupWin = new Frame("MyScreenshot");
                               ^
Capture.java:86: cannot resolve symbol
symbol  : class popupWin
location: class Capture
                final popupWin = new Frame("MyScreenshot");
                      ^

mayankeagle>> I'm actually going to have it do something else when the Close button is clicked but the problem is getting the applet to even load with that code in there.

objects and Webstorm>>There's no class called "Capture$1", just Capture.   And if I remove the one line (addWindowListener...)  everything runs fine with no errors.
setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) ;
excluding this you can use the rest for frames too..

if the apple wont even load then the problem may be in your rest of the code..

Author

Commented:
What sort of problem could their be in the rest of the code that works fine except for adding the addWindowListener?
Top Expert 2004

Commented:
>> There's no class called "Capture$1", just Capture

When you compile the Capture.java file containing :

  popupWin.addWindowListener(new WindowAdapter() {

javac normally create 2 .class files:

  Capture.class
  Capture$1.class       <-- for the new anonymous inner class (extending WindowAdapter)



Top Expert 2004

Commented:
If you don't have these 2 files, eitheir javac cannot compile your java code (syntax error, no free disk space, ...) or you're not using a standard java compiler and your compiled code may not be compatible with standards JVMs.

Top Expert 2006

Commented:
> final popupWin = new Frame("MyScreenshot");
sould be
final Frame popupWin = new Frame("MyScreenshot");

Author

Commented:
AHhhhhhhhhhhh, I was totally missing that Capture$1.class file, Webstorm.   It was jaring up Capture.class without it.

I just tried jar'ing both Capture.class and Capture$1.class but I'm getting the same error.  I think we're on the right track though.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
What error/ exception does it give now?
Top Expert 2004

Commented:
Check the jar archive file content with the following command line :
    jar tvf yourfile.jar

Both class file should be in the same package/directory. If these files are in a package, you should specify the package name in your source code:
    package mypack.mypack2;   // in /mypack/mypack2/ directory

Author

Commented:
Thanks for pointing me to that command Webstorm.  The command I was using to jar the two still wasn't including Capture$1.class in the jar:
   jar cvf Capture.jar Capture.class Capture$1.class

Once I tried:
   jar cvf Capture.jar Capture*.jar

everything got in there correctly and the close button worked.

Thanks for your help everybody.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Top Expert 2004

Commented:
:-)

Author

Commented:
:-P
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.