Java: Turn Your .class into an Applet

The following Tech-Recipes tutorial describes how to modify your existing java GUIs into applets to share with the web-viewing world.


Note that most file transfers are not possible in applets. It just does not work like that. There are ways around it to do some read or write operations, but that is beyond this simple transformation tech recipe.

An applet is basically the GUI you already made, except it does not have a frame. The frame is supplied by the browser opening the .class file.

It is quite a simple task, assuming your code was written using panels instead of frames (as you should have). This will allow for easy transfer from java code to applet.

There are a few methods that are required to make an applet work:
public void init() {}
This method is run when the applet is being loaded into memory or constructed. Think of this as the applet constructor.

public void start() {}
This is run once all preparations are done, and the code is actually beginning to run. Place any splash screens or intro parts here.

public void stop() {}
This is run when the applet has been terminated, while memory is being freed and the code is stopping.

public void destroy() {}
This method is called just before a system exit, notifying that the applet has finished and cannot be run again without reloading it.

CODE Example (applet.class):

import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class applet extends JApplet{
static final int WIDTH = 500;
static final int HEIGHT = 60;

JTextField title;

public class display extends JPanel{
public display(){
Container container;
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
getContentPane().setLayout(layout);
container = getContentPane();

JLabel label1 = new JLabel("Name:");
layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(1,1,1,1);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 2.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(label1, layoutConstraints);
container.add(label1);

title = new JTextField();
layoutConstraints.gridx = 1; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 2; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(1,1,1,1);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 40.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(title, layoutConstraints);
container.add(title);
}
}

public void init() {
System.out.println("Applet initializing");
display d = new display();
getContentPane().add(d);
}
public void start() {
System.out.println("Applet starting");
}
public void stop() {
System.out.println("Applet stopping");
}
public void destroy() {
System.out.println("Applet destroyed");
}
public static void main(String args[]){
JFrame frame = new JFrame("Code Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
applet a = new applet();

a.init(); // Do what browser normally does
frame.getContentPane().add(a); // Add applet to frame

frame.setSize(WIDTH, HEIGHT); // Set the size of the frame
frame.setVisible(true); // Show the frame
}
}

*Note: I have added the JFrame information into the main of the java file. This allows us to test our program as we go along without having to run the html file.

CODE Example (runApplet.html):








A simple applet tage designating the source and display size is all that is necessary within the head of your html file.

NOTE: Your applet has more than one .class file, but the main file is the only one that needs to be called. The html and class files should be located in the same directory to ensure all .class files can be found.

Questions/Comments: william_a_wilson@hotmail.com
-William. ยง (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.

3 Responses to “Java: Turn Your .class into an Applet”

  1. October 31, 2008 at 7:53 am, godalpha said:

    Very good tutorial.Thank for your teaching.

    Reply

  2. April 18, 2012 at 6:32 pm, ABtexas130 said:

    Thanks for this information. It has helped me out a ton.

    Reply

  3. April 24, 2013 at 9:43 am, Ben said:

    can someone please tell me how to make this work in web..
    how to make this to run as applet or in web browser…

    import java.util.Scanner; // I’will use something external to my program

    public class Students { // the name of program

    public static void main (String args[]){ // declaration of a main method

    Scanner Students = new Scanner (System.in); // creates a “scanner” that is used to read input data

    int possibility; // declaring a Options
    int failed = 0; // declaring 2 as failed Student
    int passed = 0; // declaring 1 as passed Student

    for(int i = 1; i 0)
    System.out.println(” And Ben Mondo was among the succesful Students, well done Ben”); // print the argument
    else
    System.out.println(” And Ben Mondo was not succesful this time”);
    }

    }// Ending the program

    Reply

Leave a Reply