Abstract Windowing Toolkit
Abstract Windowing Toolkit
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.
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
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.
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.
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.
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:
3FlowLayout
The FlowLayout is the default layout.It layouts the components in a directional flow.
Constructors :
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.
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:
File Dialog
FileDialog control represents a dialog window from which the user can select a file.
Class declaration
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.