Swing - SPLessons
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. [java]package swing; import javax.swing.*; import java.awt.*; import java.awt.event.*; [/java] Create a class that should extend JApplet to inherit the properties. [java]public class HTMLLabelApplet extends JApplet [/java] Create buttons and labels. [java]JButton jbtnOne; JButton jbtnTwo; JLabel jlab; [/java] 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. [java]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); } } [/java] If Applet is restarted then start() method will be called. [java]public void start() { } [/java] If Applet is stopped then stop() method will be called. [java] public void stop() { }[/java] If Applet is destroyed then destroy() method will be called. [java] public void destroy() { } [/java] Initialize GUI setup and flow layout. [java]private void guiInit() { setLayout(new FlowLayout()); [/java] Create buttons, labels and add ActionListeners to the those components. [java]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."); } }); [/java] Add components to the content pane. [java]getContentPane().add(jbtnOne); getContentPane().add(jbtnTwo); getContentPane().add(jlab); [/java] HTMLLabelApplet.java [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); } }[/java] 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.