Java Reference
In-Depth Information
// Mouse event handler for a selection button
import java.awt.Cursor;
import java.awt.event.*;
class MouseHandler extends MouseAdapter {
Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);
Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
// Handle mouse entering the selection button
@Override
public void mouseEntered(MouseEvent e) {
e.getComponent().setCursor(handCursor);
// Switch to
hand cursor
}
// Handle mouse exiting the selection button
@Override
public void mouseExited(MouseEvent e) {
e.getComponent().setCursor(defaultCursor);
// Change to
default cursor
}
}
Directory "Lottery 2 with mouse listener"
All you need to do to expedite this is to add a mouse listener for each of the six selection buttons. You
need only one listener object and after creating this you only need to change the loop in the createGUI()
method for the applet to add the mouse listener:
int[] choices = getNumbers(); // Get
initial set of numbers
MouseHandler mouseHandler = new MouseHandler();
// Create the
listener
for(int i = 0 ; i < NUMBER_COUNT ; ++i) {
luckyNumbers[i] = new Selection(choices[i]);
// Button is it's own listener
luckyNumbers[i].addActionListener(luckyNumbers[i]);
luckyNumbers[i].addMouseListener(mouseHandler);
buttonPane.add(luckyNumbers[i]);
}
Directory "Lottery 2 with mouse listener"
How It Works
The mouseEntered() method is called when the mouse enters the area of the component with which the
listener is registered, and the method then changes the cursor for the component to a hand cursor. When
Search WWH ::




Custom Search