KEMBAR78
Unit-1 awt advanced java programming | PDF
Advanced Java Programming
by
Amol S. Gaikwad
Lecturer,
Government Polytechnic Gadchiroli
Advanced Java Programming
Unit-I
Abstract Windowing
Toolkit (AWT)
Welcome!
Are you excited for a fun
learning session?
Unit Outcomes
Develop Graphical User (GUI) programs using AWT
Components for given component
Create frame window with specified AWT
components
Develop programs using menu and Dialog Boxes for the given
Arrange the GUI components using specified AWT
components
What is AWT ?
Abstract Window Toolkit (AWT) is one of java's largest packages
AWT containes many classess and methods
We can create user interface with AWT.
AWT classes are present in java.awt package
AWT is use to create and manage windows
Windows Fundamentals
Component class
Container class
Window class
Panel class
Frame class
Fig 1.1 The class hierarchy of Panel and Frame
Component object remembers
foreground,background, text font
Component
Class
Top of AWT heirarchy All UI elements are
subclass of this class
Has 100 public methods
Manages events such as
mouse and keyboard i/p
Container
Class
This class is sublcass of
Component class
Has methods Component
objects to be nested
within it.
This class is responsible for
laying out components
Other conatiner objects
can be stored inside of a
container
It uses various layout managers
Panel Class
This class is sublcass of
Container class
No new methods, simply
implements Container
Applet output is drawn on
surface of Panel object
Panel is superclass for
Applet
Other components can be added to a
Panel object by its add( ) method
Panel window doesn't contain title bar,
menu bar, or border
Window Class
This class creates a top
level window
A top level window is not
contained in any other
object
It directly sits on desktop
Generally we don't create
Window object directly
Instead we use sublcass of Window called Frame
Canvas Class
Canvas is a not a part of
heirarchy
Canvas is a blank window on
which you can draw
It is other type of window
Creating Frame windows
Setting window's dimensions
Frame( )
Frame( String title)
Frame class constructors
void setSize(int newWidth, int newHeight)
void setSize(Dimension newSize)
Dimension getSize( )
Creating Frame windows
Setting a window's title
void setVisible(boolean visibleFlag)
Hiding and showing a window
void setTitle(String newTitle)
setVisible(false)
windowClosing( )
Closing a Frame window
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
TextArea
Button
CheckBox
CheckBoxGroup
Choice
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 add(String name, int index)
String getSelectedItem( )
int getSelectedIndex( )
String[ ] getSelectedItems( )
int[ ] getSelectedIndexes( )
int getItemCount( )
void select(int index)
void select(int index)
String getItem(int index)
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
CardLayoutGridLayout
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
and top to bottom like text editor
FlowLayout( )
FlowLayout
It is default layout manager
FlowLayout(int how)
FlowLayout(int how, int horz, int vert)
Example of Flowlyout
BorderLayout
Border layout devides
window into 5 components
These are north, south , east, west
and center
BorderLayout( )
void add(Component compObj, Object region)
BorderLayout(int horz,
int vert)
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
GridLayout uses two
dimensional grid
Number of rows and columns are
defined
GridLayout( )
GridLayout(int numRows, int numColumns, int horz, int vert)
GridLayout(int numRows,
int numColumns)
Example of GridLayout
CardLayout stores various
other layouts Each layout is like card in a deck
CardLayout( )
The cards held in object off
class/type Panel
CardLayout(int horz, int vert)
CardLayout
The cards that form the deck
are also typically objects of
class/type Panel
void add(Component
panelObj, Object name)
void first(Container deck)
void next(Container
deck)
void previous(Container deck)
void last(Container deck)
CardLayout
void show(Container deck,
String cardName)
Example of CardLayout
We can specify relative
position of components
within cell inside grid
Each component has different size
void setConstraints(Component
comp, GridBagConstraints cons)
Location,size, constraints of
components is contained in object of
GridBagConstraints class
Each row in grid have different
no. of columns
GridBagLayoutGridBagLayout( )
Example of GridBagLayout
Generally top level window
has menubar associated
with it
Menu bar displayes list of top level
menu choices
Menu is subclass of MenuItemEach Menu object contains list of
MenuItem objects
Menu bar containes one or
more Menu objects
Three classes use:
MenuBar, Menu,
MenuItem
Menu Bars,Menus &
Menu Items
Checkable menu items can
also be created using
CheckboxMenuItem class
Menu( ) throws HeadlessException
MenuItem(String itemName)
throws HeadlessException
Menu(String optionName, boolean
removable) throws HeadlessException
Menu(String optionName)
throws HeadlessException
MenuItem( ) throws
HeadlessException Menu Bars,Menus &
Menu Items
MenuItem(String itemName,
MenuShortcut keyAccel)
throws HeadlessException
void setEnabled(boolean enabledFlag)
CheckboxMenuItem( ) throws
HeadlessException
String getLabel( )
void setLabel(String newName)
boolean isEnabled( )
Menu Bars,Menus &
Menu Items
CheckboxMenuItem(String itemName)
throws HeadlessException
Menu add(Menu menu)
MenuItem add(MenuItem item)
CheckboxMenuItem(String
itemName, boolean on) throws
HeadlessException
Menu Bars,Menus &
Menu Items
void setState(boolean
checked)
boolean getState( )
Object getItem( )
Example of MenuBar,Menu,MenuItem
Menus
MenuBar
MenuItems
Dialog boxes are used to get user input
Modeless dialog box: can access
other parts windows
Model dialog box: cannot access
other part of windows
They are child windows of top
level windows
Dialog Boxes
Dialog boxes don't
have menu bars
They function as
frame windows
Dialog(Frame
parentWindow, boolean
mode)
Dialog(Frame
parentWindow, String
title, boolean mode)
Example of DialogBox
Object of FileDialog class is used to
created file dialog box
String getFile( )
It is generally provided by
operating system
FileDialog
FileDialog(Frame parent)
String getDirectory( )
FileDialog(Frame parent,
String boxName, int how)FileDialog(Frame parent,
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
Unit Recap 1
Types of
Winodws
Unit Recap 2
Creating
windowed
programs
Unit Recap 3
AWT Controls.
2
1
3
4
Unit Recap 4
Layout
Managers
References
The Complete Reference Java Seventh Edition - Herbert
Schildt,McGraw Hill Publication
Thank You
For Attending!

Unit-1 awt advanced java programming

  • 1.
    Advanced Java Programming by AmolS. Gaikwad Lecturer, Government Polytechnic Gadchiroli
  • 2.
  • 3.
    Welcome! Are you excitedfor a fun learning session? Unit Outcomes Develop Graphical User (GUI) programs using AWT Components for given component Create frame window with specified AWT components Develop programs using menu and Dialog Boxes for the given Arrange the GUI components using specified AWT components
  • 4.
    What is AWT? Abstract Window Toolkit (AWT) is one of java's largest packages AWT containes many classess and methods We can create user interface with AWT. AWT classes are present in java.awt package AWT is use to create and manage windows
  • 5.
    Windows Fundamentals Component class Containerclass Window class Panel class Frame class Fig 1.1 The class hierarchy of Panel and Frame
  • 6.
    Component object remembers foreground,background,text font Component Class Top of AWT heirarchy All UI elements are subclass of this class Has 100 public methods Manages events such as mouse and keyboard i/p
  • 7.
    Container Class This class issublcass of Component class Has methods Component objects to be nested within it. This class is responsible for laying out components Other conatiner objects can be stored inside of a container It uses various layout managers
  • 8.
    Panel Class This classis sublcass of Container class No new methods, simply implements Container Applet output is drawn on surface of Panel object Panel is superclass for Applet Other components can be added to a Panel object by its add( ) method Panel window doesn't contain title bar, menu bar, or border
  • 9.
    Window Class This classcreates a top level window A top level window is not contained in any other object It directly sits on desktop Generally we don't create Window object directly Instead we use sublcass of Window called Frame
  • 10.
    Canvas Class Canvas isa not a part of heirarchy Canvas is a blank window on which you can draw It is other type of window
  • 11.
    Creating Frame windows Settingwindow's dimensions Frame( ) Frame( String title) Frame class constructors void setSize(int newWidth, int newHeight) void setSize(Dimension newSize) Dimension getSize( )
  • 12.
    Creating Frame windows Settinga window's title void setVisible(boolean visibleFlag) Hiding and showing a window void setTitle(String newTitle) setVisible(false) windowClosing( ) Closing a Frame window
  • 13.
    Output of Framewindow Applet window Frame window
  • 14.
  • 15.
    AWT controls andlayout 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
  • 16.
    AWT controls List AWTsupports following types of controls Labels Push buttons Check boxes Choice list Lists Scroll bars Text Editing
  • 17.
  • 18.
    Labels A label isan 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
  • 19.
    Functions & Exampleof Labels void setText(String str) String getText( ) void setAlignment(int how) void setAlignment(int how) int getAlignment( )
  • 20.
    Buttons A push buttonis 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( )
  • 21.
  • 22.
    Checkbox A checkbox isan 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
  • 23.
    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
  • 24.
    Functions & Exampleof Checkbox boolean getState( ) void setState(boolean on) String getLabel( ) void setLabel(String str)
  • 25.
    CheckboxGroup Creates mutually exclusivecheck 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( ).
  • 26.
  • 27.
    Choice Controls popup listis 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)
  • 28.
  • 29.
    Lists Scrolling selection listis 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
  • 30.
    Functions in Lists voidadd(String name) void add(String name, int index) String getSelectedItem( ) int getSelectedIndex( ) String[ ] getSelectedItems( ) int[ ] getSelectedIndexes( ) int getItemCount( ) void select(int index) void select(int index) String getItem(int index)
  • 31.
  • 32.
    Scroll Bars Scroll barsare 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
  • 33.
    Functions of Scrollbar voidsetValues(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( )
  • 34.
  • 35.
    TextField Single line textentry 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
  • 36.
    Functions of TextFieldclass String getText( ) void setText(String str) String getSelectedText( ) void select(int startIndex, int endIndex) boolean isEditable( ) void setEditable(boolean canEdit) void setEchoChar(char ch)
  • 37.
  • 38.
    TextArea Multiline editor iscreated 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
  • 39.
    Functions & Exampleof 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
  • 40.
    Layout Managers Layout managerdecides 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
  • 41.
  • 42.
    Question Spot !! IdentifyAWT controls and Layout managers for User Interface of Student Registration System of Government Polytechnic Gadchiroli
  • 43.
    Components are placedleft to right and top to bottom like text editor FlowLayout( ) FlowLayout It is default layout manager FlowLayout(int how) FlowLayout(int how, int horz, int vert)
  • 44.
  • 45.
    BorderLayout Border layout devides windowinto 5 components These are north, south , east, west and center BorderLayout( ) void add(Component compObj, Object region) BorderLayout(int horz, int vert)
  • 46.
  • 47.
    Insets Insets is usedto 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( )
  • 48.
    GridLayout GridLayout uses two dimensionalgrid Number of rows and columns are defined GridLayout( ) GridLayout(int numRows, int numColumns, int horz, int vert) GridLayout(int numRows, int numColumns)
  • 49.
  • 50.
    CardLayout stores various otherlayouts Each layout is like card in a deck CardLayout( ) The cards held in object off class/type Panel CardLayout(int horz, int vert) CardLayout The cards that form the deck are also typically objects of class/type Panel
  • 51.
    void add(Component panelObj, Objectname) void first(Container deck) void next(Container deck) void previous(Container deck) void last(Container deck) CardLayout void show(Container deck, String cardName)
  • 52.
  • 53.
    We can specifyrelative position of components within cell inside grid Each component has different size void setConstraints(Component comp, GridBagConstraints cons) Location,size, constraints of components is contained in object of GridBagConstraints class Each row in grid have different no. of columns GridBagLayoutGridBagLayout( )
  • 54.
  • 55.
    Generally top levelwindow has menubar associated with it Menu bar displayes list of top level menu choices Menu is subclass of MenuItemEach Menu object contains list of MenuItem objects Menu bar containes one or more Menu objects Three classes use: MenuBar, Menu, MenuItem Menu Bars,Menus & Menu Items
  • 56.
    Checkable menu itemscan also be created using CheckboxMenuItem class Menu( ) throws HeadlessException MenuItem(String itemName) throws HeadlessException Menu(String optionName, boolean removable) throws HeadlessException Menu(String optionName) throws HeadlessException MenuItem( ) throws HeadlessException Menu Bars,Menus & Menu Items
  • 57.
    MenuItem(String itemName, MenuShortcut keyAccel) throwsHeadlessException void setEnabled(boolean enabledFlag) CheckboxMenuItem( ) throws HeadlessException String getLabel( ) void setLabel(String newName) boolean isEnabled( ) Menu Bars,Menus & Menu Items
  • 58.
    CheckboxMenuItem(String itemName) throws HeadlessException Menuadd(Menu menu) MenuItem add(MenuItem item) CheckboxMenuItem(String itemName, boolean on) throws HeadlessException Menu Bars,Menus & Menu Items void setState(boolean checked) boolean getState( ) Object getItem( )
  • 59.
  • 60.
    Dialog boxes areused to get user input Modeless dialog box: can access other parts windows Model dialog box: cannot access other part of windows They are child windows of top level windows Dialog Boxes Dialog boxes don't have menu bars They function as frame windows Dialog(Frame parentWindow, boolean mode) Dialog(Frame parentWindow, String title, boolean mode)
  • 61.
  • 62.
    Object of FileDialogclass is used to created file dialog box String getFile( ) It is generally provided by operating system FileDialog FileDialog(Frame parent) String getDirectory( ) FileDialog(Frame parent, String boxName, int how)FileDialog(Frame parent, String boxName)
  • 63.
  • 64.
    Activity Time Assessment Test ProgramAssignment Group Discussion
  • 65.
  • 66.
  • 67.
    Summary of Class UnitRecap 1 Types of Winodws Unit Recap 2 Creating windowed programs Unit Recap 3 AWT Controls. 2 1 3 4 Unit Recap 4 Layout Managers
  • 68.
    References The Complete ReferenceJava Seventh Edition - Herbert Schildt,McGraw Hill Publication
  • 69.