C
***C.Steamer***
Basically I want the application to call invokeLater() to update the view of
the gui. This is for a simple card game.I want to call a public method
called updateHand(Hand hand) which I pass in a hand then it clears the items
in the JPanel and adds a Label representing each of the cards to the Jpanel.
For some reason if I call this method from the constructor of the JPanel but
the invoke method doesn't work. This is my code in the application layer.
Any suggestions why this isin't working?
private void updateScreen1()
{
Runnable updateScreen = new Runnable()
{
public void run()
{
Hand handTest = new Hand();
handTest.addCard(new Card(1,1));
handTest.addCard(new Card(2,1));
handTest.addCard(new Card(3,1));
handTest.addCard(new Card(4,1));
handTest.addCard(new Card(5,1));
mainPanel.updateHand(handTest);
}
};
SwingUtilities.invokeLater(updateScreen);
}
And the actual method in my main panel is as follows.Note that handPanel is
just a panel I added in my mainPanel which is an extension of JPanel
public void updateHand(Hand hand)
{
handPanel.removeAll();
for(int i =0;i<hand.getNumCards();i++)
{
CardLabel cardLabel = new CardLabel(hand.getCard(i));
handPanel.add(cardLabel);
cardLabel.addMouseListener(this);
}
handPanel.repaint();
}
the gui. This is for a simple card game.I want to call a public method
called updateHand(Hand hand) which I pass in a hand then it clears the items
in the JPanel and adds a Label representing each of the cards to the Jpanel.
For some reason if I call this method from the constructor of the JPanel but
the invoke method doesn't work. This is my code in the application layer.
Any suggestions why this isin't working?
private void updateScreen1()
{
Runnable updateScreen = new Runnable()
{
public void run()
{
Hand handTest = new Hand();
handTest.addCard(new Card(1,1));
handTest.addCard(new Card(2,1));
handTest.addCard(new Card(3,1));
handTest.addCard(new Card(4,1));
handTest.addCard(new Card(5,1));
mainPanel.updateHand(handTest);
}
};
SwingUtilities.invokeLater(updateScreen);
}
And the actual method in my main panel is as follows.Note that handPanel is
just a panel I added in my mainPanel which is an extension of JPanel
public void updateHand(Hand hand)
{
handPanel.removeAll();
for(int i =0;i<hand.getNumCards();i++)
{
CardLabel cardLabel = new CardLabel(hand.getCard(i));
handPanel.add(cardLabel);
cardLabel.addMouseListener(this);
}
handPanel.repaint();
}