Melvin
Melvin

Reputation: 377

How to - Java Thread Accessing Other Thread (SWING)

Regarding the problem,

when i start a thread from the main application, lets say process A runs in background and updates a text box outside of the thread saying it's running and completed when it has stop running.

How do i write the code which updates the textbox outside the thread from within the running thread ?

Upvotes: 2

Views: 423

Answers (1)

galchen
galchen

Reputation: 5290

you need a pointer to the text box. the thread cannot update the text box - in swing, all GUI commands must be executed from the event dispatch thread.

you must do:

SwingUtilities.invokeLater ( new Runnable(){
    public void run(){
        // draw textbox code
    }
});

this will add the object to the queue executed by the event dispatch thread

EDIT: just a tip: inside the new Runnable(){ ... }, which is an anonymous class, you won't be able to use a regular variable from the outside scope. You must define a variable as final, or use a getter to fetch the textbox (or address it through some static field).

Upvotes: 3

Related Questions