Upload
geoffrey-summers
View
230
Download
2
Tags:
Embed Size (px)
Citation preview
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
Panel
Container
frame
Window
Applet
Button
etc
CheckBox
Text Area
Text Field
Label
Normal Component
Menu Container
AWT Component Hierarchy
• 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.
• 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
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 );
• 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);
• 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);
• 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);
• 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.
• 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.
• 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.
• 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
• 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.
• 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.
• 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
• 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
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
• 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……
• 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•
Layout Managers
• 1) Flow Layout: this layout manager arranging the component in left to right ,top to bottom fashion.
H Gap
V Gap
• 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.
• 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.
•
• 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.
• 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.
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);
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
• 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
• 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:
• 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);
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 .
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.
Abstract Class
Toolkit
Implementation of toolkit class for windows
platform
Implementation for Linux
Plateform
For other GUI Based
plateform .
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
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.
• 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.
• 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.
• 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
• 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.
• 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
• 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 .
• 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.
• 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.
• 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.
• 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.
• 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
• 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
• 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 .
• 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();
• 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);
• 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
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…
• 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 .
• 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.
• 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.