JAVA Session11
JAVA Session11
Objectives
Lets see how to use the Event handling API to handle simple
GUI events.
ActionListener Interface:
Has only one method i.e.
actionPerformed(ActionEvent)
To detect when the user clicks an onscreen button (or does the
keyboard equivalent), a program must have an object that
implements the ActionListener interface.
The program must register this object as an action listener on
the button (the event source), using the
addActionListener() method.
When the user clicks the onscreen button, the button fires an
action event.
MouseListener interface:
To detect the mouse clicking, a program must have an object
that implements the MouseListener interface.
This interface includes several events including
mouseEntered, mouseExited, mousePressed,
mouseReleased, and mouseClicked.
When the user clicks the onscreen button, the button fires an
action event.
m1.add(mi3);
m1.addSeparator();
m1.add(mi4);
Let’ see how MenuItem will look like.
Creating a CheckBoxMenuItem:
CheckboxMenuItem mi5 =
newCheckboxMenuItem("Persistent");
mi5.addItemListener(this);
m1.add(mi5);