This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello, I'm working on creating a cribbage game, and have a question: In building the GUI, I've been displaying the playing cards by wrapping them in a JLabel object. (Using ImageIcon) Each card's image is a GIF file that I have saved in the same folder as the .java files. This works fine, except I'm not really sure if this is the best way. For instance, as the Cribbage game is being played, the cards that are displayed are going to be constantly changing. (obviously) Every time I want to display a new card, should I be creating new JLabels? Should each of the 52 cards have it's own JLabel? This seems inefficient, so I figured there might be a more logical way to handle this. If anyone has any ideas I would be appreciative. Thanks!
You could have a set of JLabels and one ImageIcon for each label. When the card that label should display changes, simply change the icon's source image using setImage() and then call invalidate() on the label and validate() on its container. You may also need to repaint(), it should work fine.