I have a simple object which extends JPanel
, when the update()
method is called on this object it it meant to add some labels to the panel and then repaint. However the labels do not show up after the update method is called, below is the code for update:
public void update(){
GridBagConstraints constraints = new GridBagConstraints();
if(cardsHidden){
for(int i = 0; i < 2; i++){
constraints.gridx = i;
constraints.gridy = 0;
JLabel card = new JLabel(PlayingCards.cardImages[PlayingCards.CARD_BACK_INDEX]);
add(card, constraints);
}
}
else{
Card[] holeCards = player.getHoleCards();
for(int i = 0; i < holeCards.length; i++){
constraints.gridx = i;
constraints.gridy = 0;
JLabel card = new JLabel(holeCards[i].getImageIcon());
add(card, constraints);
}
}
validate();
repaint();
}
any ideas?
Thanks
EDIT
solved:
It turns out that the HoleCardsPanel
wasn't adding to its parent frame properly. Once that was fixed the adding of new JLabel
s works fine. I also:
- added the call to the
update()
method to the event dispatch thread usingSwingUtillities.invokeLater
- had to call
validate()
from the uppermost component (in this case theJFrame
) as Devon_C_Miller suggests in his answer.