CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2012
    Posts
    2

    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");
    		}
    	}
    }

  2. #2
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: JLabel, Observer, setText problem

    Basic debugging would probably show you that it's not even getting into the update method:

    Code:
    ...
    	private class PlayerObserver implements Observer {
    		@Override
    		public void update(Observable player, Object arg1) {
    System.out.println("inside the update method"); // Basic debugging, something that should have been learned right after 'Hello World'
    			Player p = (Player) player;
    ...
    Then the question would be why didn't it enter the update method? The only place I see a PlayerObserver object being used is here:

    Code:
    ...
    	private Player p;
    
    ...
    		
    	public PlayerInfoPanel(Player p) {
    		this.p = p;
    
    		PlayerObserver observer = new PlayerObserver();
    		this.p.addObserver(observer);
    ...
    Ok, so its the Player.addObserver method thats using the PlayerObserver object. What does Player.addObserver do & why wouldn't it work? Who knows. You haven't posted the definition of the Player class.

  3. #3
    Join Date
    Feb 2012
    Posts
    2

    Re: JLabel, Observer, setText problem

    Well, there's no need for you to be snide, or a dick.

    No, I didn't post all 500 lines of code on the UI. I was wondering if there was something simple I'd been staring at.

    "Basic debugging, something that should have been learned right after Hello, Word."

    Really? You think I wrote only this code and wondered why it didn't work?

    Last stop I make here - I was hoping to find a community, not a handful of self-interested but not-so-intelligent ***-holes.

    I won't bother to check this again, I'm going to rebuild anyhow. It's been pretty hard to develop anything without any basic debugging skills.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JLabel, Observer, setText problem

    Well, there's no need for you to be snide, or a dick...
    Martin O's post may have been a little blunt but there's no need to be so sensitive especially as you didn't even show what effort you had made to solve the problem yourself.
    Last stop I make here - I was hoping to find a community, not a handful of self-interested but not-so-intelligent ***-holes.
    I'm impressed you can deduce all that from one reply from one person
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured