Top Banner
GUI and Event-Driven Programming Part 2
27

GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

GUI and Event-Driven Programming

Part 2

Page 2: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Event Handling

An action involving a GUI object, such as clicking a button, is called an event.

The mechanism to process events is called event handling.

The event-handling model of Java is based on the concept known as the delegation-based event model.

With this model, event handling is implemented by two types of objects:

– event source objects – event listener objects.

Page 3: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Event Sources and Listeners

An event sourceevent source object is a GUI object where an event occurs. An event source generatesgenerates events.

An event listenerevent listener object is an object that includes a methodmethod that gets executed in gets executed in response to the generated eventsresponse to the generated events. When an event is generated, the system notifies the relevant event listener objects.

Page 4: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Events and Event Listeners

There are many different kinds of events, but the most common one is an action event.

For the generated events to be processed, we must associate, or register, event listeners to the event sources.

If event sources have no registered listeners, the generated events are ignored.

Page 5: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Events and Event Listeners

An object that can be registered as an action listener must be an instance of a class that is declared specifically for the purpose. We call such classes action listener classes.

To associate an action listener to an action event source, we call the event source’s addActionListener method with the action listener as its argument.

Page 6: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Events and Event Listeners

A single listener can be associated to multiple event sources.

Likewise, multiple listeners can be associated to a single event source.

When an event source generates an event, the system checks for matching registered listeners.

– If there is no matching listener, the event is ignored.– If there is a matching listener, the system notifies the

listener by calling the listener’s corresponding method.

Page 7: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Action Listener Classes

In the case of action events, the method called is actionPerformed.

To ensure the programmer includes the necessary actionPerformed method in the action listener class, the class must be defined in a specific way. import java.awt.event.*;

public class ButtonHandler implements ActionListener {...}

Page 8: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

The ActionListener interface

ActionListener is an interface, not a class. Like a class, an interface is a reference data type,

but unlike a class, an interface includes only constants and abstract methods.

An abstract method has only the method header, or prototype; it has no method body.

A class that implements a Java interface must provide the method body to all the abstract methods defined in the interface.

Page 9: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

The ActionListener interface

By requiring an object we pass as an argument to the addActionListener method to be an instance of a class that implements the ActionListener interface, the system ensures that this object will include the necessary actionPerformed method.

Implementation of an interface is Java’s mechanism for providing a form of multiple inheritance

Page 10: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Example

Suppose we want to change the title of the frame, depending on which button is clicked

We can do this using the actionPerformed method. The method model is:public void actionPerformed(ActionEvent evt) { String buttonText = get the text of the event source; JFrame frame = the frame that contains this event source; frame.setTitle(“You clicked “ + buttonText);}

Page 11: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Handling Button Events

The first statement may be handled in one of two ways. The first way is via the getActionCommand method of

the action event object evt:String buttonText = evt.getActionCommand();

The second way is via the getSource method of the action event object evt.:JButton clickedButton = (JButton) evt.getSource();

String buttonText = clickedButton.getText();

Page 12: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Handling Button Events

A frame window contains nested layers of panes. The topmost pane is called the root pane.

To find the frame that contains the event source, we – get the root pane to which the event source belongs, then – get the frame that contains this root pane.

JRootPane rootPane = clickedButton.getRootPane();Frame frame = (JFrame) rootPane.getParent();

The frame can be the event listener of the GUI objects it contains.

cancelButton.addActionListener(this);okButton.addActionListener(this);

Page 13: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Swing Classes for Text Handling

The Swing GUI classes JLabel, JTextField, and JTextArea all deal with text. – A JLabel object displays uneditable text. – A JTextField object allows the user to enter a

single line of text. – A JTextArea object allows the user to enter

multiple lines of text. It can also be used for displaying multiple lines of uneditable text.

Page 14: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Text Fields and Action Events

An instance of JTextField generates action events when the object is active and the user presses the ENTER key.

The actionPerformed method must determine which of the event sources (for example, a button or a text field) generated the event.

The instanceof operator determines the class to which the event source belongs. The general model is thus:

if (event.getSource() instanceof JButton ){// event source is either cancelButton// or okButton...}else { // event source must be inputLine...}

Page 15: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Text Fields and Action Events

The getText method of JTextField may be used to retrieve the text that the user entered.public void actionPerformed(ActionEvent event) { if (event.getSource() instanceof JButton){ JButton clickedButton = (JButton) event.getSource(); String buttonText = clickedButton.getText(); setTitle(“You clicked ” + buttonText);

} else {// the event source is inputLine setTitle(“You entered ‘” + inputLine.getText() + “’”); }}

Page 16: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

JLabel Objects

A JLabel object is useful for displaying a label indicating the purpose of another object, such as a JTextField object.prompt = new JLabel(“Please enter your name”);

JLabel objects may also be used to display images. When a JLabel object is created, we can pass an

ImageIcon object instead of a string. To create the ImageIcon object, we must specify the

filename of the image.

Page 17: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

JLabels and Images

We declare the data memberprivate JLabel image;

then create it in the constructor aspublic TextFrame2 () {...

image = new JLabel(new ImageIcon(“cat.gif”);// assumes the .gif is/in the same directory as the programimage.setBounds(10, 20, 50, 50);contentPane.add(image);

...}

Page 18: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Example with JLabel, JButton and JTextField objects

Page 19: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

TextArea & Related Methods

A TextArea is like a TextField, but can accommodate more text

Methods include: setBorder(BorderFactory);

// creates a border around a JTextArea object setEditable(boolean);

// enables/disables changes to a JTextArea object The append(String) method allows us to add text to the text

area without replacing old content. Using the setText (String) method of JTextArea will replace

old content with new content.

Page 20: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Example frame with JTextArea object

Page 21: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Adding Scrollbars to a JTextArea

JScrollPane scrollText = new JScrollPane(textArea);

contentPane.add(scrollText);

Page 22: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Menus

The javax.swing package contains three useful menu-related classes: JMenuBar, JMenu, and JMenuItem.– MenuBar is a bar where the menus are placed.– Menu (such as File or Edit) is a single item in the

MenuBar.– MenuItem (such as Copy, Cut, or Paste) is an

individual menu choice in the Menu.

Page 23: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Menus

When a MenuItem is selected, it generates a menu action event.

We process menu selections by registering an action listener to menu items.

The following example will create the two menus shown and illustrate how to display and process the menu item selections.

Page 24: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Menus

We will follow this sequence of steps:1. Create a JMenuBar object and attach it to a

frame.

2. Create a JMenu object.

3. Create JMenuItem objects and add them to the JMenu object.

4. Attach the JMenu object to the JMenuBar object.

Page 25: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Menus

We create a fileMenu object asfileMenu = new Menu(“File”);

The argument to the Menu constructor is the name of the menu.

Menu items appear from the top in the order in which they were added to the menu.

Page 26: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Menus

To create and add a menu item New to fileMenu, we executeitem = new MenuItem(“New”);

item.addActionListener(this);

fileMenu.add(item);

And to add a horizontal line as a separator between menu items, we executefileMenu.addSeparator();

Page 27: GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Menus

We then attach the menus to a menu bar. We create a MenuBar object in the

constructor, call the frame’s setMenuBar method to attach it to the frame, and add the two Menu objects to the menu bar.MenuBar menuBar = new MenuBar();

setMenuBar(menuBar);

menuBar.add(fileMenu);

menuBar.add(editMenu);