Search code examples
javaeclipseswingappletjapplet

JApplet not running properly in eclipse


I wrote a pretty basic applet program in eclipse:

public class SwingAppletDemo extends JApplet {

    private static final long serialVersionUID = -1935096480915162747L;
    JLabel jl;

    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    new SwingAppletDemo().makeGUI();
                }
            });
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void makeGUI() {

        jl = new JLabel("Press a button!");

        setLayout(new FlowLayout());

        add(jl);
    }   
}

It compiled fine and then I ran it as an applet by right-clicking on the SwingAppletDemo.java and then I selected Run As > Java Applet

The applet viewer opened but no label was displayed in it. Can anyone please tell me where did I went wrong, I referred to a few tutorials but couldn't find the necessary info.

I also tried running it in Google Chrome but there too only an empty applet was displayed.

Thanx in advance!


Solution

  • You are creating a new SwingAppletDemo in your init method which is never shown. You can simply replace:

    new SwingAppletDemo().makeGUI();
    

    with

    makeGUI();