Unit 12 Objectoriented programming Eventdriven programming for GUI
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa
The objectives of this unit are: • To explain the concept of event-driven programming for GUI applications • To understand event, event source, and event classes • To know how to register listener objects with source objects; and to implement listener interfaces • To understand how an event is handled
Java GUI programming is event-driven • In Java GUI components allow users to interact with them, which generate events. Actions can be taken to respond to the events. • Student activity 12. 1 – Download the Show. Action. Event progem. Compile and run Show. Action. Event.
Show. Action. Event
Events and event source • Events: mouse movements, mouse clicks, and keystrokes, or by the operating system, such as a timer. • Java has pre-defined classes to represent different kinds of events. Each kind of event is defined as a class. – For example, Action. Event defines events such as pressing a button; Window. Event defines events such as closing or opening a window;
Source object of an event • The object in which an event is generated is called the source object. • The two button objects are the source objects of the events generated when the buttons are pressed • To identify the source object of an event, we use the get. Source() method in the event class.
User actions, source objects and related events. User Action Click a button Press return on a text field Select an item Source Object Generated Event JButton Action. Event JText. Field Action. Event JList. Selection. Event
Listeners, listener interfaces and event handlers • Once an event is generated, it needs to be listened by an object: the listener. • The listener object receives information about these events and takes some actions to respond to these events, i. e. to handle the events. • The actions for handling the vents are defined in the methods specified in the relevant listener interface. • The listener object’s class must implement the corresponding event-listener interface. The listener object must register with the source object.
Event and corresponding Listener interface Event Class Listener Interface Listener Methods (Handlers) Action. Event Action. Listener action. Performed(Action. Event) Window. Event Window. Listener window. Closing(Window. Event) window. Opened(Window. Event) window. Iconified(Window. Event) window. Deiconified(Window. Event) window. Closed(Window. Event) window. Activated(Window. Event) window. Deactivated(Window. Event) Mouse. Event Mouse. Listener mouse. Pressed(Mouse. Event) mouse. Released(Mouse. Event) mouse. Clicked(Mouse. Event) mouse. Exited(Mouse. Event) mouse. Entered(Mouse. Event)
Registering with source objects Implement event listener • For Show. Action. Event, listener is “this” object • Show. Action. Event implements Action. Listener //Register this object as the listener for //events generated by the two buttons button 1. add. Action. Listener(this); button 2. add. Action. Listener(this);
Student activity 12. 2 • Explore the effect of registering listener. • Modify the Show. Action. Event example by removing the button 1. add. Action. Listener(this); button 2. add. Action. Listener(this); • Compile and run the program again. Now click on Button 1 and Button 2, observe what happens and try to explain why.
Handling events // implement the action. Performed method // as specified in the // Action. Listener interface public void action. Performed(Action. Event e) { msg. set. Text(e. get. Action. Command()+ " is pressed"); }
Show. Action. Event- explained 1 • The class has two buttons and one label. private JButton button 1 = new JButton("Button 1"); private JButton button 2 = new JButton("Button 2"); private JLabel msg = new JLabel(); • It uses the default Border. Layout. Manager. It places button 1 at North, button 2 at South and the label msg in the Center // Get the content pane of the frame Container container = get. Content. Pane(); // Add buttons to the frame container. add(button 1, Border. Layout. NORTH); container. add(button 2, Border. Layout. SOUTH); container. add(msg, Border. Layout. CENTER);
Show. Action. Event- explained 2 • “this” object is registered with the two buttons to listen to events generated by these two buttons. // Register this object as the listener for // events generated by the two buttons button 1. add. Action. Listener(this); button 2. add. Action. Listener(this); • The class of “this” object, i. e. Show. Action. Event, implements Action. Listener. public class Show. Action. Event extends JFrame implements Action. Listener { … … } • • Show. Action. Event implements the method in Action. Listener, i. e. the action. Performed() method. // implement the action. Performed method as // specified in the Action. Listener interface
Show. Action. Event- explained 3 • Show. Action. Event implements the method in Action. Listener, i. e. the action. Performed() method. // implement the action. Performed method as // specified in the Action. Listener interface public void action. Performed(Action. Event e) { msg. set. Text(e. get. Action. Command()+" is pressed"); }
Student activities 12. 3: • Write a java program that displays a frame. In the frame there is one button labeled “Red” and another button labeled “Blue”. When the “Red” button is clicked, the background of the frame is changed to red. When the “Blue” button is clicked, the background colour of the frame is changed to green.
A more complicated example • Write a java program to implement the simple calculator to produce a GUI as shown in Version 1. • See hint
Summery • understand the concepts of event, event source, and event classes • know how to register listener objects with source objects; and to implement listener interfaces • understand how an event is handled • be able to write simple Java event-driven GUI applications.
Form Editor in Netbeans
- Slides: 19