Event and GUI programming - PowerPoint PPT Presentation

event and gui programming n.
Skip this Video
Loading SlideShow in 5 Seconds..
Event and GUI programming PowerPoint Presentation
Download Presentation
Event and GUI programming

play fullscreen
1 / 103
Event and GUI programming
Download Presentation
Download Presentation

Event and GUI programming

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Event and GUI programming

  2. Event Handling in Java • An object resides in a particular state until it is made to transit to other state. This transition occurs due to an event. • We want an object to invoke a function when an action is generated, e.g. pressing a key on the keyboard, moving the mouse, clicking on button, etc. • The object which generates event, i.e. the source of the event is known as the event generator. • If a button is pressed for an operation, the button is the event generator. • The object that is responsible for performing the task when the event occurs is called the event handler. • There may be more than one event handlers for one single event generated; each event handler responsible for doing a unique activity on account of that particular event

  3. How handlers know that a particular event has occurred so that they can perform their activity? • For this, there is a registration process. This registration involves the event handler simply asking the event generator to inform about the occurrence of an event. • By registering, the event generator is able to keep track of all the registered event handlers.

  4. Event Delegation Model • The Event Model Is Based on the Concept of an ‘Event Source’ and ‘Event Listeners’. • Any Object That Is Interested in Receiving Messages (or Events ) Is Called an Event Listener. • Any Object That Generates These Messages (or Events ) Is Called an Event Source.

  5. The Event Source Object Maintains a List of Listeners Who Are Interested in Receiving Events That It Produces. • The Event Source Object Provides Methods That Allow the Listeners to Add Themselves ( ‘Register’ ) or Remove Themselves From This List of ‘Interested’ Objects. • When the Event Source Object Generates an Event, or When a User Input Event Occurs on the Event Source Object, the Event Source Object Notifies All the Listeners That the Event Has Occurred.

  6. Java.Awt.Event Description • The java.awt.AWTEvent class is the root class for all AWT Events. • This package java.awt.AWTEvent includes the definition of events classes, listeners interfaces, and adapters classes, which from the basics for event handling.

  7. Event Classes • Java has a predefined hierarchy of event-related classes, at the root of which is EventObject. • It is actually a member of java.utilpackage. This class has constructors and methods defined as its members. • One such constructor is EventObject(Object src_obj) • where, src_objis the object, which generates the event. • EventObject has methods like getSource() and toString(). • getSource() – returns the source of the event • toString() – returns the string equivalent of the event

  8. Event Classes

  9. KeyEvent Class • Syntax public class KeyEvent extends InputEvent • This low-level event is generated by a component object(such as text field, Applet, Frame) when a key is pressed, released, or typed. • The event is passed to a KeyListenerobject which is registered to receive the event notification using the component’s addKeyListenermethod

  10. Constructor

  11. Methods in KeyEvent

  12. MouseEvent • It is an event which indicates that a mouse action occurred in a component. • A mouse action occurs in a particular component if and only if the mouse cursor is over the defined part of the component’s bounds when the action happens. • public class MouseEventextends InputEvent • There are eight types of mouse events defined in the MouseEvent class. • The MouseEvent class defines them as integer constants to identify each of these events.

  13. types of mouse events

  14. Constructor

  15. Methods of MouseEvent

  16. Source of Events • Button • Choice • Menu Item • Check box • List • Window • Scroll bar • Text components

  17. Source of Events • Event Listeners are created by implementing one or more interfaces defined by the java.awt.eventpackage. • Whenever a source generates an event, it basically invokes the appropriate method defined in the listener interface. • The method has a event object passed as an argument to it.

  18. Listeners

  19. KeyListener • This interface has three methods defined within it. • void keyPressed(KeyEvent e) – invoked when a key is pressed • void keyReleased(KeyEvent e) - invoked when a key is released • void keyTyped(KeyEvent e) - invoked when a character is typed

  20. MouseListener • The interface has five methods, having the signatures as follows: • void mouseClicked(MouseEvent e) • void mouseEntered(MouseEvent e) • void mousePressed(MouseEvent e) • void mouseReleased(MouseEvent e) • void mouseExited(MouseEvent e)

  21. MouseMotionListener • The interface has two methods having the signatures, • void mouseMoved(MouseEvent e) • void mouseDragged(MouseEvent e) • mouseMoved() is invoked when the mouse is moved from one place to another and mouseDragged() is used when the mouse is dragged.

  22. MouseWheelListener

  23. ItemListener

  24. ActionListener

  25. TextListener

  26. Example

  27. Output

  28. AWT • AWT provides graphical user interface (GUI) components that are used in all java applet and application • AWT contains classes that can be extended and their properties can be inherited

  29. Java.awt package • The package java.awt contain all classes used for creating graphical user interfaces, painting graphics, images, color, and fonts. • A user interface element such as a button or a textbox is called a component • A Component class is the super class of all AWT components. • These components fire events when users interact with these components, e.g. when a user click on a button. These events are handled by event handling classes. i.e. AWTEvent and its subclasses. • A container is one which contains components and other containers. A container has a layout managers that determines the visual placement of components in the container.

  30. Component and Containers • A graphical user interface is developed with the help of graphical elements like buttons, scrollbar etc. • These elements are called components. These components are generally the source of events that allow the user to interact with the program. • Componenets are added to a window using the add() method • Component add(Component ComObj) • ComObj is the object of the Component, which is to be added • This method returns the reference to the ComObj. • If you wish to remove a Component from a window, use remove() method • void remove(Component ComObj)2 • Components can not exist alone; they are found within containers. The layout of the components are contained and controlled within containers.

  31. Hierarchy of classes in AWT

  32. Button • The Button class belongs to java.awt package • public class Button extends Component implements Accessible • This class creates a button which when pushed or pressed generates an event. • The two constructors belonging to this Button class are: • Button() throws HeadlessException • Button(String str)throws HeadLessException; • To create a button • Button buttonName = new Button(Str); • ‘buttonname’ is the name you give to the button object and ‘Str’ is the text you want to appear on the button. • Once the object for Button is created, it needs to be added to the applet or any other container using • add(buttonname); • void setLabel(String str) for changing the button’s label • String getLabel() for getting the Buttons label’s text

  33. Syntax : - Button buttonname=new Button(Str); Where buttonnameis the name of the button object and stris the text we want to appear on the button • Once the object for Button is created, it needs to be added to the applet or any other containers. • The syntax add(buttonname)

  34. Button Example /*<applet code=ButtonClass.class width=400 height=150></applet>*/ import java.applet.*; import java.awt.*; import java.awt.event.*; public class ButtonClass extends Applet implements ActionListener{ Button red, white, blue; Label hit; public void init(){ red = new Button(“Red”); white = new Button(“white”); blue = new Button(“blue”); hit = new Label(“Hit a Button to change the screen color”); add(red); add(white); add(blue); add(hit);

  35. Button Example red.addActionListener(this); white.addActionListener(this); blue.addActionListener(this);} public void actionPerformed(ActionEventae){ String str = ae.getActionCommand(); if (str.equals(“Red”)) { setBackground(Color.red);} else if (str.equals(“white”)) { setBackground(Color.white);} else if (str.equals(“blue”)){ setBackground(Color.blue);} repaint();}}

  36. Output

  37. Label • Labels consist of a text string for display only and they never call an action method. • A Label can be justified LEFT, RIGHT, or CENTERED. • new Label(“This label is for demonstration.”, Label.RIGHT);

  38. Label Example /*<applet code=”LabelClass.java” width=350 height=100></applet>*/ import java.applet.*; import java.awt.*; public class LabelClass extends Applet { public void init(){ Label firstLabel = new Label(“Labels exist simply “); add(firstLabel); Label secLabel = new Label(“to place text on the screen”); add(secLabel); Label thirdLabel = new Label(“They can be aligned left, right or center.”); add(thirdLabel);}}

  39. CheckBox • Checkboxes are used as on-off or yes-no switches • if you click on an unchecked checkbox, it will get checked and vice versa. • Constructors of Checkbox • Checkbox() • Checkbox(String str) • Checkbox(String str, boolean on) • Checkbox(String str, CheckBoxGroupcbg, boolean on)

  40. Methods of Checkbox class

  41. Checkbox Example /*<applet code=CheckboxClass.class width=400 height=100></applet>*/ import java.applet.*; import java.awt.*; import java.awt.event.*; public class CheckboxClass extends Applet implements ActionListener { Button submit; Checkbox name1; Checkbox name2; Checkbox name3; public void init(){ name1 = new Checkbox (“Ram”,null,false); name2 = new Checkbox (“Ramesh”,null,false); name3 = new Checkbox (“Naresh”,null,false);

  42. Checkbox Example Font f = new Font (“Arial”,Font.ITALIC,14); submit = new Button(“SUBMIT”); add(name1); add(name2); add(name3); add(submit); submit.addActionListener(this); } public void actionPerformed(ActionEventae) { String str = ae.getActionCommand(); if (str.equals(“SUBMIT”)) repaint();} public void paint (Graphics g) { g.setFont(f); g.setColor(Color.blue); if (name1.getState()) g.drawString(“Ram”,50,60); if (name2.getState()) g.drawString(“Ramesh”,50,80); if (name3.getState()) g.drawString(“Naresh”,50,100); }}

  43. The Output

  44. RadioButton • Radio buttons, which are also called checkbox groups, are special kind of checkboxes, where within a particular group, only one box can be selected at a time Checkbox cb=new Checkbox(“mango”,fruits,false); • First argument is label, the second argument is the group of which it is a part of, and the third is the state, true or false, depending on whether the button is the selected or not

  45. Radio Button Example /*<applet code=”RadioDemo.class” width=300 height=200></applet>*/ import java.applet.*; import java.awt.*; import java.awt.event.*; public class RadioDemo extends Applet implements ItemListener{ Checkbox red, white, green; CheckboxGroupcbg; public void init(){ add(new Label(“The 4 radio buttons will change the screen color.”)); cbg = new CheckboxGroup(); red = new Checkbox(“Red”,cbg,false); white = new Checkbox(“White”,cbg,false); green = new Checkbox(“Green”,cbg,false); add(new Label(“Notice that you can only select one radio button.”)); add(new Label(“And selecting a radio button triggers an event”));

  46. Radio Button Example add(new Label(“that we use to change the screen color.”)); add(red); add(white); add(green); red.addItemListener(this); white.addItemListener(this); green.addItemListener(this); } public void itemStateChanged(ItemEventie){ String str = (String) ie.getItem(); if (str.equals(“Red”)) { setBackground(Color.red);} else if (str.equals(“White”)) { setBackground(Color.white);} else if (str.equals(“Green”)){ setBackground(Color.green);} repaint();}}