• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unclick JButton by clicking second times

 
John Joe
Ranch Hand
Posts: 68
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have a button, when it is clicked, it will change to green colour. Is it possible to unclick it by clicking second times ?

 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should consider using a JRadioButton or JToggleButton so it has a on/off status, not a pure JButton.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or perhaps you could use some code which does something like "If the colour is GREEN then set the colour to (whatever the default value is) else set the colour to GREEN".
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is, as people have already told you. But that listener class should be simplified. I don't like to see sources, nor instanceof. I think frequent use of instanceof suggests one is drifting from the principles of object oriented programming. You can pass the Component to colour as a constructor parameter.The use of the & operator is a bit like % 2 but it never returns a negative result, and it is faster than %. The first colour will of course be red, green second.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!