Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy isn't setText updating the JLabel?
    text
    copied!<p>I've checked on the other threads here and haven't found a solution. </p> <p>1) The JFrame is setVisible(true).</p> <p>2) What does this mean: "I wonder if your problem is a concurrency issue, that you are doing a long-running process on the Swing event thread and that this is preventing your label from updating its text." I read that somewhere else.</p> <p>3) I haven't initialized multiple times the JPanel that contains the label.</p> <p>EDIT: 4) updateTurn is called from the JPanel that contains <code>TrackingPanel</code> (i.e. <code>gamePanel</code>). I call the method <code>changeTurns();</code> and here's the code for that:</p> <pre><code>public void changeTurns() { if(turnPlayer == playerX) turnPlayer = playerO; else turnPlayer = playerX; trackingPanel.updateTurn(); } </code></pre> <p>Here's the relevant code in full:</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TrackingPanel extends JPanel{ /*TURN STUFF*/ private JPanel turnPanel; //turns panel to keep track of whose turn it is private JLabel turnLabel; private String turn; /*OTHER*/ private GamePanel gamePanel; public TrackingPanel( GamePanel gamePan ) { setLayout( new GridLayout(1,4) ); setBorder(BorderFactory.createMatteBorder(2,2,4,2,Color.BLACK)); gamePanel = gamePan; /*THIS PANEL DISPLAYS THE TEXT*/ turnPanel = new JPanel( new GridLayout(2,1) ); turn = gamePanel.getPlayerTurn().getLetter(); turnLabel = new JLabel(" Player " + turn + "'s turn"); add( turnPanel); }//end constructor /*THIS IS WHERE THINGS GO WRONG*/ public void updateTurn() { turn = gamePanel.getPlayerTurn().getLetter(); turnLabel.setText( " Player" + turn + "'s turn" ); System.out.println(turn); } } </code></pre> <p>Before <code>updateTurn()</code> is called, <code>turnLabel</code> says "PlayerX's turn". After, it should say "PlayerO's turn". By printing out <code>turn</code> (I get the string 'O', instead of 'X'), I know that whats being displayed ("PlayerX's turn") is not what should be displayed ("PlayerO's turn"). </p> <p>Thanks in advance you smartypants!</p> <p>EDIT. Tried giving SSCCE but don't know how to include image files. Sorry!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload