Solved

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

Posted on 2004-03-28
19
1,261 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();
0
Comment
Question by:jkeebler
  • 5
  • 5
  • 3
  • +3
19 Comments
 
LVL 1

Expert Comment

by:rajuvegesna
ID: 10702067
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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10702075
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10702292
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Assisted Solution

by:objects
objects earned 20 total points
ID: 10702648
looks like you haven't uploaded all the class files to your web server, in particulkar Capture$1.class.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10702685
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
 
LVL 13

Accepted Solution

by:
Webstorm earned 30 total points
ID: 10703540
>>     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
 

Author Comment

by:jkeebler
ID: 10705216
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
 
LVL 1

Expert Comment

by:rajuvegesna
ID: 10705965
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
 

Author Comment

by:jkeebler
ID: 10706146
What sort of problem could their be in the rest of the code that works fine except for adding the addWindowListener?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10706794
>> 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
 
LVL 13

Expert Comment

by:Webstorm
ID: 10706835
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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10707012
> final popupWin = new Frame("MyScreenshot");
sould be
final Frame popupWin = new Frame("MyScreenshot");
0
 

Author Comment

by:jkeebler
ID: 10707117
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10711536
What error/ exception does it give now?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10711856
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
 

Author Comment

by:jkeebler
ID: 10713798
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
 
LVL 92

Expert Comment

by:objects
ID: 10718431
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10722004
:-)
0
 

Author Comment

by:jkeebler
ID: 10726108
:-P
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 58
Java: anonymous class 4 37
What browser will run Java? 7 122
Strange router problem - can't access hotmail.com 14 33
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question