By running following code and I got following output:

At begining of default constructor, theApplet.isDisplayable()=false
Start Init here...
At begining of init(), theApplet.isDisplayable()=true
At begining of constructor, theApplet.isDisplayable()=false
End Init...

Two questions:
1. Does anybody know why isDisplayable() is changed without do anything on it?
2. Why does the JApplet not run init first, but default constructor?

import javax.swing.*;


public class TestJApplet extends JApplet{

    public  void init() {

          System.out.println("Start Init here...");
          /*this.setVisible(true);
          this.validate();*/



          if (this.isDisplayable()) System.out.println("At begining of init(), theApplet.isDisplayable()=true");
            else System.out.println("At begining of init(), theApplet.isDisplayable()=false");

          new TestJApplet("test");

          System.out.println("End Init...");
      }

    public TestJApplet(){

        if (this.isDisplayable()) System.out.println("At begining of default constructor, theApplet.isDisplayable()=true");
        else System.out.println("At begining of default constructor, theApplet.isDisplayable()=false");
    }

    public TestJApplet(String test){


        if (this.isDisplayable()) System.out.println("At begining of constructor, theApplet.isDisplayable()=true");
        else System.out.println("At begining of constructor, theApplet.isDisplayable()=false");
    }
}

Dani AI

Generated

Short version: the displayable flag is controlled by the AWT/Swing containment/peer plumbing, not by your code directly. The applet container (browser/appletviewer) can add the applet into a displayable component hierarchy (causing addNotify() and a native peer to be created) before or while it calls lifecycle methods, so isDisplayable() can flip to true by the time init() runs. The separate new TestJApplet("test") you create inside init() is a different object that has not been added to any container, so it remains non-displayable.

This ties to what said about constructors: a constructor always runs before any instance method on that instance. The container creates the applet instance (constructor), then at some point the container will attach it to the UI (addNotify) and call init(); those steps are separate. See the Component docs for details on displayable/addNotify and the Applet lifecycle:

Quick troubleshooting you can add to your class to see exactly when the peer is created:

@Override
public void addNotify() {
    super.addNotify();
    System.out.println("addNotify(): now displayable=" + isDisplayable());
}

@Override
public void removeNotify() {
    System.out.println("removeNotify(): before super, displayable=" + isDisplayable());
    super.removeNotify();
}

Other practical notes: don’t instantiate another JApplet inside your applet — create panels/components instead. Also follow Swing threading rules: do UI construction on the Event Dispatch Thread from init() using SwingUtilities.invokeAndWait/invokeLater when appropriate (see SwingUtilities.invokeLater).

Recommended Answers

All 4 Replies

Why does the JApplet not run init first, but default constructor?

init() is an instance method. It cannot be called without an instance. You can't get an instance without executing a constructor. So there will always be a constructor executed before init().

Thanks. How about the first question?

Another question: why can I run init() without any constructor?

If you don't supply a constructor then the compiler gives you a default no-args constructor that just calls the superclass no-args constructor. SO there is always a constructor and it's always run before any instance method can be called.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.