• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set JButton icon on click

 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure that this question has been asked before, but none of the answers I have found so far have matched exactly what I am looking for. I am making a simple tic-tac-toe game, and I want to be able to set the icon of a button when it is clicked, but what the icon is depends on what the player's piece is. For instance, if the player is "X," then it will make the button have an "X." Most solutions I have found involve using setPressedIcon() and then adding the button to the panel with that information, but I cannot set the pressed icon until after the buttons have already been created and added to the board. Also, the computer must be able to change the icon of a button to the opposing letter. Can anyone help me find a way to do this?
 
rohan sethi
Ranch Hand
Posts: 34
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use:



to set the icon on the bases of your icon clicked, Add a mouseListener to the JLabel of your icon, in the actionPreformed, change the icon image.
If not clear, let me know.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And as for picking what icon to use, have a boolean state variable called turn. One player's turn is true and one player's turn is false. Whenever a button is clicked and it is given an icon, change the turn: turn = !turn;

 
rohan sethi
Ranch Hand
Posts: 34
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So? what problem you are facing?When you change the turn, change the icon.
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- use JToggleButton instead of JButton

- maybe ther is everything that you needed for tic-tac-toe game

 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m Korbel wrote:- use JToggleButton instead of JButton

- maybe ther is everything that you needed for tic-tac-toe game


This is quite superfluous.
 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohan sethi wrote:Use:



to set the icon on the bases of your icon clicked, Add a mouseListener to the JLabel of your icon, in the actionPreformed, change the icon image.
If not clear, let me know.


I am trying to do this, but I keep getting the error that non-static method getClass() cannot be referenced from a static context even though I do not have it set as static.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leonard Connoway wrote:
I am trying to do this, but I keep getting the error that non-static method getClass() cannot be referenced from a static context even though I do not have it set as static.


The getClass() method just returns your class, so you can replace it with YourClassName.class.getResource().
 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my current statement.

and I have imported



but, I am now getting the error "cannot find symbol - method getResource(java.lang.String)"
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leonard Connoway wrote:This is my current statement.



Look closely at what I wrote and what you wrote. You're missing something very important!
 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha I'm just not seeing it. I even tried copying/pasting exactly what you typed and it still said it cannot find the getResource method
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add ".class" after your class name.
 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked perfectly. Thank you very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic