Java GUI’s are event driven, meaning they generate events when the user interacts with the...

15
Java GUI’s are event driven , meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse button, hitting ‘Enter’ in a text field, clicking a button …. you get the idea When an event occurs, an event object is created which stores information about that event. This event object may be utilized in the case that the program wishes to respond to the event.

Transcript of Java GUI’s are event driven, meaning they generate events when the user interacts with the...

Page 1: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

Java GUI’s are event driven, meaning they generate events when the user interacts with the program.

Typical events are moving the mouse, clicking a mouse button, hitting ‘Enter’ in a text field, clicking a button …. you get the idea

When an event occurs, an event object is created which stores information about that event.

This event object may be utilized in the case that the program wishes to respond to the event.

Page 2: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

The Event Class

The class from which the event object is created depends upon the type of event which has occurred.

For example:An object is created using the MouseEvent class if

the event involved the mouse.

An object is created using the ActionEvent class if a button is pressed, or textfield data has been entered.

*these event classes are provided by java.awt.event package.

Page 3: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

The Event Source

The event source is the visual component with which the user has interacted . The event source creates the event object when an event has occurred.

For example:

an applet is the event source of a mouse click

a button is the event source of a button click

Page 4: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

The Event Listener

An event listener is an object that is prepared to ‘handle’ the event, if it is notified that the event has occurred.

The event listener object receives an event object when it is notified of the event, and responds to the event using the information provided in the event object.

Page 5: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

How does it work?

Each Java event source keeps an internal list of listeners .

If an event occurs, the event source notifies all the listener objects on the list. Any listener that is ‘listening’ for this particular event may then respond.

What does the programmer do to make a GUI respond to events?

The programmer must:

* provide a class from which a listener object can

be created (a listener class)

* create and register a listener object with an event source

Page 6: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

Creating a ‘Listener’ classA class which is to be used for creating a mouse listener object must implement the

MouseListener interface. *also in java.awt.event package

public interface MouseListener{void mousePressed(MouseEvent event); //Called when a mouse button has been pressed

void mouseReleased(MouseEvent event);  //Called when a mouse button has been released

void mouseClicked(MouseEvent event); //Called when the mouse has been clicked

void mouseEntered(MouseEvent event); //Called when the mouse enters a component

void mouseExited(MouseEvent event); //Called when the mouse exits a component

}

Page 7: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

MouseSpy.java import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

// This listener simply prints out the listener method name

// and mouse position for each mouse event

public class MouseSpy implements MouseListener {

public void mousePressed(MouseEvent event) {

System.out.println("Mouse pressed. x = "

+ event.getX() + " y = " + event.getY());

}

public void mouseReleased(MouseEvent event) {

System.out.println("Mouse released. x = "

+ event.getX() + " y = " + event.getY());

}

Page 8: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

public void mouseClicked(MouseEvent event) {

System.out.println("Mouse clicked. x = "

+ event.getX() + " y = " + event.getY());

}

public void mouseEntered(MouseEvent event) {

System.out.println("Mouse entered. x = "

+ event.getX() + " y = " + event.getY());

}

public void mouseExited(MouseEvent event) {

System.out.println("Mouse exited. x = "

+ event.getX() + " y = " + event.getY());

}

}

Page 9: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

Creating a Listener Object and ‘Registering’ It

* a listener object is created just like any other – by calling a class constructor.

* all event sources are required to provide methods for ‘registering’ event listener objects. We must just call that method.

Page 10: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

MouseSpyApplet.java import java.applet.Applet;

// This applet installs a mouse spy, which responds to the

// five mouse events.

public class MouseSpyApplet extends Applet {

public MouseSpyApplet() {

MouseSpy listener = new MouseSpy();

addMouseListener(listener); //adds object listener to list of registered

// event listeners for this event source

}

}

Page 11: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

A mouse listener class does not need to be in it’s own file … it can be a private class .

public class MouseSpyApplet extends Applet {

public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); }

private class MouseSpy implements MouseListener { public void mousePressed(MouseEvent event) { //code } public void mouseReleased(MouseEvent event) { //code

} public void mouseClicked(MouseEvent event) { }public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { }

}}

Page 12: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

A mouse listener class does not need to be in it’s own file … it can be an inner class.

public class MouseSpyApplet extends Applet {

public MouseSpyApplet() { class MouseSpy implements MouseListener { public void mousePressed(MouseEvent event) { //code } public void mouseReleased(MouseEvent event) { //code

} public void mouseClicked(MouseEvent event) { }public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { }

} MouseSpy listener = new MouseSpy(); addMouseListener(listener); } }

Page 13: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

The constructor can still perform other tasks as well.public class AnApplet extends Applet {

public AnApplet() { // initialize instance variables ********the code********** // get input from user *************the code ********** class MouseSpy implements MouseListener { // listener methods

} MouseSpy listener = new MouseSpy(); addMouseListener(listener); }

public void paint (Graphics g) { ******* the code************** } }

Page 14: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

Code an applet which draws a rectangle. When the mouse is clicked the rectangle should be redrawn at the mouse position.

(MoveRec.java on website)

Code an applet which displays two rectangles, one blue and one red, and also draws a blue ellipse. When the mouse clicks on either of the two rectangles, the rectangle color should change to the same color as the box.

(ColorEllipse.java on website)

The MouseAdapter class can make coding the listener classes less tedious by eliminating the need for empty methods. Just extend MouseAdapter rather than implementing MouseListener. Since a MouseAdapter implements MouseListener, any class that extends it also implements MouseListener !!

Page 15: Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.

Applets can also ‘listen’ for any mouse motion. The listener object that does this implements the MouseMotionListener interface.

Check it out, and code an applet which draws a filled square, such that when the mouse is placed on the square and the button clicked, the mouse drags the square with it.

(MouseDrag.java on website)