B.Sc. CSIT 7th Semester Advanced Java Programming

Introduction To Java Awt, Swing and Applet

  Concept of Awt

Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI) or windows-based applications in Java.

Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavy weight i.e. its components are using the resources of underlying operating system (OS).

The java.awt package provides classes for AWT API such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

Java Awt Hierarchy

The hierarchy of Java AWT classes are shown below.

Java awt

Component

All the elements like the button, text fields, scroll bars, etc. are called components. In Java AWT, there are classes for each component as shown in above diagram. In order to place every component in a particular position on a screen, we need to add them to a container.

Container

The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel.

It is basically a screen where the components are placed at their specific locations. Thus it contains and controls the layout of components.

Types of Container

There are four types of containers in Java AWT:

  1. Window
  2. Panel
  3. Frame
  4. Dialog
WIndow

The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. We need to create an instance of Window class to create this container.

Panel

The Panel is the container that doesn’t contain title bar, border or menu bar. It is generic container for holding the components. It can have other components like button, text field etc. An instance of Panel class creates a container, in which we can add components.

Frame

The Frame is the container that contain title bar and border and can have menu bars. It can have other components like button, text field, scrollbar etc. Frame is most widely used container while developing an AWT application.

Creating Java Awt 

Java Awt can be created with the help of frame. There are two ways to create GUI  using frame in Awt.

  1. By extending frame class(inheritance)
  2. By creating object of frame class(association)

An example of Awt

In the example below a simple frame with a textarea is created.

Source Code:

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class example1 {

        public static void main(String[]args) {

         JFrame jf = new JFrame(“Example”);

         jf.setBounds(100,100,500,700);

         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         Container c= jf.getContentPane();

         c.setLayout(null);

         c.setBackground(Color.PINK);

        JTextArea ta= new JTextArea();

        ta.setBounds(100,100,200,100);        

        c.add(ta); 

jf.setvisible(true);

}

}

Output:

Java awt

Concept about Java swing

Java Swing is part of Java Foundation Classes. It is used to create window-based applications which makes it suitable for developing lightweight desktop applications.

Java Swing is built on top of an abstract windowing toolkit API purely written in Java programming language.

Java Swing provides lightweight and platform-independent components, making it suitable and efficient in designing and developing desktop-based applications (systems).

Awt vs Swing

Awt and swing can be differentiated as follows:

AwtSwing
Java AWT is an API to develop GUI applications in JavaSwing is a part of Java Foundation Classes and is used to create various applications.
The components of Java AWT are heavy weighted.The components of Java Swing are light weighted.
Java AWT has comparatively less functionality as compared to Swing.Java Swing has more functionality as compared to AWT.
The execution time of AWT is more than Swing.The execution time of Swing is less than AWT.
The components of Java AWT are platform dependent.The components of Java Swing are platform independent.
MVC pattern is not supported by AWT.MVC pattern is supported by Swing.

Java Applets

An applet is a Java program that can be embedded into a web page. It runs inside the web browser and works at client side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web server.

Applets are used to make the website more dynamic and entertaining.

  • All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.
  • Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer.
  • In general, execution of an applet does not begin at main() method.
  • Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString().

Life Cycle of Applet

The applet life cycle can be defined as the process of how the object is created, started, stopped, and destroyed during the entire execution of its application. It basically has five core methods namely init(), start(), stop(), paint() and destroy().These methods are invoked by the browser to execute.

Along with the browser, the applet also works on the client side, thus having less processing time.

Java awt

There are five methods in the life cycle of applets. They are:

  • init(): The init() method is the first method to run that initializes the applet. It can be invoked only once at the time of initialization. The web browser creates the initialized objects, i.e., the web browser (after checking the security settings) runs the init() method within the applet.
  • start(): The start() method contains the actual code of the applet and starts the applet. It is invoked immediately after the init() method is invoked. Every time the browser is loaded or refreshed, the start() method is invoked. It is also invoked whenever the applet is maximized, restored, or moving from one tab to another in the browser. It is in an inactive state until the init() method is invoked.
  • stop(): The stop() method stops the execution of the applet. The stop () method is invoked whenever the applet is stopped, minimized, or moving from one tab to another in the browser, the stop() method is invoked. When we go back to that page, the start() method is invoked again.
  • destroy(): The destroy() method destroys the applet after its work is done. It is invoked when the applet window is closed or when the tab containing the webpage is closed. It removes the applet object from memory and is executed only once. We cannot start the applet once it is destroyed.
  • paint(): The paint() method belongs to the Graphics class in Java. It is used to draw shapes like circle, square, trapezium, etc., in the applet. It is executed after the start() method and when the browser or applet windows are resized.

Working of Applet life cycle

  • The Java plug-in software is responsible for managing the life cycle of an applet.
  • An applet is a Java application executed in any web browser and works on the client-side. It doesn’t have the main() method because it runs in the browser. It is thus created to be placed on an HTML page.
  • The init(), start(), stop() and destroy() methods belongs to the applet.Applet class.
  • The paint() method belongs to the awt.Component class.
  • In Java, if we want to make a class an Applet class, we need to extend the Applet
  • Whenever we create an applet, we are creating the instance of the existing Applet class. And thus, we can use all the methods of that class.

Syntax for Java Applet

class TestAppletLifeCycle extends Applet {  

       public void init() {  

// initialized objects  

}   

public void start() {  

// code to start the applet   

}  

public void paint(Graphics graphics) {  

// draw the shapes  

}  

public void stop() {  

// code to stop the applet   

}  

public void destroy() {  

// code to destroy the applet   

}  

}  

Swing Class Hierarchy

Java swing class hierarchy can be illustrated as follows:

Java awt

Sample Program to demonstrate Java swing class

Source code:

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class example1 {

        public static void main(String[]args) {

         JFrame jf = new JFrame(“Example”);

         jf.setBounds(100,100,500,700);

         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         Container c= jf.getContentPane();

         c.setLayout(null);

         c.setBackground(Color.PINK);

         //radio button

         JRadioButton r1 = new JRadioButton(“male”);

         r1.setBounds(75,50,100,30);

         c.add(r1);

         JRadioButton r2 = new JRadioButton(“female”);

         r2.setBounds(75,100,100,30);

         c.add(r2);

         r1.setEnabled(false);

         ButtonGroup bg= new ButtonGroup();//only one will be selected

         bg.add(r1);

         bg.add(r2);

         //checkbox

         JCheckBox c1= new JCheckBox(“BSc.CSIT”);

         JCheckBox c2= new JCheckBox(“BCA”);

         JCheckBox c3= new JCheckBox(“BBS”);

         c1.setBounds(75,150,100,30);

         c2.setBounds(75,200,100,30);

         c3.setBounds(75,250,100,30);

         c.add(c1);

         c.add(c2);

         c.add(c3);

         c1.setSelected(true);//select one option in default

         c3.setEnabled(false);

         //dropdown

         String values[] = {“CSIT”,”BCA”,”BASW”};

         JComboBox cb= new JComboBox(values);

         cb.setBounds(75,300,100,30);

         c.add(cb);

         JButton b1= new JButton(“submit”);

         b1.setBounds(200,300,100,30);

         c.add(b1);

         JLabel l1 = new JLabel();

         l1.setBounds(300,400,100,30);

         l1.setForeground(Color.BLUE);

         c.add(l1);

         b1.addActionListener(new ActionListener(){

         public void actionPerformed(ActionEvent e) {

         String item=(String) cb.getSelectedItem();

         l1.setText(item);

         }

         });

        jf.setVisible(true);

        }

}

Output:

Java awt

Also Check out Layout Management in Java

About Author

Karina Shakya