Ok - so I have an object "A" that has a value (for the sake of example here, it's initialized to 0).

I'm making a GUI and I've created a panel that is supposed to display the amount from object "A" in a label which shows up on the screen. However, when I attempt to manipulate the value via the GUI, the value doesn't update.

From what I understand I need to write some kind of listener or some way for the panel to know that it needs to update, but I don't know how to do that. Can someone point me in the right direction? I assume it's label.updateSomeRandomThingIDontKnowAbout(), so if you know about it, let me know!

Again, I'm trying to display a text representation of a value from an object, through a GUI. I have a panel made, with a label that gets the value from the object. The initial value is shown, but never updated though it should be.

Thanks!

that's a wee bit hard to see, if you don't show any code, nor how you try to update your code or where you want to update it. in a JTextField, in JLabel, ...

Why dont you simply post the code??

import javax.swing.*;
import java.awt.*;


public class ScorePanel extends JPanel{

    JLabel scoreLabel;
    Player player;

        // ScorePanel constructor
    public ScorePanel(Player user){
        player = user;
        // Main panel
        setPreferredSize(new Dimension(150,100));
        setBackground(Color.lightGray);
        JLabel label = new JLabel(player.name());
        label.setFont (new Font("Arial", Font.BOLD, 20));
        add(label);

        // Score field panel
        JPanel score = new JPanel();
        score.setPreferredSize(new Dimension(100, 40));
        score.setBackground(Color.white);
        scoreLabel = new JLabel(player.toString());
        scoreLabel.setFont(new Font("Arial", Font.BOLD, 15));
        score.add(scoreLabel);
        add(score);
    }

}

as you said yourself, you'll need some Action and something to actually trigger that Action, pressing a JButton, for instance.

also, you'll be needing a place to get the new value you want to set

This is just the code in the code tags:

import javax.swing.*;
import java.awt.*;


public class ScorePanel extends JPanel{

JLabel scoreLabel;
Player player;

// ScorePanel constructor
public ScorePanel(Player user){
player = user;
// Main panel
setPreferredSize(new Dimension(150,100));
setBackground(Color.lightGray);
JLabel label = new JLabel(player.name());
label.setFont (new Font("Arial", Font.BOLD, 20));
add(label);

// Score field panel
JPanel score = new JPanel();
score.setPreferredSize(new Dimension(100, 40));
score.setBackground(Color.white);
scoreLabel = new JLabel(player.toString());
scoreLabel.setFont(new Font("Arial", Font.BOLD, 15));
score.add(scoreLabel);
add(score);
}

}

Are you trying to have the USER update the value, or is the program just updated through the object. If the program is always updated through a function in the object, the way you change the value of the JLabel would be

score.setText(newValueHere)

so every time the score changed, you would write that command to change the label to the updated value. Is this what you're looking to do?

This is just the code in the code tags:


Are you trying to have the USER update the value, or is the program just updated through the object. If the program is always updated through a function in the object, the way you change the value of the JLabel would be

score.setText(newValueHere)

so every time the score changed, you would write that command to change the label to the updated value. Is this what you're looking to do?

The user rolls a die and that value is added and stored into a "score" object. What I want to do is pass that score value from the "score" object over to this class, which is a panel that displays the score in text form on the GUI.

That way, every time the person rolls a die, the value is added to the running total. That total is displayed to the GUI.

What you want to do is write a setScore method in your ScorePanel class, and then after the score is changed in whatever class sets the score, call that method. Here, I am assuming that player.toString() returns the score. For example:

public void setScore(){
    scoreLabel.setText(player.toString());
}

After the score is changed in the player class, call this method to update your GUI.

Hopefully this will work for you. If not, let me know.

Yes, this is working for me. It clicked for me after reading your first reply. Thank you for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.