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

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();
jkeeblerAsked:
Who is Participating?
 
WebstormConnect With a Mentor Commented:
>>     java.lang.NoClassDefFoundError: Capture$1
The JVM cannot found your inner class (WindowListener subclass i guess).

You need to copy it in the same web directory as Capture.class or in your .jar file, or to recompile your class if your removed the Capture$1.class file.

0
 
rajuvegesnaCommented:
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();
 }
0
 
GrandSchtroumpfCommented:
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();
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Mayank SAssociate Director - Product EngineeringCommented:
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 () ;
0
 
objectsConnect With a Mentor Commented:
looks like you haven't uploaded all the class files to your web server, in particulkar Capture$1.class.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
rajuvegesna: >> setDefaultCloseOperation

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

setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) ; // or what-ever
0
 
jkeeblerAuthor 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.
0
 
rajuvegesnaCommented:
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..
0
 
jkeeblerAuthor Commented:
What sort of problem could their be in the rest of the code that works fine except for adding the addWindowListener?
0
 
WebstormCommented:
>> 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)



0
 
WebstormCommented:
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.

0
 
GrandSchtroumpfCommented:
> final popupWin = new Frame("MyScreenshot");
sould be
final Frame popupWin = new Frame("MyScreenshot");
0
 
jkeeblerAuthor 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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
What error/ exception does it give now?
0
 
WebstormCommented:
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

0
 
jkeeblerAuthor 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.
0
 
objectsCommented:
0
 
WebstormCommented:
:-)
0
 
jkeeblerAuthor Commented:
:-P
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.