Java abstract windows toolkit tutorial


Java abstract windows toolkit tutorial - contributed by Pradip Patil

Abstract Windows Toolkit

Awt provides the foundation to the graphic work in Java and events. Using the awt you can create and display button, labels, menu, combo box, textfield and other controls.

Component: most basic class in awt is java.awt.Component on which all awt components are based like button, label, textfield etc.

Component class is derived from the Object classs. Component class consist of large number of methods.

Container: this is the subclass of component class. It allows other component objects to be included in it. Container is responsible for laying out the positions of any component that it contains.

Panel: It is also a subclass of container. Panel is the superclass for applet. Panel is the window that doesn’t have border menu etc. and for adding other component in panel you have to use add() method.

Frame: It is the subclass of window class and it has title bar menu bar border.

Layout Managers

Layout manager is used for the automatic arrangement of the control within window with the help of some type of algorithms . Each container object can contain layout manager associated with it. Layout manager is set by using setLayout() method.

void setLayout(LayoutManager objet) ;

object refer to desired layout manager. If you pass null; it disables the layout manager then you have to pass the size and position manually and for that you have to usesetBound() method.

Java has some predefined layout manager class .

FlowLayout

FlowLayout is the default layout manager. FlowLayout implements a simple layout style, which is similar to how words flow in a text editor. Components are laid out from the upper-left corner, left to right and top to bottom. The constructors for FlowLayout are shown below:

  • FlowLayout( )
  • FlowLayout(int how)
  • FlowLayout(int how, int horz, int vert)

BorderLayout

The BorderLayout class has four narrow, fixed-width components at the edges and one large area in the center. The four sides are referred to as north, south, east, and west. The middle area is called the center.

The constructors defined by BorderLayout are shown below:

  • BorderLayout( )
  • BorderLayout(int horz, int vert)

BorderLayout defines the following constants that specify the regions:

  • BorderLayout.CENTER
  • BorderLayout.SOUTH
  • BorderLayout.EAST
  • BorderLayout.WEST
  • BorderLayout.NORTH

GridLayout

GridLayout layout is a two-dimensional grid. When you instantiate a GridLayout, you define the number of rows and columns.

The constructors supported by GridLayout are shown below:

  • GridLayout( )
  • GridLayout(int numRows, int numColumns )
  • GridLayout(int numRows, int numColumns, int horz, int vert)

CardLayout

The CardLayout class stores several different layouts. Card layout displays the container you pass to it as a card. You can give name to each card then you can move from one card to another. We can prepare the other layouts and have them hidden, ready to be activated when needed.

CardLayout provides the following two constructors:

  • CardLayout( )
  • CardLayout(int horz, int vert)

The first form creates a default card layout.

The second form allows you to specify the horizontal and vertical space left between components in horz and vert respectively.

Use of a card layout requires a bit more work than the other layouts. The cards are typically held in an object of type Panel. This panel must have CardLayout selected as its layout manager.

These are some of the layouts available with layout manager.

AWT components - Some of the awt components are as follows

Labels place text on the screen

Buttons are used very oftento trigger actions and other events.

Check Boxes allow choices

Radio Buttons are a special case of check boxes.

List Boxes are ideal for interacting with arrays of Strings

Choice Boxes are similar to list Boxes but conserve Space.

Text Areas allow the user to enter data.

Scroll bars are ideal for providing access to an integer index.

Event delegation Model: Event broadcasting system in a Java is known as event delegation model

Event : Event is an object that describes state change in source. For handling the event in Java you have to use package java.awt.event.*;

This model mainly depends on 3 things :

1)Event Source: source is an object which generate event..
2)Event Listener :classes which we are used to notify the event.
3)Event Object: classes which described the event

Event class: following are the some important event classes used in Java

1)ActionEvent: Generate when button is pressed.
2)AdjustmentEvent : Generate when scroll bar is manipulate
3)ContainerEvent : Generate when component is added or removed from container.
4)ComponentEvent : Generate when component is move resize or hidden
5)ItemEvent: When checkbox or list box is clicked.
6)InputEvent : subclass for component event.
7)FocusEvent : When component gain or lost i/p focus .
8)KeyEvent : When keyboard i/p occurred.
9)MouseEvent : Mouse related activity happened.
10)WindowEvent : Windows activate.

Event Listener interface:

Listener are created by implementing one or more interface. When event occurs, appropriate listener have to use some of the listener as follows

1)ActionListener : Have one method to receive action event.
2)ItemListener : Specify the method when state of item changed
3)KeyListener: Specify methods to recognized when keys are used.
4)MouseLisener : Define when the mouse is used
5)windowListener :define when window is used.

These are the some Listener which we use.

Anonymous Class: It is a class that is not assigned a name. Anonymous inner class can facilities the writing of event handler.

Eg.import java.applet.*;
import java.awt.event.*;
/*<applet code=demo height=200 width=200>
</applet> */

class demo extends Applet
{
       public void init()
       {
             addMouseListener(new MouseAdapter()
             {
                  Public void mousePressed(MouseEvent e)
                 {
                      showStatus(“mouse Pressed”);
                 }
            });
       }
}

Adapter class: Adapter class provides empty implementation of all methods in an event listener interface. Adapter class is useful when you want to receive and process only some of the events that are handled by particular event listener interface.

Some of the adapter classes are as follow

1)ComponentAdapter
2)KeyAdapter
3)WindowsAdapter
4)MouseAdapter
5)MouseMotionAdapter

Swing :

Swing is the set of class which provides more powerful and flexible components that are possible with awt. Like awt component, swing components are not implemented by platform specific code. They are written entirely in Java. For the use of swing component you have to use package javax.swing.*; using the swing component look and feel of your page is better compared to awt.

AWT SWING
Heavy weight component LightWeight component
Awt component required java.awt package Swing package required javax.swing package
Awt required native code to execute/run Swing don’t required native code
Look and feel is good Look and feel is better than awt.



Write your comment - Share Knowledge and Experience



 
Interview questions
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring