0% found this document useful (0 votes)
17 views9 pages

Abstract Windowing Toolkit

Java AWT (Abstract Window Toolkit) is an API for developing platform-independent graphical user interfaces in Java, consisting of components, containers, layout managers, and event handling. It includes various classes such as Label, Button, TextField, and more, each with specific constructors and functionalities. AWT also provides layout managers like BorderLayout, FlowLayout, and GridLayout to arrange components within containers effectively.

Uploaded by

pvy1524
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Abstract Windowing Toolkit

Java AWT (Abstract Window Toolkit) is an API for developing platform-independent graphical user interfaces in Java, consisting of components, containers, layout managers, and event handling. It includes various classes such as Label, Button, TextField, and more, each with specific constructors and functionalities. AWT also provides layout managers like BorderLayout, FlowLayout, and GridLayout to arrange components within containers effectively.

Uploaded by

pvy1524
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Abstract Windowing Toolkit (AWT)

Java AWT or Abstract Window Toolkit is an API used for developing GUI (Graphic User
Interfaces) or Window-Based Applications in Java. Java AWT is part of the Java
Foundation Classes (JFC) that provides a way to build platform-independent graphical
applications.
Java AWT (Abstract Window Toolkit) is an API used to create Graphical User Interface
(GUI) or Windows-based Java programs and Java AWT components are platform-
dependent, which means they are shown in accordance with the operating system’s view.
AWT is heavyweight, which means that its components consume resources from the
underlying operating system (OS). The java.awt package contains AWT API classes such
as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List, and so on.

Java AWT Hierarchy

 Components: AWT provides various components such as buttons, labels, text fields,
checkboxes, etc used for creating GUI elements for Java Applications.
 Containers: AWT provides containers like panels, frames, and dialogues to organize
and group components in the Application.
 Layout Managers: Layout Managers are responsible for arranging data in the
containers some of the layout managers are BorderLayout, FlowLayout, etc.
 Event Handling: AWT allows the user to handle the events like mouse clicks, key
presses, etc. using event listeners and adapters.
 Graphics and Drawing: It is the feature of AWT that helps to draw shapes, insert
images and write text in the components of a Java Application

Types of Containers in Java AWT


There are four types of containers in Java AWT:
1. Window: Window is a top-level container that represents a graphical window or dialog
box. The Window class extends the Container class, which means it can contain other
components, such as buttons, labels, and text fields.
2. Panel: Panel is a container class in Java. It is a lightweight container that can be used
for grouping other components together within a window or a frame.
3. Frame: The Frame is the container that contains the title bar and border and can have
menu bars.
4. Dialog: A dialog box is a temporary window an application creates to retrieve user
input.

1. Java AWT Label


Syntax of AWT Label
public class Label extends Component implements Accessible
AWT Label Class Constructors
There are three types of Java AWT Label Class
1. Label():
Creates Empty Label.
2. Label(String str):
Constructs a Label with str as its name.
3. Label(String str, int x):
Constructs a label with the specified string and x as the specified alignment

2. Java AWT Button


AWT Button is a control component with a label that generates an event when
clicked on. Button Class is used for creating a labeled button that is platform-independent.
Syntax of AWT Button
public class Button extends Component implements Accessible
Java AWT Button Class Constructors
There are two types of Button class constructors as mentioned below:
1. Button( ):
Creates a Button with no label i.e. showing an empty box as a button.
2. Button(String str):
Creates a Button with String str as a label. For example if str=”Click Here” button
with show click here as the value.

3. Java AWT TextField


Syntax of AWT TextField:
public class TextField extends TextComponent
TextField Class constructors
There are TextField class constructors are mentioned below:
1. TextField():
Constructs a TextField component.
2. TextField(String text):
Constructs a new text field initialized with the given string str to be displayed.
3. TextField(int col):
Creates a new text field(empty) with the given number of columns (col).
4. TextField(String str, int columns):
Creates a new text field(with String str in the display) with the given number of
columns (col).

4. Java AWT Checkbox


Syntax of AWT Checkbox:
public class Checkbox extends Component implements ItemSelectable, Accessible
Checkbox Class Constructors
There are certain constructors in the AWT Checkbox class as mentioned below:
1. Checkbox():
Creates a checkbox with no label.
2. Checkbox(String str):
Creates a checkbox with a str label.
3. Checkbox(String str, boolean state, CheckboxGroup group):
Creates a checkbox with the str label, and sets the state in the mentioned group.

5. Java AWT CheckboxGroup


CheckboxGroup Class is used to group together a set of Checkbox.
Syntax of AWT CheckboxGroup:
public class CheckboxGroup extends Object implements Serializable
Note: CheckboxGroup enables the use of radio buttons in AWT.

6. Java AWT Choice


The object of the Choice class is used to show a popup menu of choices.
Syntax of AWT Choice:
public class Choice extends Component implements ItemSelectable, Accessible
AWT Choice Class constructor
Choice(): It creates a new choice menu.

7. Java AWT List


The object of the AWT List class represents a list of text items.
Syntax of Java AWT List:
public class List extends Component implements ItemSelectable, Accessible
AWT List Class Constructors
The List of class constructors is defined below:
1. List():
Creates a new list.
2. List(int row):
Creates lists for a given number of rows(row).
3. List(int row, Boolean Mode)
Ceates new list initialized that displays the given number of rows.

8. Java AWT Canvas


Syntax of AWT Canvas:
public class Canvas extends Component implements Accessible
Canvas Class Constructors
1. Canvas():
Creates new Canvas.
2. Canvas(GraphicConfiguration config):
It creates a new Canvas with the given Graphic configuration.

9. AWT Scrollbar
Syntax of AWT Scrollbar:
public class Scrollbar extends Component implements Adjustable, Accessible
Java AWT Scrollbar Class Constructors
There are three constructor classes in Java mentioned below:
1.Scrollbar(): It Creates a new vertical Scrollbar in the Application.
2.Scrollbar(int orientation):
Creates a new vertical Scrollbar with the given orientation.
3.Scrollbar(int orientation, int value, int visible, int mini, int maxi):
Creates a new scrollbar with the orientation mentioned with value as the
default value and [mini, maxi] as the lower and higher limit.

10. Java AWT MenuItem & Menu


MenuItem class adds a simple labeled menu item on the menu. The MenuItem class allows
you to create individual items that can be added to menus. And Menu is a component used
to create a dropdown menu that can contain a list of MenuItem components.
Syntax of Java AWT MenuItem
public class MenuItem extends MenuComponent implements Accessible
Syntax of Java AWT Menu
public class Menu extends MenuItem implements MenuContainer, Accessible
Java AWT PopupMenu is a component that is used for dynamically popping up a menu that
appears when the user right-clicks or performs any other action on a component.
Syntax of AWT PopupMenu
public class PopupMenu extends Menu implements MenuContainer, Accessible

11. Java AWT Panel


Java AWT Panel is a container class used to hold and organize graphical components in a
Java Application.
Syntax of Java AWT Panel:
public class Panel extends Container implements Accessible

Layout Manager
Layout means the arrangement of components within the container. In other way we can say
that placing the components at a particular position within the container. The task of lay
outing the controls is done automatically by the Layout Manager.

Layout Manager
The layout manager automatically positions all the components within the container. If we do
not use layout manager then also the components are positioned by the default layout
manager. It is possible to layout the controls by hand but it becomes very difficult because of
the following two reasons.
 It is very tedious to handle a large number of controls within the container.
 Oftenly the width and height information of a component is not given when we need to arrange
them.
Java provide us with various layout manager to position the controls. The properties like
size,shape and arrangement varies from one layout manager to other layout manager. When
the size of the applet or the application window changes the size, shape and arrangement of
the components also changes in response i.e. the layout managers adapt to the dimensions of
appletviewer or the application window.
The layout manager is associated with every Container object. Each layout manager is an
object of the class that implements the LayoutManager interface.

AWT Layout Packages:

1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.

AWT Layout Manager Classes:


1BorderLayout
The borderlayout arranges the components to fit in the five regions: east, west, north,
south and center.

1. public static final int NORTH


2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER

Constructors of BorderLayout class:

o BorderLayout(): creates a border layout but with no gaps between the components.
o BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal
and vertical gaps between the components.
Commonly Used Methods:
1. toString(): Returns a string which is the representation of the state of border layout.
2. getLayoutAlignmentX(Container parent): Returns the layout alignment along the X-
axis.
3. getLayoutAlignmentY(Container parent): It will return the layout alignment along
the Y-axis.
4. removeLayoutComponent(Component comp): This method is used to remove the
specified component from the borderlayout.
5. getVgap(): Return the vertical gap between the components.
6. getHgap(): Returns the Horizontal gap between the components.
7. setHgap(int hgap): It is used to set the horizontal gap between the components.
8. setVgap(int vgap): It is used to set the vertical gap between the components.
2CardLayout
The CardLayout class manages the components in such a way that only one
component is visible at a time. It treats each component as a card in the container. Only one
card is visible at a time, and the container acts as a stack of cards. The first component
added to a CardLayout object is the visible component when the container is first
displayed.
Constructors:

1. CardLayout(): It is used to create a new card layout with gaps of size is zero.
2. CardLayout(int horizontalgap, int verticalgap): It is used to create a new
CardLayout class with the specified horizontal and vertical gaps.
3.
Commonly Used Methods:

 getLayoutAlignmentX(Container parent): Returns the alignment along the x-axis.


 getLayoutAlignmentY(Container parent): Returns the alignment along the y-axis.
 getVgap(): Used to get the vertical gap between components.
 addLayoutComponent(Component cm, Object cn): Used to add the specified
component to this card layout’s internal table of names.
 getHgap(): Used to get the horizontal gap between components.
 toString(): Returns a string representation of the state of this card layout.
 removeLayoutComponent(Component cm): Used to remove the specified component
from the layout.

3FlowLayout
The FlowLayout is the default layout.It layouts the components in a directional flow.

Constructors :

1. FlowLayout(): It will Construct a new FlowLayout with centered alignment.The


horizontal and vertical gap will be 5 pixels.
2. FlowLayout(int align) : It will Construct a new FlowLayout with given alignment.The
horizontal and vertical gap will be 5 pixels.
3. FlowLayout(int align, int HorizontalGap, int VerticalGap ): It will Construct a new
FlowLayout with given alignment, the given horizontal and vertical gap between the
components.

Commonly used methods:

1. setTitle(String Text): This Method is used to set Title of JFrame. The title you want to
set is passed as a string.
2. getAlignment(): Returns the alignment for this layout.
3. setAlignment(int align): used to set the alignment for this layout.
4. removeLayoutComponent(Component comp): Used to remove the component passed
as argument from the layout.
4GridLayout
GridLayout class represents a layout manager with a specified number of rows and
columns in a rectangular grid. The GridLayout container is divided into an equal-sized of
rectangles, and one of the components is placed in each rectangle. Every rectangle cell has
the same size therefore, they contain a component, which fills the entire cell. When the user
changes or adjusts the size of the container, the size of each rectangles changes accordingly.
Constructors of the class:

1. GridLayout(): It Creates a grid layout with a default of one column per component, in
a single row.
2. GridLayout(int rw, int cl): It creates a grid layout with the specified number of rows
and columns.
3. GridLayout(int rw, int cl, int hgap, int vgap): It creates a grid layout with the
specified number of rows and columns with horizontal and vertical gap.

Commonly Used Methods:

 addLayoutComponent(String str, Component cmp): Adds the specified component


with the specified name to the layout.
 setColumns(int cl): Sets the number of columns in this layout to the specified value.
 setHgap(int hgap): Sets the horizontal gap between components to the specified value.
 setRows(int rw): Sets the number of rows in this layout to the specified value.
 setVgap(int vgap): Sets the vertical gap between components to the specified value.
 layoutContainer(Container pr): Lays out the specified container using this layout.
 toString(): Returns the string representation of this grid layout’s values.

5GridBagLayout
GridBagLayout class is a flexible layout manager. It is used to aligns the
components horizontally, vertically, or along their baseline. It doesn’t require the
components of the same size. Each GridBagLayout object manages a rectangular grid of
cells, dynamic with each component occupying one or more cells, called its display area.
GridBagLayout components are associated with the instance of GridBagConstraints. These
constraints are used to define the component’s display area and their positions. In addition
to its constraints object, the GridBagLayout also considers each component’s minimum and
preferred sizes in order to determine a component’s size. GridBagLayout components are
also arranged in the rectangular grid but can have different sizes and can occupy the
multiple rows or columns.
Constructor:

 GridBagLayout(): It is used to creates a grid bag layout manager.

Commonly Used Methods:

 removeLayoutComponent(Component cmp): Removes the specified component


from this layout.
 getLayoutAlignmentY(Container p): Returns the alignment along the y-axis.
 addLayoutComponent(Component cmp, Object cons): Add the specified component
with the specified name to the layout.
 toString(): Returns a string representation of this grid bag layout’s values.
 getLayoutAlignmentX(Container p): Returns the alignment along the x-axis.
 getConstraints(Component cmp): Gets the constraints for the specified component.
 maximumLayoutSize(Container tar): Returns the maximum dimensions for this
layout given the components in the specified target container.
 minimumLayoutSize(Container par): Determines the minimum size of the parent
container using this grid bag layout.

File Dialog
FileDialog control represents a dialog window from which the user can select a file.

Class declaration

Following is the declaration for java.awt.FileDialog class:

public class FileDialog


extends Dialog

Field
Following are the fields for java.awt.Image class:

 static int LOAD -- This constant value indicates that the purpose of the file dialog window is
to locate a file from which to read.
 static int SAVE -- This constant value indicates that the purpose of the file dialog window is
to locate a file to which to write.

Class constructors
S.N.
Constructor & Description
1FileDialog (Dialog parent) Creates a file dialog for loading a file.
2FileDialog (Dialog parent, String title) Creates a file dialog window with the specified title for
loading a file.
3FileDialog (Dialog parent, String title, int mode) Creates a file dialog window with the specified
title for loading or saving a file.
4FileDialog (Frame parent) Creates a file dialog for loading a file.
5FileDialog (Frame parent, String title) Creates a file dialog window with the specified title for
loading a file.
6FileDialog (Frame parent, String title, int mode) Creates a file dialog window with the specified
title for loading or saving a file.
Class methods
1void addNotify() Creates the file dialog's peer.
2String getDirectory() Gets the directory of this file dialog.
3String getFile() Gets the selected file of this file dialog.
4FilenameFilter getFilenameFilter() Determines this file dialog's filename filter.
5int getMode() Indicates whether this file dialog box is for loading from a file or for saving to a file.
6protected String paramString() Returns a string representing the state of this FileDialog window.
7void setDirectory(String dir) Sets the directory of this file dialog window to be the specified
directory.
8void setFile(String file) Sets the selected file for this file dialog window to be the specified file.
9void setFilenameFilter(FilenameFilter filter) Sets the filename filter for this file dialog window to
the specified filter.
10void setMode(int mode) Sets the mode of the file dialog.

You might also like