Advanced Java Programming
by
Amol S. Gaikwad
Lecturer,
Government Polytechnic Gadchiroli
Advanced Java Programming
Unit-I
Abstract Windowing
Toolkit (AWT)
Unit Outcomes
Welcome!
Develop Graphical User (GUI) programs using AWT
Components for given component
Create frame window with specified AWT
components
Are you excited for a fun
Arrange the GUI components using specified AWT
learning session?
components
Develop programs using menu and Dialog Boxes for the given
What is AWT ?
Abstract Window Toolkit (AWT) is one of java's largest packages
AWT containes many classess and methods
AWT classes are present in java.awt package
AWT is use to create and manage windows
We can create user interface with AWT.
Windows Fundamentals
Component class
Container class
Window class
Panel class
Frame class
Fig 1.1 The class hierarchy of Panel and Frame
Top of AWT heirarchy All UI elements are
subclass of this class
Has 100 public methods
Component Manages events such as
Class mouse and keyboard i/p
Component object remembers
foreground,background, text font
This class is sublcass of Has methods Component
Component class objects to be nested
within it.
This class is responsible for
Other conatiner objects
laying out components Container
can be stored inside of a
Class
container
It uses various layout managers
This class is sublcass of No new methods, simply
Container class implements Container
Applet output is drawn on Panel is superclass for
Panel Class
surface of Panel object Applet
Panel window doesn't contain title bar,
menu bar, or border Other components can be added to a
Panel object by its add( ) method
This class creates a top A top level window is not
level window contained in any other
object
Generally we don't create
It directly sits on desktop Window object directly
Window Class
Instead we use sublcass of Window called Frame
Canvas is a not a part of Canvas is a blank window on
heirarchy which you can draw
Canvas Class
It is other type of window
Creating Frame windows
Frame class constructors
Frame( )
Frame( String title)
Setting window's dimensions
void setSize(int newWidth, int newHeight)
void setSize(Dimension newSize)
Dimension getSize( )
Creating Frame windows
Hiding and showing a window
void setVisible(boolean visibleFlag)
Setting a window's title
void setTitle(String newTitle)
Closing a Frame window
setVisible(false)
windowClosing( )
Output of Frame window
Applet window Frame window
Sample AWT Application
AWT Application
AWT controls and layout managers
Controls are components that allows to interact with the
application
Layout managers automatically positions components
within a container
Menu bar creates a drop down menus of options from
which we can select options
AWT controls List
AWT supports following types of controls
Labels
Push buttons
Check boxes
Choice list
Lists
Scroll bars
Text Editing
AWT controls
Label
TextField
CheckBoxGroup
Choice
CheckBox
TextArea
Button
Labels
A label is an object of class/type Label
It contains a string displayed on screen
It is passive control and doesn't support any interaction
Constructors of Label class
Label( ) throws HeadlessException
Label(String str) throws HeadlessException
Label(String str, int how) throws HeadlessException
Functions & Example of Labels
void setText(String str)
String getText( )
void setAlignment(int how)
void setAlignment(int how)
int getAlignment( )
Buttons
A push button is an object of class/type Button
A push button contains a label
It can be pressed to generate event
Button( ) throws HeadlessException
Button(String str) throws HeadlessException
void setLabel(String str)
String getLabel( )
Example of Buttons
Checkbox
A checkbox is an object of class/type Checkbox
It has a small box which can be checked
Checkbox has label
It's state can be changed by clicking it
Check boxes can be used individually or part of a group
Constructors of Checkbox
Checkbox( ) throws HeadlessException
Checkbox(String str) throws HeadlessException
Checkbox(String str, boolean on) throws HeadlessException
Checkbox(String str, boolean on, CheckboxGroup cbGroup) throws
HeadlessException
Checkbox(String str, CheckboxGroup cbGroup, boolean on)
throws HeadlessException
Functions & Example of Checkbox
boolean getState( )
void setState(boolean on)
String getLabel( )
void setLabel(String str)
CheckboxGroup
Creates mutually exclusive check boxes
Also called as radio button, only one can be selected
First we need to create group and then individual checkboxes
Check box groups are objects of class / type CheckboxGroup.
Checkbox getSelectedCheckbox( )
void setSelectedCheckbox(Checkbox which)
setSelectedCheckbox( ).
getSelectedCheckbox( ).
Example of CheckboxGroup
Choice Controls
popup list is created using Choice class
Each item the popup list is string and left justifiable
void add(String name)
String getSelectedItem( )
int getSelectedIndex( )
int getItemCount( )
void select(int index)
void select(int index)
String getItem(int index)
Example of Choice Controls
Lists
Scrolling selection list is created using List class
Multiple selection of options are available
Can show any number of choices in visible window
List( ) throws HeadlessException
List(int numRows) throws HeadlessException
List(int numRows, boolean multipleSelect) throws HeadlessException
Functions in Lists
void add(String name) void select(int index)
void add(String name, int index) void select(int index)
String getSelectedItem( )
String getItem(int index)
int getSelectedIndex( )
String[ ] getSelectedItems( )
int[ ] getSelectedIndexes( )
int getItemCount( )
Functions in Lists
Scroll Bars
Scroll bars are used to select continuous values between specified
minimum and maximum
The current value of the scroll bar relative to its minimum and
maximum values is indicated by the slider box (or thumb) for the
scroll bar.
Scroll bars are created using Scrollbar class
Scrollbar( ) throws HeadlessException
Scrollbar(int style) throws HeadlessException
Scrollbar(int style, int initialValue, int thumbSize, int min, int max)
throws HeadlessException
Functions of Scrollbar
void setValues(int initialValue, int thumbSize, int min, int max)
int getValue( )
void setValue(int newValue)
int getMinimum( ), int getMaximum( )
void setUnitIncrement(int newIncr)
void setBlockIncrement(int newIncr)
getAdjustmentType( )
Example of Scrollbar
TextField
Single line text entry is created using TextField class
Also called edit control
TextField class is a subclass of TextComponent class
TextField( ) throws HeadlessException
TextField(int numChars) throws HeadlessException
TextField(String str) throws HeadlessException
TextField(String str, int numChars) throws HeadlessException
Functions of TextField class
String getText( )
void setText(String str)
String getSelectedText( )
void select(int startIndex, int endIndex)
boolean isEditable( )
void setEditable(boolean canEdit)
void setEchoChar(char ch)
Example of TextField
TextArea
Multiline editor is created using TextArea class
TextArea( ) throws HeadlessException
TextArea(int numLines, int numChars) throws HeadlessException
TextArea(String str) throws HeadlessException
TextArea(String str, int numLines, int numChars) throws HeadlessException
TextArea(String str, int numLines, int numChars, int sBars) throws
HeadlessException
Functions & Example of TextArea
void append(String str)
void insert(String str, int index)
void replaceRange(String str, int
startIndex, int endIndex)
TextArea class is subclass of
TextComponent class
Layout Managers
Layout manager decides where to place controls in window
Each container object has layout manager associated with it
Layout manager is instance/object of any class that implements
LayoutManager interface
void setLayout(LayoutManager layoutObj)
void setLayout(LayoutManager layoutObj)
If setLayout() function is not used then default
layout manager is used
LayoutManager classes
FlowLayout BorderLayout
GridLayout CardLayout
GridBagLayout
Question Spot !!
Identify AWT controls and Layout managers for
User Interface of Student Registration System of
Government Polytechnic Gadchiroli
Components are placed left to right
It is default layout manager
and top to bottom like text editor
FlowLayout
FlowLayout( ) FlowLayout(int how)
FlowLayout(int how, int horz, int vert)
Example of Flowlyout
These are north, south , east, west
Border layout devides
and center
window into 5 components
BorderLayout(int horz,
BorderLayout int vert)
BorderLayout( )
void add(Component compObj, Object region)
Example of BorderLayout
Insets
Insets is used to provide small amount of space betweeen the container
that holds your components and the window that contains it.
getInsets( ) method of container class is used for this.
Insets(int top, int left, int bottom, int right)
Insets getInsets( )
GridLayout uses two Number of rows and columns are
dimensional grid defined
GridLayout(int numRows,
int numColumns)
GridLayout
GridLayout( )
GridLayout(int numRows, int numColumns, int horz, int vert)
Example of GridLayout
CardLayout stores various
other layouts Each layout is like card in a deck
CardLayout(int horz, int vert)
CardLayout
CardLayout( )
The cards held in object off The cards that form the deck
class/type Panel are also typically objects of
class/type Panel
void add(Component void first(Container deck)
panelObj, Object name)
void last(Container deck)
CardLayout
void next(Container
deck)
void show(Container deck,
void previous(Container deck)
String cardName)
Example of CardLayout
We can specify relative
position of components
Each component has different size
within cell inside grid
Each row in grid have different
no. of columns
GridBagLayout( ) GridBagLayout
Location,size, constraints of void setConstraints(Component
components is contained in object of comp, GridBagConstraints cons)
GridBagConstraints class
Example of GridBagLayout
Generally top level window
has menubar associated Menu bar displayes list of top level
with it menu choices
Menu bar containes one or
more Menu objects
Three classes use: Menu Bars,Menus &
MenuBar, Menu, Menu Items
MenuItem
Each Menu object contains list of Menu is subclass of MenuItem
MenuItem objects
Checkable menu items can
also be created using Menu( ) throws HeadlessException
CheckboxMenuItem class
Menu(String optionName)
throws HeadlessException
MenuItem( ) throws
HeadlessException Menu Bars,Menus &
Menu Items
Menu(String optionName, boolean MenuItem(String itemName)
removable) throws HeadlessException throws HeadlessException
MenuItem(String itemName,
MenuShortcut keyAccel)
void setEnabled(boolean enabledFlag)
throws HeadlessException
void setLabel(String newName)
boolean isEnabled( )
Menu Bars,Menus &
Menu Items
String getLabel( ) CheckboxMenuItem( ) throws
HeadlessException
CheckboxMenuItem(String itemName) CheckboxMenuItem(String
throws HeadlessException itemName, boolean on) throws
HeadlessException
void setState(boolean Menu Bars,Menus & boolean getState( )
checked)
Menu Items
Object getItem( )
MenuItem add(MenuItem item)
Menu add(Menu menu)
Example of MenuBar,Menu,MenuItem
Menus
MenuBar
MenuItems
They are child windows of top
Dialog boxes are used to get user input
level windows
Dialog boxes don't
have menu bars
They function as
Dialog(Frame Dialog Boxes frame windows
parentWindow, String
title, boolean mode)
Dialog(Frame
parentWindow, boolean
Model dialog box: cannot access mode)
other part of windows
Modeless dialog box: can access
other parts windows
Example of DialogBox
Object of FileDialog class is used to It is generally provided by
created file dialog box operating system
FileDialog(Frame parent)
String getDirectory( )
FileDialog
String getFile( )
FileDialog(Frame parent,
FileDialog(Frame parent, String boxName, int how)
String boxName)
Example of FileDialog
Activity Time
Assessment Test
Program Assignment
Group Discussion
Supplemental
Video
https://nptel.ac.in/courses/106/105/1
06105191/
Additional Resources
https://www.tutorialspoint.com/java
https://www.javatpoint.com/free-java-
projects
Summary of Class
1
2 3
Unit Recap 1 Unit Recap 2
Unit Recap 3
Types of Creating
AWT Controls.
Winodws windowed
programs
4
Unit Recap 4
Layout
Managers
References
The Complete Reference Java Seventh Edition - Herbert
Schildt,McGraw Hill Publication
Thank You
For Attending!