• 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

ImageIcon Update Questions : Is it Possible?

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm writing an application that displays "playing cards". I have gif files for all the cards, such as "aceclubs.gif", "sevendiamonds.gif", etc.
I'm displaying the cards using JLabel like this ...
JLabel fred = new JLabel("hello",
cardImageIcon,
JLabel.CENTER);
(where cardImageIcon is an image icon).
Now, when I press a button, I want to change
the card displayed to a different card.
Can't seem to do it. It seems that Java lets
me change the text (i.e. "hello" to "goodbye")
but won't let me change my image from
"aceofhearts.gif" to "twospades.gif")
If not ImageIcon ... what should I use?
Should I use something other than JLabel?
Thanks!
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setIcon() method of JLabel should do it. Can you post a code fragment that doesn't work?
regards,
Raj
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are needing to change cards, there is a better way than changing the image.
Create all your cards in one sweep adding the images needed to each card. You can use whatever component you wish to hold the JLabel. Personally I would use a JPanel or even a JButton (easier to catch click events) and place a JLabel with an ImageIcon on one of those.
Use the CardLayout to place your cards. That way you can stack them. Card Layout uses an int to keep track of where the card is in the stack. If you need to display a new card, just move that card to the top of the Layout.
Does that make sense?
 
Donald Wedding
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For some reason, I looked at the docs a thousand times and kept overlooking "setIcon".
It worked, of course!
Thanks for the help, I'll go get my eyes
checked now!
 
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic