awt

61
AWT AWT is a java API that facilitate development of GUI based Application in java. API :- represents set of classes & interface that are design to be used in development of specific kind of application. AWT API is defined in java.awt & its subpackages.

Upload: sahara

Post on 24-Feb-2016

49 views

Category:

Documents


0 download

DESCRIPTION

AWT . AWT is a java API that facilitate development of GUI based Application in java. API :- represents set of classes & interface that are design to be used in development of specific kind of application. AWT API is defined in java.awt & its subpackages. Component. Check Box. Normal - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: AWT

AWT AWT is a java API that facilitate development of GUI

based Application in java. API :- represents set of classes & interface that are design to be used in development of specific kind

of application.AWT API is defined in java.awt & its subpackages.

Page 2: AWT

Component

Panel

Container

frame

Window

Applet

Button

etc

CheckBox

Text Area

Text Field

Label

Normal Component

Menu Container

AWT Component Hierarchy

Page 3: AWT

• At the root of class hierarchy is an abstract class named component that defines the common features of all GUI Components .

• Container is a subclass of Component that adds the features of containership to a Component.

• Container is also a Abstract class.• It has two sub classes 1) Window 2) Panel • A Panel is a Rectangular region that does not

have a border & title Bar.• A window is a rectangular Region with border

& title bar.

Page 4: AWT

• Frame is a sub class of Window that has the additional functionalities of supporting menus.

No. 1

No. 2

Result

SUM

label

Button

Text Field

Frame

Page 5: AWT

Steps to design Frame Based Interface • 1) Create a sub class of Frame• 2) Create an Object of the subclass .• 3) create component objects to be displayed in the

frame .• 4) Add the components to the frame .• 5) set the size of the frame & make Its visible.• WORKING WITH LABLE ,BUTTON, & TEXT

FIELD :Lable: to describe other controls.• Public Lable();• Public Lable(String text );

Page 6: AWT

• Methods :• Public void setText(String text);• Public string getText();• Button(): are used to initiate actions.Public Button();Public Button(string label);Public String getLabel();Public void setLabel(String text);TextField:represents a single line text box.Public TextField();Public TextField(String text);Public TextField (int no of characters);

Page 7: AWT

• Methods: public String getText();• Public void setText(String Text);• Note: by force fully display a character • Public void setEchoChar(Char ch);• Public Char getEchoChar();• Public void setEditable(boolean editability);• COMMONLY USED METHODS OF CONTAINER

CLASS:• Add(): is used to add a component to the

container.• Public void add(Component c);

Page 8: AWT

• Public void setLayout(LayoutManagers manager);• It is used to specify the layout manager gor the

container.• A layout manager is responsible for

arranging ,positioning & sizing components in a container.

• setSize():- is used to specify the dimension of the container.

• Public void setsize(int width, int height);• COMMONLY USED METHOD OF FRAME CLASS:-• Settitle():- is used to display text on the title bar of a

frame.• Public void settitle(String Text);

Page 9: AWT

• getTitle():- public String getTitle();• setVisible():- public void setVisible(boolean

visiblity);• Note: by default off.• Pgm.• In GUI applications control of execution is

determine by the user interaction with the application .

• In GUI application . Concepts of events is used to response to user interaction . An event represents change of state of a component . It may be called by user interaction or by the application it self.

Page 10: AWT

• An AWT each change of state does represented by an object for which various event classes are defined in java.awt.event package. Most commonly used among these classes are

• 1) ActionEvent• 2) FocusEvent• 3) ItemEvent• 4) MouseEvent• 5) TextEvent• 6)KeyEvent.• 7) WindowEvent.

Page 11: AWT

• An ActionEvent is generated when the Button is click, a menu Item is selected, a list item is double clicked.

• FocusEvent :Occurs when a component gains or losses focus.

• ItemEvent: Occurs when a check box radio button or list item is selected.

• KeyEvent:occurs when a key is pressed, released & type.

• MouseEvent:Occurs when mouse is pressed, released ,clicked,moved,drag,events or exit component.

• Text event: occurs when the value of a text Field or TextArea is changed.

Page 12: AWT

• WindowEvent: occurs when a window is open ,closed,activated,deactivated,minimised&maximize.

• HANDLING EVENT IN AN APPLICATION:• In java deligation event model is used to handle

event in a GUI application . In Delegation event model they are two participant .

• 1)Source 2) listiner• 1) Source is a component whose change of state

triggers an event.• 2) A listener is an object that is intrusted in a

change of state i.e. it wants to do something when

Page 13: AWT

• A particular change of states occurs.• All GUI components i.e.

Button,Lable,TextField,Frame,Panel,Applet,etc. can be source of event.

• Listener has to be define by the application developer. • A listener represents Application logic that is executed

when an event occurs. • In order to be a listener a class has to be define call

back methods require to handle the event & then object of the listener is registered with the source of the event. whenever the event occurs source does the following things

1) An object that represents event is created.

Page 14: AWT

• 2) notification is send to all registered listener by invoking their call back methods.

• When a source invokes a call back methods and a listener object it provides the reference of event object as argument in the method.

Page 15: AWT

• Listener class

Call back Method(){

Statements to be excecuted in case of

event }

The registered listener object & reference event obj is provided to listener

Listener Object

Event Object

Sorce Object

A user interects with sorce & triggers event2.2 1.1

2.1

2.0

2.3

1.0

Page 16: AWT

• 1.0 Listener Object is created • 1.1 listener object is registered with the source .• 2.0 A user interacts with source and triggers

event .• 2.1 Event object is created from the source .• 2.2 call back method is invoke on.• 2.3 call back method is executed

Page 17: AWT

Call back• A call back method is a method that provided by API

provider implemented by the application developer and invoke by the API provider .

• To specify the call back method for different event listener interfaces are define java.awt.event package.

• 1) ActionListener • 2) FocusListener • 3) ItemListener• 4)KeyListener• 5)MouseListener• 6) MouseMotionListener • 7) TextListener • 8)WindowListener

Page 18: AWT

• ActionListener :- it is a interface provide call back method for handling action event. It contains one method :

• Public void actionPerformed(ActionEvent e);• General signature of the method to register a

listener object with source .• Public void addTypeListener(TypeListner obj);• Actual method:• Public void addActionlistener(ActionListener obj);• Public void addWindowListener(Window Listener

obj);• Etc……

Page 19: AWT

• Pgm

No. 1

No.2

Result

DivAdd Sub Mul

Page 20: AWT

• Methods of Action of Event class :to identify source of event .

• 1)getSource(): returns a refrence of the source object.

• Syntax:public object getSource();• 2) getActionCommand(): returns a string that

represents the label of the source button.• Syntax: public String getActionCommand();• Pgm•

Page 21: AWT

Layout Managers

• 1) Flow Layout: this layout manager arranging the component in left to right ,top to bottom fashion.

H Gap

V Gap

Page 22: AWT

• Constructor :-• Public FlowLayout();• Public FlowLayout(Int Alignment);• Public FlowLayout(int Alignment, int H gap,int V

gap);• To specify the value of Alignment static final

constant are defind in FlowLayout class .• flowLayout LEFT.• FlowLayout RIGHT• FlowLayout CENTER• Bydefault value of horizontal gap & vertical gap is

5 pixels.

Page 23: AWT

• Note: This is the default Layout manager for panel & its subclasses.

• GRIDLAYOUT: this Layout manager divide the container into specified rows & columns .each cell can contain the single component ,size of the component is change according the size of the cell.

Page 24: AWT

• Public GridLayout(int row, int column)• Public grid layout(int row,int column, int Hgap,int

Vgap);• E.g. GridLayout layout=new GridLayout(3,4);• frm.setLayout(layout);• BORDERLAYOUT: this layout manager divides the

container into 5 region each of the region can contain a single component.

Page 25: AWT

CENTER

NORTH

EAST

SOUTH

WEST

Page 26: AWT

• Public BorderLayout();• Note: 1)by default an item is added to the center

region of the container.• 2) to Add a component to a specific region

following overloaded method is used .• Public void add(component c, int region);• 3) this is the default layout manager for window

& its subclasses.

Page 27: AWT

CARDLAYOUT• This layout manager is used to arrange containers in the

form of deck of cards.• Public CardLayout();• Method:- first:- is used to show the first card from the

deck.• Public void first(container deck);• Public void last(container deck);• Public void prevoius(container deck);• Public void next(container deck);• At random calling a card:• Public void show(container deck,String card name);

Page 28: AWT

. Flow

41 2 3

LayoutManagerDemo

Grid Flow . Grid

121110

987

654

321

Page 29: AWT

LayoutManagerDemo

. Flow Grid

Deck

Panel

Card 1 which have

button for Flow Lay

out

Card 2 which have

Button for Grid layout

Panel

PanelAdd to center region

Page 30: AWT

• Java.awt.Checkbox & CheckBoxGroup classes are used to create Radio buttons.

• Constructor: public Checkbox();• Public CheckBox(String txet);• Public CheckBox(string text, boolean state);• Create Radio Buttons• Public checkbox(String Text, CheckBoxGroup,

Boolean state);public CheckBox(StringText, boolean state, CheckBoxGroup group);

• METHODS: public boolean getState();• Public void setState(boolean state);

CreatecheckB

ox

Page 31: AWT

• When a checkbox or radio button selected ItemEvent is generated.

• ItemListener Interface provide call back method to handle this event .this interface contains following method :-

• Public void itemStateChanged(ItemEvent e);• WINDOWEVENT: inorder to close a frame when

the close button is clicked ,windowClosing() of the WindowListener interface has to be define.

• WindowListener interface provide following method:

Page 32: AWT

• 1) public void windowOpened(WindowEvent e);• 2) public void windowClosing(WindowEvent e) ;• 3) public void windowClosed(WindowEvent e);• 4) public void windowActivated(WindowEvent e);• public void windowDeActivated(WindowEvent e);• public void windowIconified(WindowsEvent e);• public void windowDeiconified(WindowsEvent

e);

Page 33: AWT

Disadvantages of AWT• Major disadvantage of AWT is that AWT are

plateformdependent . i.e. look & feel of AWT component differ from one plateform to another .

Page 34: AWT

VB APP.GUI API

Common features of All GUI API

GUI API

GUI API

LINUXWindow

Other GUI Based O/SAll

abstract class

named toolkit is defined.

Page 35: AWT

Abstract Class

Toolkit

Implementation of toolkit class for windows

platform

Implementation for Linux

Plateform

For other GUI Based

plateform .

Page 36: AWT

Abstract method that are used by application

developers to write GUI Code

Method of Toolkit class are defined each method

uses JNI to Invoke Function of Native O/S

GUI library

Linux :Set Back Color();

For Example:in Window

Change Back color();

Native O/S GUI API

Plateform Specific

Implementation

Toolkit

Page 37: AWT

• Public void setBackGround(Int c);

• Public void setBackGround(int c)• {• changeBackColor(c);• }

Page 38: AWT

Swing• It is the extension of AWT that removes the draw

Backs of AWT.• Difference AWT & Swing:• 1) AWT component are platform dependent where

as Swing platform are not i.e. look & feel of Swing Components remains same across all platform.

• 2) Swing Component supports richer functionality as compared to AWT Components.

• 3) Swing Component support multiple look & feels.

Page 39: AWT

• 4) Swing components are light weight component where as AWT components havy weight component.

• 5) Swing Component are based on model view Controller (MVC) Architure

• A GUI component as the following functionality .• GUI component :• 1) providing presentation logic i.e. generating

view • 2) maintaining state or data .• 3) Monitoring user interaction & providing

response.

Page 40: AWT

• Model view controller Architure is a model based on division of labor concept . That separate distinct functionality of a GUI Component into three distinct loosely connected models named Model View Controller.

• VIEW

Responsible for providing presentation logic

CONTROLLER Monitors users interaction & interface

between model & view

MODEL Represents state of a Component.

Page 41: AWT

• 1.1 A Button is created in an Application.

Model gethers /obtaines data

required to draw a button

Controller

OK

View : presentation logic is applied on the data end a view is generated for the

end user .

1.22.4

1.3 2.0

2.1 2.12.2

2.3

2.5

Page 42: AWT

• 1.1 A Button is created in an application .• 1.2 obtain data from the model• 1.3 presentation logic is applied on the data end

a view is generated for end user. • 2.0 user interacts with the generated view . • 2.1 user interaction is identified by the

controller. • 2.2 model is modified about change of state.• 2.3 notify view to update interface .• 2.4 Obtains updated states • 2.5 updated interface.

Page 43: AWT

• SwingFrame is not platform independent i.e. it is still dependent of native o/s for its look & feel to give a consistent look & feel across all platform concept of light weight containers are used in a swing frame.

Root Pane

LayredPane

ContentPaneGlassPa

ne

Page 44: AWT

• RootPane: it is a light weight that contains all the panes that are added to a swing Frame as a deck.

• LayerdPane: it is a light weight container that is used to provide depth to the contents of swing frame.

• Content Pane: it is a light weight container that is responsible to holding to content for a swing frame.

• Glass pane: it is responsible for monitoring event or user interaction as well as supports drag & drop functionality .

Page 45: AWT

• getContentPane():- method of JFrame class ,it is used to obtain the reference of content pane of a frame.

• Public container getContentPane();• If closed button of a swing Frame is clicked then

Frame is made invisible to close the application by clicking close Button of a swing Frame

• setDefaultCloseOperation(int Operation);• Method of JFrame class is used. • To specify the operation following static final

constant is defined in Jframe class • Jframe: EXIT_ON_CLOSE.

Page 46: AWT

• Jlist: it is a class represents a list Box.• Public Jlist();(blank list )• Public Jlist(Object item[]);• Methods:• setSelectionModel():- is used to specify the

mode of selection of elements .• Syntax : public void setSelectionMode(int mode);• To specify the mode following static final

constant are defind in • Javax.swing.ListSelectionModel.

Page 47: AWT

• ListSelectionModel.SINGLE.SELECTION• listSelectionModel.SINGLE.INTERVAL-

SELECTION(with Shift)• listSelectionmode.MULTIPLE.INTERVAL.SELECTION(

With control)• Note: default is Multiple Interval Selection .• setVisibleRowCount():is used tospecify number of

visible operation. • Public void setVisible row count(int no of

operation);• getSelectedValue();• Returns the reference of selected object.

Page 48: AWT

• Public object[] getSelectedValues();• Public int getSelectedIndex();• Public int[] getSelectedIndex();• setListData(); is used to add items to list box at

runtime.• Public void setlistData(object [] items);• Javax.swing.event.ListSelectionEvent: is

generated when items are selected in Jlist component.

• Javax.swing.event.ListSelectionListener Interface provides call back method to handle this event.

Page 49: AWT

• Public void valueChanged(ListSelectionEvent e);

Items

Selected Items

Page 50: AWT

• setBounds(): it is of a component class , is used to specify the size & position of a component.

• Public void setBounds(int left,int top, int width, int height);

• Note: setLayout: layout manager must be set “null”befor using setBounds method.

lefttop

height

width

0,0 Container

Page 51: AWT

• Jtable: Jtable Component is used to displa data in a tabular formate .

• Contructor: public Jtable();• Public Jtable(object [][] data, object [] header);

Name salary

A

JobHeader

dataB

C

x

y

z 2000

200

200

Page 52: AWT

• Methods: getValueAt(): is used to find out the value of a cell.public object getValueAt(int r(row),int c(column));

• setValueAt():-it is used to change the value of cell.• Public void setValueAt(object value, int r,int c);• getSelectedrow():- returns the index of selected row

.• Public int getSelectedRow();• Public int getSelectedColumn(); • Public void setCellSelectionEnabled(boolen flag);• Javax.swing.event.listSelection event is generated

when a selection is made in a table .

Page 53: AWT

• Jtable component does not generate list Selection event own its on , rather it deligate responsibility of generating selection event on its behalf to a list selection of model object.

JTable

ListSelectionModel

Listener

getSele

ctionM

odel();

addListSelecti

onListener();

addListSelectionItem();

Page 54: AWT

• getSelectionModel(); method of Jtable class is used to obtain the return of the selection model object for the table

• Public list SelectionModel();• Public getSelectionModel();• JOPTIONPANE:class provides static methods to

display message dialogue box, input dialogueBox & confermationDialougeBox.

• Methods :• Public static void showmessageDialouge(container

parent, String no);

Page 55: AWT

• Public static string showinputDialoge(Cotainer parent, String number);

• Public int showConfermationdialouge(container parent string number);

• Show confermation dialouge method returns an integer that represents the code of the option button selected by the user.

• To find out selected option following static final constant defind

• 1) JOptionPane.YES_OPTION• 2)JOptionPane.NO_OPTION

Page 56: AWT

• 3)JOptionPane.OK_OpTION• 4) JOptionPane.CANCEL_OPTION.

text

InputMessage

Confirm

Page 57: AWT

JFileChooser Class • It is a class provides methods to display file open &

save as dialogue boxes.• Public int showOpenDialouge(Container parent);• Public int showSaveDialouge(container parent );• File open & save dialouge boxes does not open or

save file rather the provide a interface to the user to selection name, location of the file. The information of the file is provided by the JFileChooser object on the form of a file object. Java.io.File ,this object can be used to obtain all the information about the file such as its name ,path, attributes, etc…

Page 58: AWT

• getSelectedFile():- method of JFileChooser class returns a file object representing the information of selected file.

• COMONLY USED METHOD OF FILE CLASS:• public string getName();• Public string getPath();• WORKING WITH MANUES:• JMenuBar• Jmenu• JMenuItem provides support of menues in an

application .

Page 59: AWT

fopen1.1

File

1.4

1.5

1.0

1.2

1.3

1.6

Page 60: AWT

• 1.0 showOpenDialogue();• 1.1 FileOpenDialogueBox is display();• 1.2 user select a file • 1.3 user selection is identified by filechooser

object and this information is stored in a file object .

• 1.4 object(information of identified file is save into a file object)

• 1.5 getSelectedField(); method is invoked to obtain the file object.

• 1.6 reference of file object is returned.

Page 61: AWT

• JMenuBar:is a container of menues.• Jmenu. Is a collection of Jmenu items &

JMenuItmes represents an option in a menu.• Steps : to add menues to a frame.• 1) create a menubar object.• 2)create menuobject • 3) create menuitemobject for each menu• 4) add menu item object to their respective menu

objects.• 5) add the menu object to the menubar• 6) set the menubar on the frame.