Swing - SPLessons

Swing Applet

Chapter 21

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Swing Applet

Swing Applet

shape Description

An applets are client side web based program i.e executes on web browser. To write an applet developer must write access specifier public .
Swing applets are same as AWT applets, the variation is that Swing extends JApplet and JApplet consists of all the features of Applet because of JApplet is derived from Applet.
JApplet is a high level container that includes panes.Applet life cycle uses five methods such as init(), start(), paint(), stop(), destroy() methods.
The init() and destroy() methods of an applet gets executed only once where as remaining methods of an applet gets executed every time when applet comes into focus uses start() or lost focus uses stop().

shape Syntax

Here class name should extend the Applet and this applet class available in import java.applet.*; package.

import java.applet.*;

Public class MyApplet extends Applet

Swing Applet

shape Example

Create a package and import all the required packages.

package swing;

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

Create a class that should extend JApplet to inherit the properties.

public class HTMLLabelApplet extends JApplet 

Create buttons and labels.

JButton jbtnOne; 
  JButton jbtnTwo; 
 
  JLabel jlab; 

Call the init() method, where invokeAndWait belongs to Swing utility classes used to update the GUI thread.Swing is not thread safe and only repaint() is thread safe in swing. If user tries to update any thing exception will be occured.

public void init() { 
    try { 
      SwingUtilities.invokeAndWait(new Runnable () { 
    	  public void run() { 
        	guiInit(); // initialize the GUI 
        } 
      }); 
    } catch(Exception exc) { 
      System.out.println("Can't create because of "+ exc); 
    } 
  } 

If Applet is restarted then start() method will be called.

public void start() { } 

If Applet is stopped then stop() method will be called.

 public void stop() { }

If Applet is destroyed then destroy() method will be called.

 public void destroy() { } 

Initialize GUI setup and flow layout.

private void guiInit() { 
    setLayout(new FlowLayout()); 

Create buttons, labels and add ActionListeners to the those components.

jbtnOne = new JButton("One"); 
    jbtnTwo = new JButton("Two"); 
 jlab = new JLabel("Press a button."); 
jbtnOne.addActionListener(new ActionListener() {      
      public void actionPerformed(ActionEvent le) {  
        jlab.setText("Button One pressed.");  
      }      
    });      

Add components to the content pane.

getContentPane().add(jbtnOne); 
    getContentPane().add(jbtnTwo); 
    getContentPane().add(jlab);    

HTMLLabelApplet.java

package swing;

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
 
public class HTMLLabelApplet extends JApplet { 
  JButton jbtnOne; 
  JButton jbtnTwo; 
 
  JLabel jlab; 
 
  public void init() { 
    try { 
      SwingUtilities.invokeAndWait(new Runnable () { 
    	  public void run() { 
        	guiInit(); // initialize the GUI 
        } 
      }); 
    } catch(Exception exc) { 
      System.out.println("Can't create because of "+ exc); 
    } 
  } 
 public void start() { 
    
  } 
 
  public void stop() { 
    
  } 
 public void destroy() { 
    
  } 
 private void guiInit() { 
    
    setLayout(new FlowLayout()); 
 
    // Create  buttons and a label. 
    jbtnOne = new JButton("One"); 
    jbtnTwo = new JButton("Two"); 
 
    jlab = new JLabel("Press a button."); 
 
    // Add action listeners for the buttons. 
    jbtnOne.addActionListener(new ActionListener() {      
      public void actionPerformed(ActionEvent le) {  
        jlab.setText("Button One pressed.");  
      }      
    });      
 
    jbtnTwo.addActionListener(new ActionListener() {      
      public void actionPerformed(ActionEvent le) {  
        jlab.setText("Button Two pressed.");  
      }      
    });      
 
    // Add the components to the applet's content pane. 
    getContentPane().add(jbtnOne); 
    getContentPane().add(jbtnTwo); 
    getContentPane().add(jlab);     
  } 
}

Output:
The Swing Applet Output will be as follows. When click on button one and two, it displays Button one pressed and Button two is pressed.

Summary

shape Key Points

  • Swing Applet won’t override paint() method.
  • Swing Applet does not support any rules where as Swing supports threads.