• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make a button stay look like pressed?

 
Veronique Kus
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was wondering how I could make a button look like pressed after clicking on it until another button is clicked?
I can kind of simulate the clicked look by setting a lowered BevelBorder to the button but when I do that, it doesnt look
exactly how it should (I want the button to look like when the mouse is pressed on it). I tried using the doClick() method but
I don't think it's suitable as apart from changing the look it also fires an action. I also tried setting a default ButtonModel
and calling setPressed() method on the button's model. I read somewhere that i could try using the JToggleButton class but
I'm not sure how I could go about it...

Thanks in advance.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
calling setPressed() method on the button's model.


That should work. Don't forget when you click on a button the default code will reset this property, so maybe you have a timing problem. Try wrapping your code in a SwingUtilities.invokeLater(...) so your code is the last code executed.

If you need more help then post your SSCCE.
 
Veronique Kus
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the hint.
After I read that setPressed() should've worked I looked at the buttons very closely and actually I think it did - but the difference is barely visible... the buttons are made to look slightly pressed which is barely noticeable and they don't retain that blue shade which appears when I actually press a mouse button on them... any idea how to retain that blue shade?
 
Sriraman Srinivasan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using the below method...
public void doClick(int pressTime)


You can mention the time to be pressed in milliseconds.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JToggleButton in a ButtonGroup sounds like it would suit your description

simple demo (click a button, then another etc)

 
Veronique Kus
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last piece of code does exactly what I wanted, thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!