Ahmad Issawi
Ahmad Issawi

Reputation: 11

Java JTextField cannot be updated

when I run the following code, I have problem in the level of the TextField. It is not updated during the process. Only when all the calculations was made, it will be updated completely at once.

System.out.println("Start....!!");
MyJTextField.setText("Start....!!"); 

MyJTextField.setText("Result1 is calculated now….”); 
/* here : Connect a DataBase and Make Calculations of the Var : Result1 */
System.out.println(Result1); 

MyJTextField.setText("Result2 is calculated now….”); 
/* here : Connect a DataBase and Make Calculations of the Var : Result2 */
System.out.println(Result2); 

MyJTextField.setText("Result3 is calculated now….”); 
/* here : Connect a DataBase and Make Calculations of the Var : Result3 */
System.out.println(Result3); 

// and so ….

Running the code will make the following :

After that it updates MyJTextField completely at once.

Thanks a lot for any useful help.

Upvotes: 1

Views: 83

Answers (3)

camickr
camickr

Reputation: 324197

it will be updated completely at once.

All the code is executing on the Event Dispatch Thread (EDT), so the GUI can only repaint itself once the processing is finished.

If you have a long running task, then you need to execute that task on a separate Thread so you don't block he EDT and the GUI is able to repaint itself.

One way to do this is to use a SwingWorker. Read the section from the Swing tutorial on Concurrency for more information about the EDT and how to use a SwingWorker.

If you want to update the GUI while the database access is being done you can then "publish" the updates. This will make sure the GUI is updated on the EDT.

Upvotes: 0

Tommy Brettschneider
Tommy Brettschneider

Reputation: 1500

I'd call all Swing UI-related methods inside an anonymous java.lang.Runnable class and run it by passing it to SwingUtilities.invokeLater(Runnable runnable). This ensures that the UI operations called inside of the Runnable's run() method will be called on the EDT (event dispatching thread). Never run Swing operations on e.g. the same thread that does for example longer running computations. See code below...

// non-ui thread
System.out.println("Start....!!");

// call on ui-thread (event dispatching thread)
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        MyJTextField.setText("Start....!!"); 
        MyJTextField.setText("Result1 is calculated now….”); 
    }
}

// non ui-thread again...
/* here : Connect a DataBase and Make Calculations of the Var : Result1 */
System.out.println(Result1); 

// call on ui-thread again (event dispatching thread)
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        MyJTextField.setText("Result2 is calculated now….”); 
    }
}

// and so ….

shorter version using lambdas (Java8+):

SwingUtilities.invokeLater(() -> MyJTextField.setText("Result2 is calculated now….”));

Upvotes: 2

Kapcash
Kapcash

Reputation: 6919

If you're using Java swing library, then the UI is rendered by a specific thread, meaning a thread update the UI at a different time the calculation are made from the main thread.

You should try to use the validate() or repaint() methods over Swing components to update them properly as:

MyJTextField.setText("Result1 is calculated now….”); 
MyJTextField.validate();
// or MyJTextField.repaint();

Upvotes: 0

Related Questions