Solved

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

Posted on 2004-03-28
19
1,266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

628 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