0

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

Community
  • 1
  • 1
Sudh33ra
  • 2,675
  • 3
  • 15
  • 19
  • http://stackoverflow.com/questions/6578205/swing-jlabel-text-change-on-the-running-application : on a button click event http://stackoverflow.com/questions/26495337/can-i-change-the-text-of-a-label-from-a-thread-in-java : has to make the string final. I cant. http://stackoverflow.com/questions/5895481/update-jlabel-from-another-thread?rq=1 : Uses Timers, I need the thread count @Andrew Thompson These are the ones i have open now but i tried more – Sudh33ra Jul 11 '16 at 11:54
  • @AndrewThompson 5 of 5. Thanks for your help – Sudh33ra Jul 11 '16 at 12:06
  • *"5 of 5."* Good question. ..sorry, I don't know the answer, but hopefully now the info. is included, it can give people who **can** answer, the context they need in order to do so. – Andrew Thompson Jul 11 '16 at 12:12

3 Answers3

2

It seems you are creating a new Frame every time.

new Main().jobQueueView.setText(Integer.toString(jobQueueCount ));

So you have multiple frames but one static label. This may cause the problem. Access the jobQueueView through a static way like below.

Main.jobQueueView.setText(Integer.toString(jobQueueCount ));

mfidan
  • 647
  • 5
  • 19
  • As per OP "jobQueueView is a static and final jLabel" so how many instances of Main it is creating `jobQueueView` will still be same and will be updated rightly. I agree it should have been used in a static way though – Sanjeev Jul 11 '16 at 14:20
  • a single label can only be added to a single parent container. this may cause update problem. – mfidan Jul 11 '16 at 14:22
  • that can only be clarified, if OP posts full code. might be there is an init method that is called separate from constructor and jlabel got added only once. – Sanjeev Jul 11 '16 at 14:25
  • @mfidan This worked, combined with sanjeev's answer below :D Thank you. The problem was me trying to add the same label to the same parent again and again i suppose like you said. Thanks everyone.Ill post the complete code as a separate answer. – Sudh33ra Jul 12 '16 at 13:40
1

So your problem is, Only final variables are accessible in Anonymous Inner classes from outer classes

So in order to make your code work

   public void refreshQueue(int jobQueue) {
        final int jobQueueCount = jobQueue;
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().jobQueueView.setText(Integer.toString(jobQueueCount ));
            }
        });

    }

And use it by

jobQueue += 1; refreshQueue(jobQueue);

And

jobQueue -= 1; refreshQueue(jobQueue);

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
0

The two answers given solved the problem. Here's the final code.

public void refreshQueue(int jobQueue) {
        final int jobQueueCount = jobQueue;
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
               Main.jobQueueView.setText(Integer.toString(jobQueueCount ));
            }
        });

    }

At the start of the thread

jobQueue += 1; refreshQueue(jobQueue);

And at the end of it

jobQueue -= 1; refreshQueue(jobQueue);
Sudh33ra
  • 2,675
  • 3
  • 15
  • 19