Introduction
- In Java, interacting with the user via various forms of input is relatively easy compared
to other languages.
- Most notably:
- Keyboard input (pressing and releasing a key)
- Mouse clicks
- Mouse motion
- Mouse “over” events
- Interacting with widgets in an abstract sense
- We refer to all of the above as Events
- Today, we'll cover how to do this
Events & Listeners
- The following is rather abstract, with gratuitous in-class discussion...
- For each type of event listed above, Java provides an interface and framework
for capturing and handling the event.
- When a class implements the appropriate interface, Java allows objects
of that class to capture/detect those events.
- When an objects is listening for those events, it is made aware of
any such event via method calls.
- Java passes information about the event to each listening object
- This is called the Event + Listener Model
-
- The class you want to listen for these events must
implement KeyListener.
This signals to Java that this class could be notified of events related to
the keyboard.
- An instance of that class (an object) must direct Java that it should be
notified of events related to the keyboard by saying
addKeyListener(this). This signals to Java that any events of
this class will be sent to this object.
- Events are received through the use of methods. For each class of events, each
specific event corresponds to a method. When that event is received, the
corresponding method is fired.
- Classes are capable of listeneing for events by implementing the appropriate interface.
- Objects of such classes receive events by registering themselves through addSomeListener(object)
- Events are sent to the object through methods.
Keyboard Events
- Keyboard events include
- pressing a key on the keyboard
- releasing a key on the keyboard
- typing a key on the keyboard
- A class can listen for these events by implementing the KeyListener interface
- Such a class then must define the following methods:
- public void keyPressed(KeyEvent e)
- public void keyReleased(KeyEvent e)
- public void keyTyped(KeyEvent e)
- The method addKeyListener(object) is used to have an individual
object receive and respond to these events.
- Explanation of methods:
- keyPressed
- This method is called as soon as the user presses any key on the keyboard
- keyReleased
- This method is called as soon as the user releases any key on the keyboard
- keyTyped
- This method is a little tricky, but intuitive. You've probably noticed
that if you hold down a given key, say b, whatever program your running will
print-out/record multiple b's (think of typing in a Word document and
holding down the b key... you'll get lots-o-b's in your document). They
keyTyped event is what's being sent for this.
- Documentation & Tutorial:
Mouse Events
- Mouse events events include
- pressing a mouse button
- releasing a mouse button
- the mouse entering/exiting a given context
- A class can listen for these events by implementing the MouseListener interface
- Such a class then must define the following methods:
- public void mousePressed(MouseEvent e)
- public void mouseReleased(MouseEvent e)
- public void mouseClicked(MouseEvent e)
- public void mouseEntered(MouseEvent e)
- public void mouseExited(MouseEvent e)
- The method addMouseListener(object) is used to have object receive and respond to these events.
- Explanation of methods:
- mousePressed
- This event is called as soon as the user presses either the left,
middle, or right mouse buttons
- mouseReleased
- This event is called as soon as the user releases either the left,
middle, or right mouse buttons
- mouseClicked
- This event is analagous to the keyTyped event.
- mouseEntered
- This event is called anytime the mouse first hovers over a given
visual object. For example, if you wanted to change the icon used by the mouse while
it was over a specific object, this is the method you would use.
- mouseExited
- This event is called anytime the mouse leaves a given visual object.
This is what you would use, in the above example, to change the mouse icon back to the
normal one.
- Documentation & Tutorial:
Mouse Motion Events
- Mouse Motion events include
- moving the mouse
- dragging the mouse (moving while a button is held down)
- A class can listen for and receive mouse motion events by implementing the MouseMotionListener interface.
- Such a class must define the following methods:
- public void mouseMoved(MouseEvent e)
- public void mouseDragged(MouseEvent e)
- The method addMouseMotionListener(object) is used to have object
receive and respond to these events.
- Explanation of methods:
- mouseMoved
- This event is called anytime the user moves the mouse (with no button pressed).
- mouseDragged
- This event is called anytime the user moves the mouse with some button pressed.
- Documentation & Tutorial:
Action Events
- You've seen us build GUI's in class, with buttons, text inputs, etc. It's possible to
build very complicated GUI's quite easily in Java
- In all GUI's, we need to be able to respond to events such as clicking a button, etc.
- Such events are called action events
- To have a class listen for and receive action events, it needs to implement the
ActionListener interface and define the following method:
- public void actionPerformed(ActionEvent e)
Then use addActionListener(object) to have object actually
receive and respond to any such event.
- The actionPerformed method is called anytime any sub-component of an
object is clicked/selected or otherwise changed.
- Documentation & Tutorial:
Ohter Events
- Java has many types of events you can listen for.
- The above are rather generic in that we are dealing with primitives (moving mouse,
typing keyboard, etc.)
- Many Java objects (JButton, JPanel, etc.) have their own, specific type of
listeners that give you more detailed information.
- See this tutorial for a complete list of all input listeners Java supports, and what they
can do.