Alex
Alex

Reputation: 5

How would I change the text of a JButton when hovering over it?

I have a music player application that has a repeat button. I want to make it so when the user hovers over the repeat button with their cursor, the text will change to display the current state of the repeat option (off, one, or list). How can I program my button to do this?

Upvotes: 0

Views: 168

Answers (1)

user432
user432

Reputation: 3534

Use a MouseListener and the methodes mouseEntered() and mouseExited().

final JButton btn = new JButton("repeat");
btn.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent e) {
        btn.setText("hover");
    }
    @Override
    public void mouseExited(MouseEvent e) {
        btn.setText("repeat");
    }
});

Upvotes: 2

Related Questions