• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Gui image game stuck  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch.

Please UseCodeTags when posting code. I've added them for you this time.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thanks for quick reply il try to do what you said now
 
Ryan Smithy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay i undertand
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!