|
-
February 1st, 2012, 03:09 PM
#1
JLabel, Observer, setText problem
I've seen a lot of posts regarding this issue, but I'm completely stumped as to what I'm doing wrong. I'm hoping someone here will be able to see what the problem.
I have a JPanel onto which I'm placing some JLabels. I then have an inner class implementing the Observer interface, which updates the JLabels as you can see in the code. What's completely blowing my mind is that the currentScore and cumScore labels update fine: the setText() updates the text in the label as expected. The nameField label does not update.
I whittled the problem down because I was unable to change the background color of the JPanel from the listener, or even change the background color of the labels (yes, I set the opacity to true and made sure and revalidated). While trouble shooting I've narrowed it down to this - and it's baffling to me.
If anyone is reading the code thinking, "Oh, what an idiot" then please let me know why I am such.
Thanks for your help!
Code:
private class PlayerInfoPanel extends JPanel {
private Player p;
private JLabel nameField;
private JLabel currentScore;
private JLabel cumScore;
private JLabel strategy;
public PlayerInfoPanel(Player p) {
this.p = p;
PlayerObserver observer = new PlayerObserver();
this.p.addObserver(observer);
setupControls();
}
private void setupControls() {
ScoreCard sc = p.getScoreCard();
int score = sc.getUpperTotal() + sc.getUpperBonus()
+ sc.getLowerTotal() + sc.getYahtzeeBonus();
nameField = new JLabel(p.getName());
currentScore = new JLabel(Integer.toString(score));
cumScore = new JLabel(Integer.toString(p.getScore() + score));
strategy = new JLabel(p.getStrategyName());
this.setLayout(new GridLayout(1, 4, 5, 15));
this.add(nameField);
this.add(strategy);
this.add(currentScore);
this.add(cumScore);
}
private class PlayerObserver implements Observer {
@Override
public void update(Observable player, Object arg1) {
Player p = (Player) player;
ScoreCard sc = p.getScoreCard();
int score = sc.getUpperTotal() + sc.getUpperBonus()
+ sc.getLowerTotal() + sc.getYahtzeeBonus();
currentScore.setText(Integer.toString(score));
cumScore.setText(Integer.toString(p.getScore()));
(won't update) -->> nameField.setText("Some text");
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|