Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event-driven programming for GUI

Similar presentations


Presentation on theme: "Event-driven programming for GUI"— Presentation transcript:

1 Event-driven programming for GUI

2 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

3

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

5 Show Action Event

6 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, ActionEvent defines events such as pressing a button; WindowEvent defines events such as closing or opening a window;

7 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 getSource() method in the event class.

8 User actions, source objects and related events.
Generated Event Click a button JButton ActionEvent Press return on a text field JTextField Select an item JList ListSelectionEvent

9 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 events 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.

10 Event and corresponding Listener interface
Event Class Listener Interface Listener Methods (Handlers) ActionEvent ActionListener actionPerformed(ActionEvent) WindowEvent WindowListener windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) MouseEvent MouseListener mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseClicked(MouseEvent) mouseExited(MouseEvent) mouseEntered(MouseEvent)

11 Registering with source objects Implement event listener
For ShowActionEvent, listener is “this” object ShowActionEvent implements ActionListener //Register this object as the listener for //events generated by the two buttons button1.addActionListener(this); button2.addActionListener(this);

12 Handling events // implement the actionPerformed method // as specified in the // ActionListener interface public void actionPerformed(ActionEvent e) { msg.setText(e.getActionCommand()+ " is pressed"); }

13 ShowActionEvent- explained 1
The class has two buttons and one label. private JButton button1 = new JButton("Button1"); private JButton button2 = new JButton("Button2"); private JLabel msg = new JLabel(); It uses the default BorderLayoutManager. It places button1 at North, button2 at South and the label msg in the Center // Get the content pane of the frame Container container = getContentPane(); // Add buttons to the frame container.add(button1,BorderLayout.NORTH); container.add(button2,BorderLayout.SOUTH); container.add(msg,BorderLayout.CENTER);

14 ShowActionEvent- 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 button1.addActionListener(this); button2.addActionListener(this); The class of “this” object, i.e. ShowActionEvent, implements ActionListener.

15 public class ShowActionEvent extends JFrame
implements ActionListener { … … } ShowActionEvent implements the method in ActionListener, i.e. the actionPerformed() method. // implement the actionPerformed method as // specified in the ActionListener interface public void actionPerformed(ActionEvent e) { msg.setText(e.getActionCommand()+" is pressed");

16 ShowActionEvent- explained 3
ShowActionEvent implements the method in ActionListener, i.e. the actionPerformed() method. // implement the actionPerformed method as // specified in the ActionListener interface public void actionPerformed(ActionEvent e) { msg.setText(e.getActionCommand()+" is pressed"); }

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

18 A more complicated example
Write a java program to implement the simple calculator to produce a GUI as shown

19 Summary 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.


Download ppt "Event-driven programming for GUI"

Similar presentations


Ads by Google