I have a java aplpet in eclipse. How do i get my java applet to load in an html web page. I put the .class file from the bin director in the same directory as my web page. I tried applet tag but it gives me an empty white box. Here is the code for my web page.

<html>
<body >
<applet code="Main2.class" width="800" height="600">
</body>
</html>

Recommended Answers

All 4 Replies

Looks fine to me. You need to get the error message. Does it work when you run it on a local machine? Try putting Main2.class and the html page in the same folder on your local hard drive and double-clicking on the html file to open a browser. See if it works. If not, run it through appletviewer at the command line and see what error mssage(s) you get.

http://java.sun.com/j2se/1.5.0/docs/guide/plugin/developer_guide/using_tags.html#applet

This link may be of some help. From it:

<applet code=Applet1.class width="200" height="200">
    Your browser does not support the <code>applet</code> tag.
    </applet>

You might want to stick in the "Your browser does not support..." line in there. Again, everything looks fine to me, so you need to find the exact error (i.e. can't find Main2.class or something).

I added the browser does not support line and it is still just giving me an empty white box. When I run the applet in the applet viewer it works just fine. Here is my applet code. Did I set it up incorrectly? I believe I have gotten applets to work this way before. Do I need to implement start(), stop(), or destroy() methods?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.applet.Applet;
import javax.swing.*;



public class Main3 extends JApplet {

	//globals for user interface
    private Container content;
    private JPanel centerPanel;			 	//Bottom panel that changes
    private CardLayout card;				//Layout for the applet
    //globals for Euclidean Algorithm
    private static JTextField eucl_ai;
    private static JTextField eucl_bi;
    private static JTextArea eucl_o;
    
    //This will initialize all of the GUIs, even the ones not showing
    public void init() 
    {
    	
    	//Applet Settings
        content = getContentPane();
        setSize(800, 600);
        content.setBackground(Color.GREEN);
        content.setLayout(new BorderLayout());
        
        //This is the panel that contains all the buttons
        JPanel northPanel = new JPanel();
        
        //Button for the Vigenere cipher
        JButton vigenere_b = new JButton("Vigenere Cipher");
        vigenere_b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                vigenere_gui();
            }
        });
        northPanel.add(vigenere_b);

        //Button for the Euclidean Algorithm
        JButton eucl_b = new JButton("Euclidean");
        eucl_b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
            	eucl_gui();
            }
        });
        northPanel.add(eucl_b);

        //Button for Reset
        JButton reset_b = new JButton("Reset");
        reset_b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                reset_gui();
            }
        });
        northPanel.add(reset_b);
        
        //Add the north panel to the applet
        content.add(northPanel, BorderLayout.NORTH);
        
        
        //Set the layout in the applet to cardlayout
        centerPanel = new JPanel();
        card = new CardLayout();
        centerPanel.setLayout(card);
        
        //Set the gui for the Euclidean Algorithm
        JPanel euclPanel = new JPanel();
        euclPanel.setBackground(Color.WHITE);
        SpringLayout eucl_layout = new SpringLayout();
        euclPanel.setLayout(eucl_layout);
        //Text
        JTextField eucl_1 = new JTextField();
        eucl_1.setBorder(BorderFactory.createEmptyBorder());
        eucl_1.setText("Extended Euclidean Algorithm");
        eucl_1.setEditable(false);
        eucl_1.setBackground(Color.WHITE);
        eucl_layout.putConstraint(SpringLayout.WEST, eucl_1, 5, SpringLayout.NORTH, content);
        eucl_layout.putConstraint(SpringLayout.NORTH, eucl_1, 5, SpringLayout.NORTH, content);
        euclPanel.add(eucl_1);
        //Text
        JTextField eucl_2 = new JTextField();
        eucl_2.setBorder(BorderFactory.createEmptyBorder());
        eucl_2.setText("Enter a,b to find gcd(a,b)");
        eucl_2.setEditable(false);
        eucl_2.setBackground(Color.WHITE);
        eucl_layout.putConstraint(SpringLayout.WEST, eucl_2, 5, SpringLayout.NORTH, content);
        eucl_layout.putConstraint(SpringLayout.NORTH, eucl_2, 55, SpringLayout.NORTH, content);
        euclPanel.add(eucl_2);
        //Text
        JTextField eucl_3 = new JTextField();
        eucl_3.setBorder(BorderFactory.createEmptyBorder());
        eucl_3.setText("a:");
        eucl_3.setEditable(false);
        eucl_3.setBackground(Color.WHITE);
        eucl_layout.putConstraint(SpringLayout.WEST, eucl_3, 5, SpringLayout.NORTH, content);
        eucl_layout.putConstraint(SpringLayout.NORTH, eucl_3, 80, SpringLayout.NORTH, content);
        euclPanel.add(eucl_3);
        //User Input
        eucl_ai = new JTextField(3);
        eucl_ai.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        eucl_layout.putConstraint(SpringLayout.WEST, eucl_ai, 25, SpringLayout.NORTH, content);
        eucl_layout.putConstraint(SpringLayout.NORTH, eucl_ai, 80, SpringLayout.NORTH, content);
        euclPanel.add(eucl_ai);
        //Text
        JTextField eucl_4 = new JTextField();
        eucl_4.setBorder(BorderFactory.createEmptyBorder());
        eucl_4.setText("b:");
        eucl_4.setEditable(false);
        eucl_4.setBackground(Color.WHITE);
        eucl_layout.putConstraint(SpringLayout.WEST, eucl_4, 75, SpringLayout.NORTH, content);
        eucl_layout.putConstraint(SpringLayout.NORTH, eucl_4, 80, SpringLayout.NORTH, content);
        euclPanel.add(eucl_4);
        //User Input
        eucl_bi = new JTextField(3);
        eucl_bi.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        eucl_layout.putConstraint(SpringLayout.WEST, eucl_bi, 95, SpringLayout.NORTH, content);
        eucl_layout.putConstraint(SpringLayout.NORTH, eucl_bi, 80, SpringLayout.NORTH, content);
        euclPanel.add(eucl_bi);
        //Submit Button
        JButton eucl_submit = new JButton("Submit");
        eucl_submit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                process_eucl();
            }
        });
        eucl_layout.putConstraint(SpringLayout.WEST, eucl_submit, 5, SpringLayout.NORTH, content);
        eucl_layout.putConstraint(SpringLayout.NORTH, eucl_submit, 105, SpringLayout.NORTH, content);
        euclPanel.add(eucl_submit);
        //Text
        JTextField eucl_5 = new JTextField();
        eucl_5.setBorder(BorderFactory.createEmptyBorder());
        eucl_5.setText("Output:");
        eucl_5.setEditable(false);
        eucl_5.setBackground(Color.WHITE);
        eucl_layout.putConstraint(SpringLayout.WEST, eucl_5, 5, SpringLayout.NORTH, content);
        eucl_layout.putConstraint(SpringLayout.NORTH, eucl_5, 155, SpringLayout.NORTH, content);
        euclPanel.add(eucl_5);
        //Output
        eucl_o = new JTextArea(15,30);
        eucl_o.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        JScrollPane eucl_o_scroll = new JScrollPane(eucl_o);
        eucl_o_scroll.setPreferredSize(new Dimension(490,330));
        eucl_layout.putConstraint(SpringLayout.WEST, eucl_o_scroll, 5, SpringLayout.NORTH, content);
        eucl_layout.putConstraint(SpringLayout.NORTH, eucl_o_scroll, 180, SpringLayout.NORTH, content);
        euclPanel.add(eucl_o_scroll);
        //
        centerPanel.add("eucl", euclPanel);
        
        
        //Set the gui for the vigener cipher
        JPanel vigenerePanel = new JPanel();
        vigenerePanel.setBackground(Color.BLUE);
        vigenerePanel.add(new JButton("button vigenere"));
        centerPanel.add("vigenere", vigenerePanel);
        
        
        //Set the gui for another panel
        JPanel emptyPanel = new JPanel();
        emptyPanel.setBackground(Color.WHITE);
        centerPanel.add("empty", emptyPanel);

        //Center components
        content.add(centerPanel, BorderLayout.CENTER);
    }

    //This method displays the vigenere cipher in the center panel
    public void vigenere_gui() {
        card.show(centerPanel, "vigenere");
    }

    //This method displays the Euclidean in the center panel
    public void eucl_gui() {
        card.show(centerPanel, "eucl");
    }

    //This method resets the gui
    public void reset_gui() {
    	//...
    }
    
    
    //
    // Extended Euclidean Algorithm for integers a & b
    //
    public void process_eucl() {
    	
    	//...
    	
    }
    
    
 
}

One, your class is named Main3, not Main1, so make sure your html file points to the actual class file. I renamed the class to Main1 and compiled it. When I did, it created Main1.class, but it also created these files too:

  • Main1$1.class
  • Main1$2.class
  • Main1$3.class
  • Main1$4.class

Make sure you copy them too. If you only copied Main1.class, you could have problems.

When I did that, it worked, both in Firefox and Internet Explorer, but it only worked in IE after I clicked the "Allow Add-On".

So try renaming Main3 to Main1 in order to match the html file, make sure you copied the $ files too, and try it again. If it still doesn't work, try clearing your browser's cache, exiting it, and opening it again.

The Main3 issue was not my problem that was something else I was trying. My problem was that I wasn't importing the files with the '$' in it. Thanks I really appreciate the help

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.