Event Handling Event-Delegation Model: Setactioncommnand ("Add")
Event Handling Event-Delegation Model: Setactioncommnand ("Add")
Event-Delegation Model
In Java, events are objects. The java.awt.event. package defines a rich hierarchy of
event types. Through this package, instances of the various event classes are
constructed when users are GUI components. It is up to the programmer to decide
how to handle the generated event. For example, when a user clicks a button, the
system constructs an instance of class ActionEvent, in which it stores details about the
event (when, where, and so on). At this point, the programmer has three options:
// SelfButton.java
import java.awt.*;
import java.awt.event.*;
// ButtonDelegateTest.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Example 2.2;
import java.awt.*;
import java.awt.event.*;
TestEvents (){
tagFrame = new JFrame();
TagFrame.add(btn1);
TagFrame.add(btn2);
TagFrame.add(btn3);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
tagFrame.setVisible(true);
}
Interface MouseListener
public interface MouseListener
extends EventListener
The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component. (To track
mouse moves and mouse drags, use the MouseMotionListener.)
Method Summary
void mouseClicked(MouseEvent e)
Invoked when the mouse has been clicked on a component.
void mouseEntered(MouseEvent e)
Invoked when the mouse enters a component.
void mouseExited(MouseEvent e)
Invoked when the mouse exits a component.
void mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component.
void mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a component.
Method Detail
mouseClicked
public void mouseClicked(MouseEvent e)
Invoked when the mouse has been clicked on a component.
mousePressed
public void mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component.
mouseReleased
public void mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a component.
mouseEntered
public void mouseEntered(MouseEvent e)
Invoked when the mouse enters a component.
mouseExited
public void mouseExited(MouseEvent e)
Invoked when the mouse exits a component.
Interface ActionListener
public interface ActionListener
extends EventListener
The listener interface for receiving action events. The class that is interested in processing an action event implements this
interface, and the object created with that class is registered with a component, using the component's addActionListener method.
When the action event occurs, that object's actionPerformed method is invoked.
Method Summary
void actionPerformed(ActionEvent e)
Invoked when an action occurs.
Method Detail
actionPerformed
public void actionPerformed(ActionEvent e)
Invoked when an action occurs.
Interface TextListener
public interface TextListener
extends EventListener
The listener interface for receiving text events. The class that is interested in processing a text event implements this interface. The
object created with that class is then registered with a component using the component's addTextListener method. When the
component's text changes, the listener object's textValueChanged method is invoked.
Method Summary
void textValueChanged(TextEvent e)
Invoked when the value of the text has changed.
Method Detail
textValueChanged
public void textValueChanged(TextEvent e)
Invoked when the value of the text has changed. The code written for this method performs the operations that need to
occur when text changes.