Ancient Behemoth
Ancient Behemoth

Reputation: 68

How to change appearance of pressed/clicked/selected button in Java?

I'd like to change the appearance of a button when it's in pressed/clicked/selected state.

To be more specific, I'd like to change it's border to BorderFactory.createLoweredBevelBorder() when it' is pressed/clicked/selected.

How can I do this?

Upvotes: 2

Views: 4249

Answers (2)

mKorbel
mKorbel

Reputation: 109823

Have look at ButtonModel for JButtons JComponents, there are implemented all your requierements

Upvotes: 2

Dan D.
Dan D.

Reputation: 32391

Please see the code below. It sets the border when pressed and resets it when released. You can also do this on mouseEntered / mouseExited.

button.addMouseListener(new MouseAdapter() {
  public void mousePressed(MouseEvent e) {
    button.setBorder(BorderFactory.createLoweredBevelBorder());
  }

  public void mouseReleased(MouseEvent e) {
    button.setBorder(null);
  }
});

Upvotes: 3

Related Questions