Search code examples
javahtmlnetbeansjungjapplet

JApplet fails to run in HTML page


I have created a JApplet using the JUNG library in Netbeans that compiles and runs normally. However, when I try to create an html file that runs the applet, only a grey pane appears but the components are missing. My class is :

   public class View extends JApplet {

   //Here I declare the buttons etc..

   public View()
{
    initializeComponent();
            fetchGraphs();

}


   public static void main(String[] args) throws IOException{

    f = new JFrame();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    x = screenSize.width;
    y = screenSize.height;


    f.getContentPane().add(new View());
    f.setTitle("Social Network Privacy Settings and Access Control");
    f.setLocation(new Point(15, 20));
    f.setSize(new Dimension(x-20,y-50));
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setResizable(false);

    f.setVisible(true);
}  
}

The method initializeComponent() adds all the components to the main window. I used JFrameBuilder to build some basic components. JFrameBuilder uses a method addComponent(container, component, x, y, width, height) to add components

I use the code below for that:

  contentPane = (JPanel)this.getContentPane();

  //to create the japplet contentpane

  addComponent(contentPane, genGraphButton, (int)(0.35*x),(int)(0.63*y),       
  (int)(0.2*x),28);

  // to add components

Then I create an html file:

    <applet code = 'MyPackage.View' 
    archive = 'MyProject.jar',
    width = 1600, 
    height = 800/>

in the /dist folder but then only a grey pane appears when I try to open it with Mozilla Firefox. The strange thing is that I have created another simple applet, this time with netbeans JBuilder and it runs normally in a web page.

I really need some help!


Solution

  • You mention the JUNG library, it relies on the two third party libraries, Collections-Generic & Cern Colt Scientific Library 1.2.0. As mentioned by @othman they need to be added to the run-time class-path of the applet (added to the archive attribute of the applet element).

    But just so we are clear, make sure the HTML contains more than just the applet element. Something like this:

    <html>
    <body>
    <applet 
        code='MyPackage.View' 
        archive='MyProject.jar,jung.jar,collections.jar,colt-scientific.jar'
        alt='Java is DISABLED in this browser!'
        width='1600'
        height='800'>
    This browser does not recognize the applet element! 
    </applet>
    </body>
    </html>
    

    Of course, you'll need to change the names of the last 3 Jars to their real names.