*
The moose likes Swing / AWT / SWT and the fly likes Set JButton icon on click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Set JButton icon on click" Watch "Set JButton icon on click" New topic
Author

Set JButton icon on click

Leonard Connoway
Greenhorn

Joined: Feb 19, 2013
Posts: 21
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

Joined: Dec 14, 2012
Posts: 34

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

Joined: Apr 20, 2011
Posts: 237
    
    1

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

Joined: Dec 14, 2012
Posts: 34

So? what problem you are facing?When you change the turn, change the icon.
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155
- use JToggleButton instead of JButton

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

Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 237
    
    1

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

Joined: Feb 19, 2013
Posts: 21
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

Joined: Apr 20, 2011
Posts: 237
    
    1

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

Joined: Feb 19, 2013
Posts: 21
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

Joined: Apr 20, 2011
Posts: 237
    
    1

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

Joined: Feb 19, 2013
Posts: 21
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

Joined: Apr 20, 2011
Posts: 237
    
    1

You need to add ".class" after your class name.
Leonard Connoway
Greenhorn

Joined: Feb 19, 2013
Posts: 21
That worked perfectly. Thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set JButton icon on click