Learning Java
Custom Search

Previous Next

JFC / Swing - Applet

In this exercise, we are going to create an applet for a web page very similar to the SingleSwing application. In the SingleSwing application we extended the class JFrame. For the applet, SimpleSwingApplet, we will extend the class JApplet, which will act as the outermost container.

In the source code below you will note the following steps:

  • Import the same 2 packages as the application did and add the java.applet package.
  •     import java.applet.*;
        import java.awt.*;
        import javax.swing.*;
    
  • Use the new operator to create (or instantiate) the 3 components. Also, initialize them by calling their constructor with some text to be displayed. This is the same as for the application.
  •     JButton btn = new JButton("Do Something");
        JTextField txt = new JTextField("Enter your answer here.");
        JLabel lbl = new JLabel("Protected Area");
    
  • The applet does not have a constructor, but uses an init method to prepare its user interface.
  •     public void init()
    
  • We will not use the super method for the applet. The title on the HTML page that holds the applet comes from the <TITLE> tag of the HTML code.
  •     <TITLE>A Simple Applet</TITLE>
    
  • We won't need the setSize method because the size of the applet is set in the HTML code.
  •     <APPLET CODE="SimpleSwingApplet.class" width=180 height=150>
    
  • Create a container using JPanel to hold the components.
  •     JPanel panl = new JPanel();
    
  • Set the foreground color (used for the text) and the background color by calling the methods setForeground and setBackground.
  •     lbl.setForeground(Color.blue);
        txt.setForeground(Color.red);
        btn.setForeground(Color.green);
        btn.setBackground(Color.yellow);
        panl.setBackground(Color.magenta);
    
  • Using the method, add, place each of our 3 components onto the JPanel.
  •     panl.add(lbl);
        panl.add(txt);
        panl.add(btn);
    
  • Add panl to the JApplet SimpleSwingApplet. Again, we don't need the containername, SimpleSwingApplet, for the add, because it defaults to this container where it is found.
  •     add(panl);
    
  • Upon closing the window in our browser, we don't need to specify any operation like the setDefaultCloseOperation method for the applet. The applet will be stopped when the window is closed. If there are other threads running, such as playing music or video, coding to end these might have to be implemented.
  •  
     
  • We don't need the setVisible() method for the applet.
  •  
     
  • There is no main method for applets. The HTML code will start the execution of the SimpleSwingApplet class.
  •     <APPLET CODE="SimpleSwingApplet.class" width=180 height=150>
    

Following is the code for this applet.

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class SimpleSwingApplet extends JApplet {
    JButton btn = new JButton("Do Something");
    JTextField txt = new JTextField("Enter your answer here.");
    JLabel lbl = new JLabel("Protected Area");

    public void init() {
	JPanel panl = new JPanel();
	lbl.setForeground(Color.blue);
	txt.setForeground(Color.red);
	btn.setForeground(Color.green);
	btn.setBackground(Color.yellow);
	panl.setBackground(Color.magenta);
	panl.add(lbl);
	panl.add(txt);
	panl.add(btn);
	add(panl);
    }
}

Use the following HTML code to display the applet in your browser. Save this code to a file, such as, SimpleApplet.html. Then double click on the name to execute it with it's applet.

<HTML>
<HEAD>
<TITLE>A Simple Applet</TITLE>
</HEAD>
<BODY>
<APPLET CODE="SimpleSwingApplet.class" width=180 height=150>
In case your browser won't show this applet, you will see this text.
</APPLET>
</BODY>
</HTML>

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved