-1

I have the class GridButtonPanel such here is and I have a JLabel like this in createNavPanel void:

public JPanel box;         
public JLabel cpLabel; 


n = new JPanel(new BorderLayout());
box = new JPanel();
box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
cpLabel = gui.currentPlayerLabel(game.currentPlayer);
box.add(cpLabel);
n.add(box, BorderLayout.WEST);

Of course I have some other thing here too... The gui.currentPlayer is this:

JLabel currentPlayerLabel(int cp) {
    JLabel l = new JLabel();
    switch(cp){
        case 1 : l.setText("Current Player: Player 1");
                 l.setForeground(Color.green);
                 break;
        case 2 : l.setText("Current Player: Player 2");
                 l.setForeground(Color.blue);
                 break;
    }
    return l;
}

And I add this all things to main JFrame like this: f.add(createNavPanel(), BorderLayout.NORTH);

And I have an other class, the gui. Here I make some things to work, and I want to update the cpLabel. I tried like this:

    private void changePlayerLabel(){
    int cp = game.currentPlayer;
    switch(cp){
        case 1 : 
                 gpanel.cpLabel.setText("Current Player: Player 1");
                 gpanel.cpLabel.setForeground(Color.green);
                 break;
        case 2 : gpanel.cpLabel.setText("Current Player: Player 2");
                 gpanel.cpLabel.setForeground(Color.blue);
                 break;
    }
}

But this isn't working in this case. How can I do this?

korte alma
  • 59
  • 6
  • *"Of course I have some other thing here too..."* For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 14 '18 at 15:08
  • @AndrewThompson this isn't minimal or complete? I think I wrote all what is relevant here. This is why I didn't copy-pasted the whole code, to be more specific. – korte alma Jun 14 '18 at 15:14
  • *".. or complete?"* Can you copy/paste it into a new project, compile it (with **no** changes) & run it to see the problem? – Andrew Thompson Jun 14 '18 at 15:21
  • 1
    @kortealma the code you've posted give us no hint about, where is your problem. To provide an answer to your question we need to reproduce your wrong behavior. So please write a [mcve] and we can try to solve your issue. – Sergiy Medvynskyy Jun 14 '18 at 15:22
  • *"I think I wrote all what is relevant here."* Relevant to make it easy for others to help (for free)? You need to realize that whereas you have a keen interest in solving this, our interest is 'purely academic'. If you cannot post a code that makes it easy to help, there are plenty of other people asking for help who can. Where do you think we're going to focus our effort? – Andrew Thompson Jun 14 '18 at 15:23
  • @AndrewThompson The problem is, this didn't change the `JLabel` – korte alma Jun 14 '18 at 15:30
  • *"..this didn't change the `JLabel`"* Why did you expect an MCVE / SSCCE to change the label? **Make one & I'll look more closely at the problem.** – Andrew Thompson Jun 14 '18 at 15:33
  • @SergiyMedvynskyy I edited my question with some more code snippet. – korte alma Jun 14 '18 at 15:36
  • 1
    *"with some more code snippet"* You were asked 3 times for a [mcve] by @AndrewThompson, what part of it don't you understand? A snippet isn't a complete code, we need a COMPLETE example that FULLY demonstrates your issue... and please [edit] your question instead of adding answers as edits to it. – Frakcool Jun 14 '18 at 17:12
  • 1
    Look at [this answer](https://stackoverflow.com/questions/50801887/deleting-a-line-in-java-when-using-paintgraphics-p/50802633#50802633) to guide you in doing a MCVE, it must contain a `main` method, the imports, etc. We should be able to copy-paste it and see the same as you. – Frakcool Jun 14 '18 at 17:17
  • 1
    @Frakcool *"Look at this answer to guide you.."* Yes. That was a classic MCVE. (I just regret already having up voted it, since seeing again, I want to up vote it again!) – Andrew Thompson Jun 14 '18 at 17:24
  • 1
    @AndrewThompson well, you can offer a bounty of +50 on the next 5 times you use it as an example and it will be as if you upvoted it 5 times ;) jk. I think an example is much better when OP doesn't understand what a MCVE looks like :) – Frakcool Jun 14 '18 at 17:29
  • @Frakcool The complete code is more than 400 rows, as I think no one want to read, understand it, but as you see, I found the solution, I resolved the problem, and I added the solution to my question, as you see there already the good version is. I don't understand what is your problem, I added the minimal example, and in point of view of the problem also the complete version as I think. Thank you for the answers, and for help and your time, but as you see the problem is solved. – korte alma Jun 14 '18 at 17:32
  • Glad you solved it, but for future questions you can always use that answer as a guide. We don't want your whole current code but a nice new little one that we can use as an example. Isolate the issue. You create a class with a JButton and a JLabel in another class, you link them and try to update the JLabel based on how many times you clicked the button or something like that. BTW don't forget to accept your answer then – Frakcool Jun 14 '18 at 17:34

1 Answers1

1

Here isn't a solution to create a new JLabel and add it to the old one. Change it directly, like this:

gpanel.cpLabel.setText("Some label text");

and not like this:

 JLabel l = new JLabel();
 l.setText("Some label text");
 gpanel.cpLabel = l;
korte alma
  • 59
  • 6