Solved

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

Posted on 2004-03-28
19
1,251 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:mayankeagle
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
 
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:mayankeagle
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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:mayankeagle
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now