Reputation: 2702
I've created a button in AWT with the name "Reset". I want the cursor to be hand cursor when the mouse is hovered on this button.
I tried the mouseEntered
method of the MouseAdapter
class but no effect.
void createResetButton() {
Button resetButton = new Button("Reset");
resetButton.setBounds(300, 335, 100, 40);
add(resetButton);
resetButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
usernameTextField.setText(null);
passwordTextField.setText(null);
invalidMessage.setVisible(false);
}
@Override
public void mouseEntered(MouseEvent e) {
Cursor.getPredefinedCursor(HAND_CURSOR);
}
});
}
Thanks in advance.
Upvotes: 0
Views: 1278
Reputation: 10137
Your statement Cursor.getPredefinedCursor(HAND_CURSOR);
in your mouseEntered
method had no effect,
because you only got the cursor, but then did nothing with it.
The solution is simpler than you might have expected.
You don't need your mouseEntered
method.
Just use the setCursor(Cursor)
method of class Component
on your resetButton
.
void createResetButton() {
Button resetButton = new Button("Reset");
resetButton.setBounds(300, 335, 100, 40);
add(resetButton);
resetButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
resetButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
usernameTextField.setText(null);
passwordTextField.setText(null);
invalidMessage.setVisible(false);
}
});
}
Then AWT will do the rest for you: showing the hand cursor when the mouse
enters the resetButton
, and showing the normal cursor when leaving it.
Upvotes: 1
Reputation: 2702
I got this done this way after few hits and trials:
void createResetButton() {
Button resetButton = new Button("Reset");
resetButton.setBounds(300, 335, 100, 40);
add(resetButton);
resetButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
usernameTextField.setText(null);
passwordTextField.setText(null);
invalidMessage.setVisible(false);
}
@Override
public void mouseEntered(MouseEvent e) {
resetButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
});
}
Upvotes: 0