File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt related to applet and frames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt related to applet and frames" Watch "doubt related to applet and frames" New topic

doubt related to applet and frames

Maya Nair

Joined: Jan 12, 2001
Posts: 3
I tried the following code taken from some mock exam:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class hello extends Applet
public void init()
add(new myButton("BBB"));
public void paint(Graphics g){}
class myButton extends Button
myButton(String label)
public String paramString()
return super.paramString();
public static void main(String a[])
Frame myFrame=new Frame("Frame1");
Applet myapplet=new hello();
Button b=new Button("My Button");
myButton b1=(new hello()).new myButton("ParamButton");
myFrame.addWindowListener(new WindowAdapter (){
public void windowClosing(WindowEvent e){
System.exit(0);} });
}// class hello ends
The above code when run as a standalone Application returns only one button ("MyButtonNew")
Why doesnt the other button("BBB") initialised in the applet appear?
One more thing, when u compile the class the following files are generated.
The first two are understood but why the third one???
Could anyone please explain me as soon as possible??I am planning to give JCP by end of this Jan.
Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
appletviewer does not run main(). hello$1.class file was created because of new WindowAdapter (). Let me know if I am wrong.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Maya,
When you run the code as an application vs an Applet you must explicitly call the Applet <code>init()</code> method.

Re-compile and run. You will see both buttons.
When you load an Applet in a browser, it handles the call to <code>init()</code>. When you load it from an application, your code must replicate the functionality provided by the browser.
Hope that helps.

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Maya Nair

Joined: Jan 12, 2001
Posts: 3
Hi Jane,
That means if I run the code as an application then applets
init() wont get called,the class hello's constructor will be called which in this case will be default no-arg constr.
Actually I was under the impression that when u execute the line
Applet myapplet= new hello();it will first execute init() method of the applet and then continue further...but since init() is called by the browser,things are simpler.
now I got it!!
Thanks for the explaination.
I agree. Here's the link:
subject: doubt related to applet and frames
It's not a secret anymore!