Documente Academic
Documente Profesional
Documente Cultură
Event Handling
Events
In the delegation model, an event is an object that
describes a state change in a source.
Among other causes, an event can be generated as
a consequence of a person interacting with the
elements in a graphical user interface.
Some of the activities that cause events to be
generated are pressing a button, entering a
character via the keyboard, selecting an item in a
list, and clicking the mouse.
Many other user operations could also be cited as
examples. Events may also occur that are not
directly caused by interactions with a user
interface.
For example, an event may be generated when a
timer expires, a counter exceeds a value, software
Event Sources
A source is an object that generates an event. This
occurs when the internal state of that object
changes in some way.
Sources may generate more than one type of
event. A source must register listeners in order for
the listeners to receive notifications about a specific
type of event.
Each type of event has its own registration method.
Here is the general form:
public void addTypeListener (TypeListener el )
Here, Type is the name of the event, and el is a
reference to the event listener.
For example, the method that registers a keyboard
event listener is called addKeyListener( ). The
method that registers a mouse motion listener is
Event Listeners
A listener is an object that is notified when an
event occurs. It has two major requirements.
First, it must have been registered with one or
more sources to receive notifications about
specific types of events.
Second, it must implement methods to receive
and process these notifications.
The methods that receive and process events are
defined in a set of interfaces, such as those found
in java.awt.event.
For example, the MouseMotionListener interface
defines two methods to receive notifications when
the mouse is dragged or moved.
Any object may receive and process one or both
Event Classes
The classes that represent events are at the core
of Javas event handling mechanism.
Thus, a discussion of event handling must begin
with the event classes.
It is important to understand, however, that Java
defines several types of events and that not all
event classes can be discussed in this chapter.
Arguably, the most widely used events at the
time of this writing are those defined by the AWT
and those defined by Swing.
This chapter focuses on the AWT events. (Most of
these events also apply to Swing.) Several Swingspecific events are described later, when Swing is
covered.
VK_ALT
VK_DOWN
VK_LEFT
VK_RIGHT
VK_CANCEL
VK_ENTER
VK_PAGE_DO
VK_SHIFT
WN
VK_PAGE_UP
VK_UP
VK_CONTROL VK_ESCAPE
char getKeyChar( )
int getKeyCode( )
If no valid character is available, then getKeyChar( )
returns CHAR_UNDEFINED. When a KEY_TYPED event
occurs, getKeyCode( ) returns VK_UNDEFINED.
The
mouse
ED
component.
MOUSE_EXITED The mouse
entered
exited
from
component.
MOUSE_MOVED The mouse moved.
MOUSE_PRESS The mouse was pressed.
a
a
WINDOW_CLOSED
WINDOW_CLOSING
WINDOW_DEACTIVATED
be closed.
The window was deactivated.
WINDOW_DEICONIFIED
WINDOW_GAINED_FOCUS
WINDOW_ICONIFIED
WINDOW_LOST_FOCUS
WINDOW_OPENED
WINDOW_STATE_CHANGED
import java.awt.*;
import java.awt.event.*;
public class MouseEventHandling {
private Frame f;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
public MouseEventHandling() {
f = new Frame("Java MouseEvent Examples");
f.setSize(400, 400);
f.setLayout(new GridLayout(3, 1));
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
headerLabel = new
Label("MouseListener",Label.CENTER);
statusLabel = new Label("",Label.CENTER);
controlPanel = new Panel();
controlPanel.setLayout(new FlowLayout());
f.add(headerLabel);
f.add(controlPanel);
f.add(statusLabel);
Panel panel = new Panel();
panel.setBackground(Color.magenta);
panel.setLayout(new FlowLayout());
panel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
statusLabel.setText("Mouse Clicked: (" +
e.getX() + ", " + e.getY() + ")");
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
Label msglabel = new Label();
msglabel.setAlignment(Label.CENTER);
msglabel.setText("Welcome to MouseEvent Demo");
msglabel.addMouseListener(new MouseListener()
{
@Override
public void mouseClicked(MouseEvent e) {
statusLabel.setText("Mouse Clicked: (" +
e.getX() + ", " + e.getY() + ")");
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
panel.add(msglabel);
controlPanel.add(panel);
f.setVisible(true);
}
public static void main(String[] args) {
new MouseEventHandling();
}
}
Program 2
import java.awt.*;
import java.awt.event.*;
public class MouseEventDemo extends Frame implements MouseListener
{
int x = 0, y = 0;
String strEvent = "";
MouseEventDemo(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
addMouseListener(this);
setSize(300, 300);
setVisible(true);
}
TextEvent Handling
import java.awt.*;
import java.awt.event.*;
public class TextEventHandling {
private Frame f;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
private TextField textField;
public TextEventHandling() {
f = new Frame("Java TextEvent Handling Examples");
f.setSize(400, 400);
f.setLayout(new GridLayout(3, 1));
controlPanel.add(textField);
f.setVisible(true);
}
public static void main(String[] args) {
new TextEventHandling();
}
}
Frame mainFrame,aboutFrame;
Label headerLabel;
Label statusLabel;
Panel controlPanel;
public WindowEventHandling() {
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new GridLayout(3, 1));
Adapter Classes
import java.awt.*;
import java.awt.event.*;
public class KeyAdapterDemo extends KeyAdapter{
private Frame f;
private Label headerLabel;
private Label statusLabel;
private Panel panel;
private TextField textField;
public KeyAdapterDemo() {
f = new Frame("Java AWT Examples");
f.setSize(400, 400);
f.setLayout(new GridLayout(3, 1));
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent
windowEvent) {
System.exit(0);
}
});
@Override
public void keyPressed(KeyEvent e){
System.out.println("hello");
//
}
public static void main(String[] args) {
new KeyAdapterDemo();
}
}
Inner Classes
import java.awt.*;
import java.awt.event.*;
public class InnerClassEventHandling {
private Frame f;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
private TextField textField;
public InnerClassEventHandling() {
f = new Frame("Java AWT Examples");
f.setSize(400, 400);
f.setLayout(new GridLayout(3, 1));
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
controlPanel.add(textField);
f.setVisible(true);
}
public static void main(String[] args) {
InnerClassEventHandling a = new InnerClassEventHandling();
}
class CustomKeyListener implements KeyListener {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
statusLabel.setText("Entered text: " + textField.getText());
}
}
public void keyReleased(KeyEvent e) {
}
}
}
import
import
import
import
import
java.awt.Button;
java.awt.Frame;
java.awt.GridLayout;
java.awt.TextField;
java.awt.event.*;
@Override
public void componentAdded(ContainerEvent e) {
System.out.println("COmponent added");
}
@Override
public void componentRemoved(ContainerEvent e) {
System.out.println("Component removed");
}
});
f.add(new Button());
f.remove(t);
f.setVisible(true);
}
}