Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to disable a JButton without it turning gray.  RSS feed

 
Jane Hall
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm writing a tic tac toe game, and have colored X and 0 icons which should appear on JButtons when the empty button is clicked.

At the moment, I'm using setEnabled(false) to stop a player being able to click a square on the board that has already been used. But that makes the colored X and 0 icons gray.

Is there a way to stop this happening?

Thanks!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could remove the ActionListeners, so the button is still enabled, can be pressed, but doesn't do anything.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall move this discussion to our GUIs forum.
 
Jane Hall
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response. How would you go about removing them?

I looked up removeActionListener, but it's not clear to me how it's used - could you enlighten me?

I tried adding each the following to my ActionPerformed method:







But neither works. I also tried creating a new method:



But I'm not sure how this could help!

Thanks.



 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The removeActionListener is called from the JButton, so you would do:

You need to provide an ActionListener to remove. This might not be too hard - you presumably are in the action listener when you want to do the removal, so you might do this:

Or, if you are someplace else and aren't sure how to get a reference to a particular ActionListener you would brute-force it and remove all listeners:
 
Jane Hall
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant! I used

 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why are you using addActionListener(this) in the first place? I know you find it in lots of books, and even the Java Tutorials, but it usually means you are departing from object‑oriented design.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And why are you using addActionListener(this) in the first place? I know you find it in lots of books, and even the Java Tutorials, but it usually means you are departing from object‑oriented design.


That isn't necessarily what is happening. The user clicks on a button, and it switches from nothing to either an O or X. The OP also wants to disable the action so it can't be switched again. Since when you switch the button's display you are currently 'in' the action listener which does the switching when you want to disable the switching removeActionListener(this) should remove that ability regardless of how your listeners are defined.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right. Sorry for my mistake.
 
Jane Hall
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of interest, why would what you said:

Campbell Ritchie wrote:And why are you using addActionListener(this) in the first place? I know you find it in lots of books, and even the Java Tutorials, but it usually means you are departing from object‑oriented design.


mean (or sometimes mean) a departure from OOP?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You end up with a long bank of
if (e.getSource() == button1) . . . else if (e.getSource() == button2) ... else if (e.getSource() == radioButton99) . . . blocks
You should attach one listener object (or sometimes more than one) to each control object, each with the action appropriate to that button.
[Another way to do it is with AbstractAction and similar.]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!