3
 class Deal implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
             dl.setDeck();
             dl.shuffle();
             dl.firstDraw(pl);

             for(Card c:pl.showHand())
             panelplay.add(new JLabel(c.getImageIcon()));

             panelplay.validate();
        }
    }

This is an event handler for a Jbutton. The method pl.showHand() returns a ArrayList of a user defined class 'Card'. Inserting a println() inside the loop shows the print, so the code is being executed but the Panel panelplay isnt showing card Images.

Nitish Upreti
  • 6,312
  • 9
  • 50
  • 92
  • Look at this post http://stackoverflow.com/questions/299495/java-swing-how-to-add-an-image-to-a-jpanel – Romain Hippeau Jun 17 '10 at 18:11
  • I dont want to override paintcomponent, the other option is to add it to a JLabel and then add the label to JPanel. I am doing it but its still not working for me.. :| – Nitish Upreti Jun 17 '10 at 18:20

3 Answers3

2

What about the existing labels on the panel? You don't remove them. I'm guessing you are using a FlowLayout and the labels just get added to the end of the panel so you don't see them.

So one solution is to use panel.removeAll() before adding the labels back to the panel. I then use:

panel.revalidate();
panel.repaint();

Or the better option as suggested earlier is to not replace the labels but just replace the Icons using the setIcon() method.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

Do as Gilbert says, look at the Swing Tutorial part that concerns Labels.
JLabel has the following methods... void setIcon(Icon)
Icon getIcon()

Also look at the SplitPaneDemo It does exactly what you want, you can even run it with JNLP to see.

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
0

You don't want to add the JLabel in the ActionListener.

You want to use an already added JLabel setText() method in the ActionListener.

You define all the Swing components once, when you create the GUI.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • whats wrong with add JLabel in ActionListener. The Images have to change as the game progresses... – Nitish Upreti Jun 17 '10 at 18:30
  • That's why Swing components have methods. You set the text in an already added JLabel. You draw the image in an already added JPanel. You define all of the Swing components once, when you create the GUI. – Gilbert Le Blanc Jun 17 '10 at 18:37
  • I dont know how the number of cards which can be displayed.Depends on gameplay..so how many JLabel do I declare initially? – Nitish Upreti Jun 18 '10 at 03:47
  • @Myth17: That's a different question than the question you asked. Have you seen the Windows Solitaire card game? You might have to draw card images on a JPanel, rather than use a variable number of JLabels to hold a variable number of cards, – Gilbert Le Blanc Jun 18 '10 at 05:24
  • I am taking adding JLabels as a way of drawing cards on a JPanel. What other alternatives do I have(Except overridding paintComponent() )? – Nitish Upreti Jun 18 '10 at 05:53
  • @Myth17: That's your alternative. Overriding paintComponent(). Here's a quick explanation of drawing on a JPanel. http://leepoint.net/notes-java/GUI-lowlevel/graphics/40drawingpanel/10drawingpanel.html – Gilbert Le Blanc Jun 18 '10 at 06:12
  • There is nothing wrong with dynamically adding or removing components in ActionListeners in this case. – Russ Hayward Jun 18 '10 at 10:45