• Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing and Adding to a JPanel during Run Time

 
scott matzka
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a text based BlackJack application and want to add some jpg cards. I have created a panel and JLabel[] for the cards and added them. This step is just makeshift and not sure if it needs to be done this way. See below.

JPanel playerRow = new JPanel();
JLabel[] playerCards = new JLabel[7];

FlowLayout layout4 = new FlowLayout(FlowLayout.LEFT,10, 10);
playerRow.setLayout(layout4);
for(int i=0; i<7; i++){
playerCards[i] = new JLabel();
playerRow.add(playerCards[i]);
}
pane.add(playerRow);

Then I add the cards on deal and hit. See below. The cards are being placed correctly, but on the next deal, after I have taken at least one hit, the extra card from the hit stays on the screen and the 2 new cards replace the previous hands first 2 cards.

deal(){

//supposed to remove all the cards
gui.playerRow.removeAll();
gui.playerRow.revalidate();

tempImageIcon = new ImageIcon(playerHand[currentHand].hand[0].getImageLocation();
gui.playerCards[0] = new JLabel(tempImageIcon);
gui.playerRow.add(gui.playerCards[0]);

tempImageIcon = new ImageIcon(playerHand[currentHand].hand[1].getImageLocation());
gui.playerCards[1] = new JLabel(tempImageIcon);
gui.playerRow.add(gui.playerCards[1]);

gui.playerRow.revalidate();
}

hit(){

tempImageIcon = new ImageIcon(playerHand[currentHand].hand[count-1].getImageLocation());
gui.playerCards[count-1] = new JLabel(tempImageIcon);
gui.playerRow.add(gui.playerCards[count-1]);
}

Anybody know how I can delete all the cards played in the previous hand when I hit deal again. thanks.

Scott
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scott matzka wrote:I have written a text based BlackJack application and want to add some jpg cards. I have created a panel and JLabel[] for the cards and added them. This step is just makeshift and not sure if it needs to be done this way. See below.

JPanel playerRow = new JPanel();
JLabel[] playerCards = new JLabel[7];

FlowLayout layout4 = new FlowLayout(FlowLayout.LEFT,10, 10);
playerRow.setLayout(layout4);
for(int i=0; i<7; i++){
playerCards[i] = new JLabel();
playerRow.add(playerCards[i]);
}
pane.add(playerRow);

Then I add the cards on deal and hit. See below. The cards are being placed correctly, but on the next deal, after I have taken at least one hit, the extra card from the hit stays on the screen and the 2 new cards replace the previous hands first 2 cards.

deal(){

//supposed to remove all the cards
gui.playerRow.removeAll();
gui.playerRow.revalidate();

Sometimes a call to repaint() is required after a revalidate(). Try it.

I might suggest, however, that you never actually remove any JLabels from your playerRow panel.
You can just call playerCards[index].setIcon(null) to make a JLabel invisible. Then to make it appear
again replace null with the appropriate imageIcon. You shouldn't have to call revalidate() or repaint().
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello scott and welcome to JavaRanch.

Please use the code tags when posting code to make it easier to read.
Brian Cole wrote: You can just call playerCards[index].setIcon(null) to make a JLabel invisible. Then to make it appear
again replace null with the appropriate imageIcon.


Easily way to do it is setVisible(false) then setVisible(true). JComponent API
 
scott matzka
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Now I'm using this code and everything works great until I ask to take another card (on hit). after this, the screen is cleaned and the new cards added but they are added with an offset and not directly flush left, which they were originally. a little confusing. i think i understand that there are some JLabel(null) before the cards, but why only after I take a hit. Any answers? Thanks.
 
scott matzka
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, the setVisible did it. perfect!!
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scott matzka wrote:


Now I'm using this code and everything works great until I ask to take another card (on hit). after this, the screen is cleaned and the new cards added but they are added with an offset and not directly flush left, which they were originally. a little confusing. i think i understand that there are some JLabel(null) before the cards, but why only after I take a hit. Any answers? Thanks.


Your code keeps calling add() after setIcon(somethingNonNull).

In the code you posted before you were adding all the JLabels in the initialization phase. If you're still doing that (which makes sense to me) then you're done after calling setIcon()--no need to call add() because it has already been added. That could be causing your extra-space problem, but it's hard to tell from the code you've shown us.
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scott matzka wrote:Thanks for the help, the setVisible did it. perfect!!


My pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!