Sudh33ra
Sudh33ra

Reputation: 2765

How can i make the JLabel change through different threads without a final String?

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

Upvotes: 0

Views: 450

Answers (3)

Sudh33ra
Sudh33ra

Reputation: 2765

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);

Upvotes: 0

mfidan
mfidan

Reputation: 647

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 ));

Upvotes: 2

Sanjeev
Sanjeev

Reputation: 9946

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.

Upvotes: 1

Related Questions