This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2014/6/9

How to remove a JButton from my card layout.

JasonZhu JasonZhu

2014/6/9

#
I am coding a solitary game of Elevens where the goal is to use up all the cards by making sums of 11 with 2 number cards or a set of J, Q, K with 3 cards. The layout is a row of JButtons representing cards. The problem I'm encountering is deleting the used JButtons when the deck has no more cards. I can only seem to make the JButtons fade as if I setEnabled(false) even if I call the method remove() on the JButton. Help greatly appreciated. Here's my relevant code:
public class CardButton extends JButton
{
    private PlayingCard theCard;
    private int position;
    private Layout layout;

    public CardButton(PlayingCard card, int pos, Layout lay)
    {
        super(card.faceIcon);
        theCard = card;
        setIcon(card.faceIcon);
        position = pos;
        layout = lay;
    }

    public Card getCard()
    {
        return theCard;
    }

    public void setCard(PlayingCard card)
    {
        if(card==null){  //  I'm trying to get this to work.  I have a deal method in my Deck class that returns null if the cardCount is past 52 in which I pass to the parameter of this method.
            layout.remove(layout.getLay()[position]);
        }else{
            PlayingCard temp = card;
            setIcon(temp.faceIcon);
            theCard.setValue(temp.getValue());
            theCard.setFace(temp.getFace());
            theCard.setSuit(temp.getSuit());
        }
    }
}
The odd thing is if I had the layout of 9 cards and 1 remaining card in the deck, and I match 2 number cards to make 11, the layout will decrease to 8, removing 1 of the JButtons, but when I continue to finish off the game with no more cards in the deck, the cards used to create a sum of 11 do not remove themselves like before. It's not a problem of the deal() method because I've tested it and I'm sure it returns null after the 52nd card.
danpost danpost

2014/6/10

#
What about using 'setVisible(false)'. As far as the odd behavior, you probably need to show the relevant code.
JasonZhu JasonZhu

2014/6/10

#
Good suggestion for the visibility, I will try it tomorrow and post an update as soon as possible. Thanks Dan.
JasonZhu JasonZhu

2014/6/10

#
It works wonders. It beats deleting then having to replace the button by a long shot. Thanks a lot.
You need to login to post a reply.