I know this seems like a question asked and answered time and again, But even after combing through the stack Overflow for hours i couldn't solve the problem. Sorry in advance if I'm missing something obvious.
I need to change a jLable's text each time a Thread starts, and again when that thread finishes. Simply, I'm trying to show the number of threads that are currently running.
jobQueueView
is a static and final jLabel. Main
is the jFrame which has the jLabel. jobQueue
is a static int.
At the start of each thread:
jobQueue += 1; refreshQueue();
At the end of each thread:
jobQueue -= 1;refreshQueue();
And finally
public void refreshQueue() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Main().jobQueueView.setText(Integer.toString(jobQueue));
}
});
}
This doesn't work. any ideas? Thanks
Edit : As per the instructions of Andrew Thompson Swing JLabel text change on the running application : on a button click event
Can I change the text of a label from a Thread in Java? : has to make the string final. I cant.
Update JLabel from another thread : Uses Timers, I need the thread count
JLabel on JPanel doesn't update when setText from another method : Tried the given solutions. Didn't work
Thread and JLabel in Swing- Not working properly : More button clicks but different solutions. Still didnt work