This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Gui image game stuck Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Gui image game stuck" Watch "Gui image game stuck" New topic
Author

Gui image game stuck

Ryan Smithy
Greenhorn

Joined: Dec 15, 2012
Posts: 5
hi im looking to have an array of image buttons to appear and under the buttons when clicked are different coloured images
i have the 12 image buttons appearing in panel and when you click the bottom left button the red colour image appears behind it but none of the others are working i no its something simple but i cant figure it out any help would be appreciated
my code is below



Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
Welcome to the ranch.

Please UseCodeTags when posting code. I've added them for you this time.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
when you click the bottom left button the red colour image appears behind it

I think you mean bottom right.

The reason you have a problem is the only button reference you are keeping is the last button you create. This is because you are creating 12 new JButtons and storing the references to them in the same variable ie button1. You should store the references in an array or a Collection of some type. Personally, I'd store them in a Map with the button reference as the key and the name of the colour image to display as the value. Then in your actionListener you can easily find which colour image you want to display when a button is pressed.

BTW why are you creating another 12 buttons for the Pics array (which should be called pics - by convention in Java variable names start with a lower case letter).
Also in your actionPerformed() method why are you setting button1's icon to 3 different values one after the other.
Ryan Smithy
Greenhorn

Joined: Dec 15, 2012
Posts: 5
okay thanks for quick reply il try to do what you said now
Ryan Smithy
Greenhorn

Joined: Dec 15, 2012
Posts: 5
BTW why are you creating another 12 buttons for the Pics array (which should be called pics - by convention in Java variable names start with a lower case letter).
Also in your actionPerformed() method


why are you setting button1's icon to 3 different values one after the other.

im doing this because when you press the orange button i want all the colours behind them to be shown when ever there clicked, im prob not doing it right.

can i ask wud you make button 1 an array button like for example button1[i]............... i tried this and it didnt seem to work

Ryan Smithy
Greenhorn

Joined: Dec 15, 2012
Posts: 5
BTW why are you creating another 12 buttons for the Pics array

sorry these shud not be buttons just images i kept them as buttons just because i got them working .
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
Now is the time to StopCoding (← click). I suggest you read this link before going any further, then post more questions here if you have them.
Ryan Smithy
Greenhorn

Joined: Dec 15, 2012
Posts: 5
Okay i undertand
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Gui image game stuck
 
Similar Threads
How to get a panel to display a button and a component?
Applet background image help
JToggleButton Question
Recreating Buttons
What is the best way to create GUI components- in-line, methods, inner classes or separate classes