Java Reference
In-Depth Information
MouseListener internalMouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
requestFocusInWindow();
}
};
addKeyListener(internalKeyListener);
addMouseListener(internalMouseListener);
}
public void addActionListener(ActionListener actionListener) {
actionListenerList.add(ActionListener.class, actionListener);
}
public void removeActionListener(ActionListener actionListener) {
actionListenerList.remove(ActionListener.class, actionListener);
}
protected void fireActionPerformed(ActionEvent actionEvent) {
EventListener listenerList[] =
actionListenerList.getListeners(ActionListener.class);
for (int i=0, n=listenerList.length; i<n; i++) {
((ActionListener)listenerList[i]).actionPerformed(actionEvent);
}
}
public boolean isFocusable() {
return true;
}
}
Timer Class
In addition to the
invokeAndWait()
and
invokeLater()
methods of
EventQueue
, you can use the
Timer
class to create actions to be executed on the event-dispatch thread. A
Timer
provides a
way of notifying an
ActionListener
after a predefined number of milliseconds. The timer can
repeatedly notify the listeners, or just call them once.
Creating Timer Objects
Following is the single constructor for creating a
Timer
that specifies the millisecond delay time
between calls to the
ActionListener
:
public Timer(int delay, ActionListener actionListener);
// 1 second interval
Timer timer = new Timer(1000, anActionListener);