1 / 61

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. Component. Check Box. Normal

sahara
Download Presentation

AWT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. Component Check Box Normal Component Menu Container Container Text Area Text Field Panel Window Label Button Applet frame etc AWT Component Hierarchy

  3. 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.

  4. Frame is a sub class of Window that has the additional functionalities of supporting menus. Frame No. 1 label No. 2 Text Field Result SUM Button

  5. 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 );

  6. 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);

  7. 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(booleaneditability); • COMMONLY USED METHODS OF CONTAINER CLASS: • Add(): is used to add a component to the container. • Public void add(Component c);

  8. 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);

  9. getTitle():- public String getTitle(); • setVisible():- public void setVisible(booleanvisiblity); • 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.

  10. 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.

  11. 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.

  12. 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

  13. 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 • An object that represents event is created.

  14. 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.

  15. Sorce Object Call back Method() { Statements to be excecuted in case of event } 2.2 • Listener class 1.1 2.1 1.0 Listener Object 2.0 2.3 Event Object A user interects with sorce & triggers event The registered listener object & reference event obj is provided to listener

  16. 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

  17. 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

  18. 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(TypeListnerobj); • Actual method: • Public void addActionlistener(ActionListenerobj); • Public void addWindowListener(Window Listener obj); • Etc……

  19. Pgm No. 1 No.2 Result Add Sub Mul Div

  20. 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

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

  22. 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.

  23. 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.

  24. Public GridLayout(int row, int column) • Public grid layout(introw,int column, intHgap,intVgap); • 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.

  25. CENTER NORTH EAST WEST SOUTH

  26. 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.

  27. 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);

  28. LayoutManagerDemo . Flow Grid . Flow Grid 1 2 3 4 2 1 3 4 5 6 8 9 7 12 10 11

  29. LayoutManagerDemo Card 2 which have Button for Grid layout Panel Card 1 which have button for Flow Lay out . Flow Grid Deck Add to center region Panel Panel

  30. Create checkBox • 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 booleangetState(); • Public void setState(boolean state);

  31. 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:

  32. 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);

  33. 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 .

  34. Window LINUX VB APP. GUI API GUI API All abstract class named toolkit is defined. Other GUI Based O/S Common features of All GUI API GUI API

  35. Toolkit Abstract Class Implementation of toolkit class for windows platform Implementation for Linux Plateform For other GUI Based plateform .

  36. Toolkit For Example:in Window Change Back color(); Abstract method that are used by application developers to write GUI Code Plateform Specific Implementation Linux : Set Back Color(); Method of Toolkit class are defined each method uses JNI to Invoke Function of Native O/S GUI library Native O/S GUI API

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

  38. 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.

  39. 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.

  40. 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.

  41. 2.2 Model gethers /obtaines data required to draw a button • 1.1 A Button is created in an Application. Controller 2.3 2.4 1.2 2.1 2.1 1.3 View : presentation logic is applied on the data end a view is generated for the end user . OK 2.0 2.5

  42. 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.

  43. 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 GlassPane ContentPane LayredPane

  44. 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 .

  45. 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.

  46. 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.

  47. 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.

  48. Public object[] getSelectedValues(); • Public intgetSelectedIndex(); • 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.

  49. Public void valueChanged(ListSelectionEvent e); Items Selected Items

  50. setBounds(): it is of a component class , is used to specify the size & position of a component. • Public void setBounds(intleft,int top, int width, int height); • Note: setLayout: layout manager must be set “null”befor using setBounds method. 0,0 Container top left height width

More Related