Awt controls ppt

1,033 views 73 slides Feb 23, 2020
Slide 1
Slide 1 of 73
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73

About This Presentation

ABSTRACT WINDOW TOOLKIT- AWT CONTROL
WINDOW FUNDAMENTALS
WORKING WITH FRAME WINDOW
LAYOUT MANAGERS
DIALOG CLASS
FILE DIALOG CLASS
MENUS AND MENUBAR
HANDLING EVENTS BY EXTENDING COMPONENTS



Slide Content

Java GUI Programming
Abstract Windowing Toolkit
(AWT)
AWT-SOUMYA K 1
PRESENTED BY
SOUMYA K
ASSISTANT PROFESSOR
KRISTUJAYANTI COLLEGE
BANGALORE

CONTENTS
•Introduction
•AWT classes
•Window fundamentals
•Working with frame window
•AWT controls
•Layout managers
•Menus and menubar
•Dialog class
•File dialog class
•Handling events by extending controls
AWT-SOUMYA K 2

Abstract Windowing Toolkit(AWT)
•AWT is used for GUI programming in java.
•The AWT contains numerous classes and methods that allow
you to create and manage windows.
•The main purpose of the AWT is to support applet windows
and to create stand-alone windows that run in a GUI
environment, such as Windows.
AWT-SOUMYA K 3

Abstract Windowing Toolkit(AWT)
Because the Java programming language is
platform-independent, the AWT must also be
platform-independent.
The AWT was designed to provide a common set
of tools for graphical user interface design that
work on a variety of platforms.
AWT-SOUMYA K 4

Abstract Windowing Toolkit(AWT)
The user interface elements provided by the
AWT are implemented using each platform's
native GUI toolkit, thereby preserving the look
and feel of each platform. This is one of the
AWT's strongest points.
The disadvantage of such an approach is the fact
that a graphical user interface designed on
one platform may look different when
displayed on another platform.
AWT-SOUMYA K 5

AWT Platform-independence
AWT-SOUMYA K 6
Every AWT component class has an equivalent peer class, and every
component object has a peer object that controls the object's look and
feel.
Button peers are implemented in platform-specific classes that
implement the java.awt.peer ButtonPeer interface.

java.awt.peer
AWT-SOUMYA K 7

AWT Classes
•The AWT classes are contained in the java.awt package.
AWT-SOUMYA K 8

AWT-SOUMYA K 9

AWT Classes
AWT-SOUMYA K 10

AWT Classes (cont...)
AWT-SOUMYA K 11

AWT Classes (cont...)
AWT-SOUMYA K 12

Window Fundamentals
•The two most common windows are those derived from
Panel, which is used by applets, and those derived from
Frame, which creates a standard window.
–Component
–Container
–Panel
–Window
–Frame
–Canvas
AWT-SOUMYA K 13

AWT-SOUMYA K 14

Component
•At the top of the AWT hierarchy is the Component class.
•Component is an abstract class that encapsulates all of the
attributes of a visual component.
•All user interface elements that are displayed on the screen
and that interact with the user are subclasses of Component.
•It defines over a hundred public methods that are responsible
for managing events, such as mouse and keyboard input,
positioning and sizing the window, and repainting.
•A Component object is responsible for remembering the
current foreground and background colors and the currently
selected text font.
AWT-SOUMYA K 15

Container
•The Container class is a subclass of Component.
•It has additional methods that allow other Component
objects to be nested within it.
•Other Container objects can be stored inside of a Container.
•This makes for a multileveled containment system.
•A container is responsible for laying out (that is, positioning)
any components that it contains
AWT-SOUMYA K 16

Panel
•The Panel class is a concrete subclass of Container.
•It doesn’t add any new methods; it simply implements
Container.
•Panel is the superclass for Applet.
•Panel is a window that does not contain a title bar, menu bar,
or border.
•Other components can be added to a Panel object by its
add( ) method (inherited from Container).
AWT-SOUMYA K 17

Window
•The Window class creates a top-level window.
•A top-level window is not contained within any other object; it
sits directly on the desktop.
•subclass of Window calledFrame
AWT-SOUMYA K 18

Frame
•It is a subclass of Window and has a title bar, menu bar,
borders, and resizing corners.
AWT-SOUMYA K 19

Canvas
•ACanvascomponent
represents a blank
rectangular area of the
screen onto which the
application can draw or
from which the
application can trap input
events from the user.
AWT-SOUMYA K 20

Working with Frame Windows
•two Frame’s constructors:
•Frame( )
–creates a standard window that does not contain a title.
•Frame(String title)
–creates a window with the title specified by title.
AWT-SOUMYA K 21

Setting the Window’s Dimensions
•The setSize( ) method is used to set the dimensions of the
window.
•void setSize(int newWidth, int newHeight)
–The new size of the window is specified by newWidth and newHeight.
•void setSize(Dimension newSize)
–the width and height fields of the Dimension object passed in
newSize.
•getSize( )
–is used to obtain the current size of a window
•Dimension getSize( )
–This method returns the current size of the window contained within
the width and height fields of a Dimension object.
AWT-SOUMYA K 22

Hiding and Showing a Window
•frame window will not be visible until you call
setVisible( ).
•void setVisible(boolean visibleFlag)
AWT-SOUMYA K 23

Setting a Window’s Title
•change the title in a frame window using setTitle( ).
–void setTitle(String newTitle)
import java.awt.*;
publicclassFrameWindowExextendsFrame
{
FrameWindowEx(Stringtitle)
{
super();
this.setTitle(title);
this.setVisible(true);
}
publicstaticvoidmain(Stringargs[])
{
FrameWindowEx window=newFrameWindowEx(“Create Window Example");
}
} AWT-SOUMYA K 24

AWT Controls
•Labels
•Push buttons
•Check boxes
•Choice lists
•Lists
•Scroll bars
•Text editing
These controls are subclasses of Component.
AWT-SOUMYA K 25

Adding and Removing Controls
•add( ),-defined by Container
•Component add(Component compObj)
•void remove(Component obj)
AWT-SOUMYA K 26

Layout Managers
•The components are laid out on a container using a layout
manager.
•The size and position of components are controlled by a layout
manager.
•There are several predefined layout manager classes in the AWT:
FlowLayout, GridLayout, BorderLayout, CardLayout and
GridBagLayout.
•It is also possible to define new layout managers.
•Every container is assigned a default layout manager when it is
first created. For Window, the default layout manager is a
BorderLayout.
AWT-SOUMYA K 27

Understanding Layout Managers
•Each Container object has a layout manager associated with
it.
•A layout manager is an instance of any class that implements
the LayoutManager interface.
•The layout manager is set by the setLayout( ) method.
•If no call to setLayout( ) is made, then the default layout
manager is used.
AWT-SOUMYA K 28

•void setLayout(LayoutManager layoutObj)
AWT-SOUMYA K 29

FlowLayout
•FlowLayoutis the default layout manager for panel.
•FlowLayoutimplements 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.
•When no more components fit on a line, the next one appears on
the next line.
•A small space is left between each component, above and below, as
well as left and right.
•Here are the constructors for FlowLayout:
•FlowLayout( )
•FlowLayout(int align)
•FlowLayout(int align, int horz, int vert)
FlowLayoutDemo
AWT-SOUMYA K 30

•public static final int LEFT
•public static final int RIGHT
•public static final int CENTER
•public static final int LEADING
•public static final int TRAILING
AWT-SOUMYA K 31

•FlowLayout():creates a flow layout with centered alignment
and a default 5 unit horizontal and vertical gap.
•FlowLayout(int align):creates a flow layout with the given
alignment and a default 5 unit horizontal and vertical gap.
•FlowLayout(int align, int hgap, int vgap):creates a flow layout
with the given alignment and the given horizontal and vertical
gap.
AWT-SOUMYA K 32

BorderLayout
•The BorderLayout class implements a common layout style
for top-level windows.
•It 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.
•Here are the constructors defined by BorderLayout:
AWT-SOUMYA K 33

•public static final int NORTH
•public static final int SOUTH
•public static final int EAST
•public static final int WEST
•public static final int CENTER
AWT-SOUMYA K 34

•BorderLayout():creates a border layout but with no gaps
between the components.
•BorderLayout(int horz, int vert)
AWT-SOUMYA K 35

GridLayout
•GridLayout lays out components in a two-dimensional grid.
•When you instantiate a GridLayout, you define the number
of rows and columns.
•The constructors supported by GridLayout are shown here:
•GridLayout( )
•GridLayout(int numRows, int numColumns )
•GridLayout(int numRows, int numColumns, int horz, int vert)
AWT-SOUMYA K 36

•GridLayout():creates a grid layout with one column per
component in a row.
•GridLayout(int rows, int columns):creates a grid layout with
the given rows and columns but no gaps between the
components.
•GridLayout(int rows, int columns, int hgap, int vgap):creates
a grid layout with the given rows and columns alongwith given
horizontal and vertical gaps.
AWT-SOUMYA K 37

CardLayout
•The CardLayout class is unique among the other layout managers in that it stores
several different layouts.
•The CardLayout class manages the components in such a manner that only one
component is visible at a time.
•It treats each component as a card that is why it is known as CardLayout.
•Each layout can be thought of as being on a separate index card in a deck that can be
shuffled so that any card is on top at a given time.
•This can be useful for user interfaces with optional components that can be dynamically
enabled and disabled upon user input.
•You can prepare the other layouts and have them hidden, ready to be activated when
needed.
AWT-SOUMYA K 38

•CardLayout provides these two constructors:
–CardLayout():creates a card layout with zero horizontal and vertical
gap.
–CardLayout(int hgap, int vgap):creates a card layout with the given
horizontal and vertical gap.
AWT-SOUMYA K 39

•public void next(Container parent):is used to flip to the next
card of the given container.
•public void previous(Container parent):is used to flip to the
previous card of the given container.
•public void first(Container parent):is used to flip to the first
card of the given container.
•public void last(Container parent):is used to flip to the last
card of the given container.
•public void show(Container parent, String name):is used to
flip to the specified card with the given name.
AWT-SOUMYA K 40

•void add(Component panelObj, Object name);
•void first(Container deck)
•void last(Container deck)
•void next(Container deck)
•void previous(Container deck)
•void show(Container deck, String cardName)
AWT-SOUMYA K 41

Menu Bars and Menus
•A top-level window can have a menu bar associated with it.
•A menu bar displays a list of top-level menu choices. Each choice is
associated with a drop-down menu.
•This concept is implemented in Java by the following classes:
–MenuBar,
–Menu, and
–MenuItem.
•In general, a menu bar contains one or more Menu objects.
•Each Menu object contains a list of MenuItem objects.
AWT-SOUMYA K 42

•Each MenuItem object represents something that can be
selected by the user. Since Menu is a subclass of MenuItem, a
hierarchy of nested submenus can be created.
•It is also possible to include checkable menu items.
•These are menu options of type CheckboxMenuItem and will
have a check mark next to them when they are selected.
AWT-SOUMYA K 43

•To create a menu bar, first create an instance of MenuBar.
•This class only defines the default constructor.
•Next, create instances of Menu that will define the selections
displayed on the bar.
•Following are the constructors for Menu:
–Menu( )
–Menu(String optionName)
–Menu(String optionName, boolean removable)
AWT-SOUMYA K 44

Labels
•Labels are passive controls that do not support
any interaction with the user.
•Label defines the following constructors:
–Label( )
–Label(String str)
–Label(String str, inthow)
•Methods
–void setText(String str)
–String getText( )
•LabelDemo
AWT-SOUMYA K 45

Buttons
•A push button is a component that contains a
label and that generates an event when it is
pressed.
•Button defines these two constructors:
–Button( )
–Button(String str)
•void setLabel(String str)
•String getLabel( )
AWT-SOUMYA K 46

Handling Buttons
•Each time a button is pressed, an action event is generated.
•This is sent to any listeners that previously registered an interest in
receiving action event notifications from that component.
•Each listener implements the ActionListenerinterface. That
interface defines the actionPerformed( ) method, which is called
when an event occurs.
•An ActionEventobject is supplied as the argument to this method.
It contains both a reference to the button that generated the event
and a reference to the string that is the label of the button.
•Usually, either value may be used to identify the button, as you will
see.
•ButtonDemo.java
•ButtonList.java
AWT-SOUMYA K 47

Check Boxes
•A check box is a control that is used to turn an option on or off. It
consists of a small box that can either contain a check mark or not.
•There is a label associated with each check box that describes what
option the box represents.
•You change the state of a check box by clicking on it. Check boxes
can be used individually or as part of a group.
•Check boxes are objects of the Checkbox class.
•Checkbox supports these constructors:
–Checkbox( )
–Checkbox(String str)
–Checkbox(String str, booleanon)
–Checkbox(String str, booleanon, CheckboxGroupcbGroup)
–Checkbox(String str, CheckboxGroupcbGroup, booleanon)
AWT-SOUMYA K 48

Check Boxes
•boolean getState( )
•void setState(boolean on)
•String getLabel( )
•void setLabel(String str)
AWT-SOUMYA K 49

Handling Check Boxes
•Each time a check box is selected or deselected, an item
event is generated.
•This is sent to any listeners that previously registered an
interest in receiving item event notifications from that
component. Each listener implements the ItemListener
interface.
•That interface defines the itemStateChanged( ) method.
•An ItemEventobject is supplied as the argument to this
method.
•It contains information about the event (for example,
whether it was a selection or deselection).
•CheckboxDemo
AWT-SOUMYA K 50

CheckboxGroup
•It is possible to create a set of mutually
exclusive check boxes in which one and only
one check box in the group can be checked at
any one time.
•These check boxes are often called radio
buttons, because they act like the station
selector on a car radio—only one station at a
time
AWT-SOUMYA K 51

CheckboxGroup.
•Check box groups are objects of type
CheckboxGroup.
•Only the default constructor is defined, which
creates an empty group.
•methods
–Checkbox getSelectedCheckbox( )
–void setSelectedCheckbox(Checkbox which)
•CBGroup
AWT-SOUMYA K 52

Choice Controls
•The Choice class is used to create a pop-up list of
items from which the user may choose.
•Thus, a Choice control is a form of menu.
•When inactive, a Choice component takes up only
enough space to show the currently selected item.
•When the user clicks on it, the whole list of choices
pops up, and a new selection can be made.
•Each item in the list is a string that appears as a left-
justified label in the order it is added to the Choice
object.
AWT-SOUMYA K 53

•Choice only defines the default constructor,
which creates an empty list.
•void add(String name)
•String getSelectedItem( )
•intgetSelectedIndex( )
•intgetItemCount( )
•void select(intindex)
•void select(String name)
•String getItem(intindex)
AWT-SOUMYA K 54

Using Lists
•List( )
•List(intnumRows)
•List(intnumRows, booleanmultipleSelect)
•void add(String name)
•void add(String name, intindex)
•String getSelectedItem( )
•intgetSelectedIndex( )
•String[ ] getSelectedItems( )
•int[ ] getSelectedIndexes( )
•intgetItemCount( )
•void select(intindex)
AWT-SOUMYA K 55

•To process list events, you will need to implement the
ActionListenerinterface.
•Each time a List item is double-clicked, an ActionEvent
object is generated.
•Its getActionCommand( ) method can be used to retrieve
the name of the newly selected item.
•Also, each time an item is selected or deselected with a
single click, an ItemEventobject is generated.
•Its getStateChange( ) method can be used to determine
whether a selection or deselectiontriggered this event.
•getItemSelectable( ) returns a reference to the object that
triggered this event.
AWT-SOUMYA K 56

Scroll Bars
•Scroll bars are used to select continuous values between a specified
minimum and maximum.
•Scroll bars may be oriented horizontally or vertically.
•A scroll bar is actually a composite of several individual parts.
•Each end has an arrow that you can click to move the current value
of the scroll bar one unit in the direction of the arrow.
•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.
•The slider box can be dragged by the user to a new position.
•The scroll bar will then reflect this value.
•In the background space on either side of the thumb, the user can
click to cause the thumb to jump in that direction by some
increment larger than 1.
AWT-SOUMYA K 57

•Scrollbar( )
•Scrollbar(int style)
•Scrollbar(int style, int initialValue, int
thumbSize, int min, int max)
AWT-SOUMYA K 58

•void setValues(intinitialValue, intthumbSize,
intmin, intmax)
•intgetValue( )
•void setValue(intnewValue)
•intgetMinimum( )
•intgetMaximum( )
•void setUnitIncrement(intnewIncr)
•void setBlockIncrement(intnewIncr)
AWT-SOUMYA K 59

MenuItem.
•MenuItem( )
•MenuItem(String itemName)
•MenuItem(String itemName, MenuShortcut
keyAccel)
AWT-SOUMYA K 60

•void setEnabled(boolean enabledFlag)
•boolean isEnabled( )
•void setLabel(String newName)
•String getLabel( )
AWT-SOUMYA K 61

CheckboxMenuItem
•CheckboxMenuItem( )
•CheckboxMenuItem(String itemName)
•CheckboxMenuItem(String itemName,
boolean on)
•boolean getState( )
•void setState(boolean checked)
AWT-SOUMYA K 62

•MenuItem add(MenuItem item)
•Menu add(Menu menu)
•Object getItem( )
AWT-SOUMYA K 63

Dialog Boxes
•Dialog boxes are primarily used to obtain user input.
•They are similar to frame windows, except that dialog boxes
are always child windows of a top-level window.
•Also, dialog boxes don’t have menu bars. In other respects,
dialog boxes function like frame windows.
•Dialog boxes may be modal or modeless.
•When a modal dialog box is active,all input is directed to it
until it is closed.
AWT-SOUMYA K 64

•This means that you cannot access other parts of your
program until you have closed the dialog box.
•When a modeless dialog box is active, input focus can be
directed to another window in your program.
AWT-SOUMYA K 65

FileDialog
•Java provides a built-in dialog box that lets the user specify a
file.
•To create a file dialog box, instantiate an object of type
FileDialog.
•Usually, this is the standard file dialog box provided by the
operating system.
•FileDialog constructors:
–FileDialog(Frame parent, String boxName)
–FileDialog(Frame parent, String boxName, int how)
–FileDialog(Frame parent)
AWT-SOUMYA K 66

FileDialog( ) methods
•String getDirectory( )
•String getFile( )
•SampleFrame.java
AWT-SOUMYA K 67

•Dialog(Frame parentWindow, boolean mode)
•Dialog(Frame parentWindow, String title,
boolean mode)
AWT-SOUMYA K 68

Handling Events by Extending AWT
Components
•To extend an AWT component, you must call the
enableEvents( ) method of Component.
–protected final void enableEvents(long eventMask)
•The eventMask argument is a bit mask that defines the events
to be delivered to thiscomponent.
AWT-SOUMYA K 69

AWTEvent constants for mask.
•ACTION_EVENT_MASK
•KEY_EVENT_MASK
•ADJUSTMENT_EVENT_MASK
•MOUSE_EVENT_MASK
•COMPONENT_EVENT_MASK
•MOUSE_MOTION_EVENT_MASK
•CONTAINER_EVENT_MASK
•MOUSE_WHEEL_EVENT_MASK
•FOCUS_EVENT_MASK
•TEXT_EVENT_MASK
•INPUT_METHOD_EVENT_MASK
•WINDOW_EVENT_MASK
•ITEM_EVENT_MASK
AWT-SOUMYA K 70

override the appropriate method
AWT-SOUMYA K 71

Extending Button
•ButtonDemo2
•calls enableEvents( ) so that action events may be received by
this object.
•When an action event is generated, processActionEvent( ) is
called.
•That method displays a string on the status line and calls
processActionEvent( ) for the superclass.
AWT-SOUMYA K 72

Extending Checkbox
•CheckboxDemo2
•It calls enableEvents( ) so that item events may be received by
this object.
•When an item event is generated, processItemEvent( ) is
called.
•That method displays a string on the status line and calls
processItemEvent( ) for the superclass.
AWT-SOUMYA K 73
Tags