Hello, my GUI locks up because I need to update it through the EDT, however, I need to also pass a variable that is being updates with the GUI:
while ((message = this.in.readLine()).startsWith("NUMPLAYERS"))
{
numOfPlayers = Integer.parseInt(message.split(":")[1]);
numPlayers.setText("There are currently " + numOfPlayers + " players in this game");
}
This does not work. I need to set the text in the EDT but I cannot pass numOfPlayers to it without declaring it as final (which I don't want to do, because it changed as new players join the server)
-
The easiest solution would be to use a
finaltemporary variable:final int currentNumOfPlayers = numOfPlayers; EventQueue.invokeLater(new Runnable() { public void run() { numPlayers.setText("There are currently " + currentNumOfPlayers + " players in this game"); } });Tom Hawtin - tackline : In this case, it just needs the local variable to be defined at an appropriate point (and made final). -
You have to make it final or have the
Runnablereference a field (class varable). If referencing a field make sure that it's thread safe (via synchronized or volatile).Logan Serman : How would I reference the class variable? That would be ideal. -
How about this:
while ((message = this.in.readLine()).startsWith("NUMPLAYERS")) { numOfPlayers = Integer.parseInt(message.split(":")[1]); final newText = "There are currently " + numOfPlayers + " players in this game"; EventQueue.invokeLater(new Runnable() { public void run() { numPlayers.setText(newText); } }); }NOTE: I assume that the OP has a good reason for not marking
numOfPlayersas final, perhaps that it is changed later in the samewhileloop in code that's not relevant to the question, so not shown. And thus thatnumOfPlayersis declared before thewhileloop.Without this assumption, I wouldn't make the additional variable
newText.Tom Hawtin - tackline : Missing String before badly capitalised newtext. Probably can make numOfPlayers itself final after putting declaration in the correct place.Eddie : @Tom Hawtin: I updated my answer ... the OP says that he has a good reason for not making numOfPlayers final. I assume that this ties into why the OP's sample does not show the variable declaration. And because you complained, I changed the capitalization of newText. :) -
Define this class outside of your method:
public abstract class MyRunnable implements Runnable { protected int var; public MyRunnable (int var) { this.var = var; } } Now your code can look like this: SwingUtilities.invokeAndWait(new MyRunnable(5) { @Override public void run() { //numPlayers.setText("There are currently " + var + " players in this game"); } });(For the purposes of this example, I am assuming that there is a good reason why using a locally scoped final temp variable will not work. I honestly can't think of any reason for that restriction, though.)
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.