tags:

views:

78

answers:

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.

A: 

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
whats wrong with add JLabel in ActionListener. The Images have to change as the game progresses...
Myth17
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
I dont know how the number of cards which can be displayed.Depends on gameplay..so how many JLabel do I declare initially?
Myth17
@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
I am taking adding JLabels as a way of drawing cards on a JPanel. What other alternatives do I have(Except overridding paintComponent() )?
Myth17
@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
There is nothing wrong with dynamically adding or removing components in ActionListeners in this case.
Russ Hayward
+1  A: 

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
What game are you trying to do ? What is the max number of cards you need to display ?
Romain Hippeau
I am trying to show cards on the JPanel as soon as "Deal" button is hit.(its blackjack) Initially 2cards are dealt.
Myth17
+2  A: 

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