• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to disable a JButton without it turning gray.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could remove the ActionListeners, so the button is still enabled, can be pressed, but doesn't do anything.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brilliant! I used

 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right. Sorry for my mistake.
 
Jane Hall
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.]
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic