Applet cum Application


Applet cum Application: A single program can be made to work as an Applet and also as an Application (that is, running the program as an Applet and Application). It is of Programming logic importance rather than realtime job.

In an applet, we can include a main() method that can be called implicitly by Java runtime environment. As usual, we write an <APPLET> tag to run this applet. This type of programs can easily be generated with Java tools like VJ++, JBuilder through their applet wizard.

The following program can be used both as Applet cum Application. Depending on the button clicked the background of the button changes.

When we use the program as an application, we call the init() method explicitly in the main() method. When worked as an applet, main() method is simply ignored by the browser and calls init() method implicitly. Screen outputs are given at the end of the program when it is worked as an applet and an application.

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

public class AppletApplication extends JApplet implements ActionListener
{	
  JButton rb,gb,bb;
  Container c ;
  public void init()
  {
    c = getContentPane();
    c.setLayout(new FlowLayout());

    c.add(rb = new JButton("Red"));
    c.add(gb = new JButton("Green"));
    c.add(bb = new JButton("Blue"));
    rb.addActionListener(this);
    gb.addActionListener(this);
    bb.addActionListener(this);
  }
  public void actionPerformed(ActionEvent e)
  {
    String str = e.getActionCommand();
    if(str.equals("Red")) 		
      c.setBackground(Color.yellow);
    else if(str.equals("Green")) 	
      c.setBackground(Color.green);
    else if(str.equals("Blue")) 	
      c.setBackground(Color.blue);
  }
  public static void main(String args[])
  {
    JFrame f = new HelperClass();                       // create an object of JFrame class
    AppletApplication aa = new AppletApplication();     // an object current class
    aa.init( );
    f.getContentPane( ).add(aa,"Center");               // to work with application
    f.setTitle("Applet & Application");
    f.setSize(250,200);
    f.setVisible(true);
  }
}
class HelperClass extends JFrame
{
  public HelperClass()
  {
    addWindowListener(new WindowAdapter()   
    {
      public void windowClosing(WindowEvent e)  
      {
        System.exit(0);
    } } ) ;
  }
}

HTML file, File Name: AppletApplication.html



Applet cum Application

Applet cum Application

AppletApplication aa = new AppletApplication();
aa.init();

An object of current class AppletApplication, aa, is created to call the init() method. It is necessary to call the init() method explicitly, if the program runs an application. If the the program works as an applet, the init() method is called implicitly.

The HelperClass is the actual class that creates a frame. The HelperClass extends JFrame. The HelperClass includes only window closing mechanism.

f.getContentPane( ).add(aa,”Center”);

To the container, AppletApplication object aa, is added at the center, using an object of HelperClass, f, that extends JFrame.

====================Extra Reading on Applets==================
1. Applets Vs Applications
2. Applet to Application
3. Application to Applet

Leave a Comment

Your email address will not be published.