1 / 19

Event-driven programming for GUI

Event-driven programming for GUI. 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

Download Presentation

Event-driven programming for GUI

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

  4. Show Action Event

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

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

  7. User actions, source objects and related events.

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

  9. Event and corresponding Listener interface

  10. Registering with source objectsImplement 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);

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

  12. ShowActionEvent- explained 1 • The class has two buttons and one label. private JButton button1 = new JButton("Button1"); private JButton button2 = new JButton("Button2"); private JLabelmsg = 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);

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

  14. 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"); • }

  15. 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"); • }

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

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

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

More Related