• Post Reply Bookmark Topic Watch Topic
  • New Topic

ImageIcon Update Questions : Is it Possible?  RSS feed

 
Donald Wedding
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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!
 
raj madhuram
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setIcon() method of JLabel should do it. Can you post a code fragment that doesn't work?
regards,
Raj
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!