简体   繁体   English

Java JTextField无法更新

[英]Java JTextField cannot be updated

when I run the following code, I have problem in the level of the TextField . 当我运行以下代码时,我在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 : 运行代码将执行以下操作:

  • Prints in the consol : Start….!! 在控制台上打印:开始…。!!
  • Calculates Result1 计算结果1
  • Prints in the consol the value of Result1 在控制台中打印Result1的值
  • Calculates Result2 计算结果2
  • Prints in the console the value of Result2 在控制台中打印Result2的值
  • Calculates Result3 计算结果3
  • Prints in the console the value of Result3 在控制台中打印Result3的值

After that it updates MyJTextField completely at once. 之后,它将MyJTextField完全更新MyJTextField

Thanks a lot for any useful help. 非常感谢您提供的有用帮助。

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) . 我将在匿名java.lang.Runnable类中调用所有与Swing UI相关的方法,并将其传递给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). 这样可以确保在EDT(事件分派线程)上调用Runnable的run()方法内部调用的UI操作。 Never run Swing operations on eg the same thread that does for example longer running computations. 切勿在例如执行更长时间的计算的同一线程上运行Swing操作。 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+): 使用lambdas(Java8 +)的较短版本:

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

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. 如果您使用的是Java swing库,则UI是由特定线程呈现的,这意味着线程会在从主线程进行计算的不同时间更新UI。

You should try to use the validate() or repaint() methods over Swing components to update them properly as: 您应该尝试在Swing组件上使用validate()repaint()方法来正确更新它们,如下所示:

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

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. 所有代码都在Event Dispatch Thread (EDT) ,因此GUI仅在处理完成后才能重新绘制自身。

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. 如果您的任务运行时间较长,则需要在单独的Thread上执行该任务,以免阻塞EDT ,并且GUI能够重新绘制自身。

One way to do this is to use a SwingWorker . 一种方法是使用SwingWorker Read the section from the Swing tutorial on Concurrency for more information about the EDT and how to use a SwingWorker . 阅读Swing 并发教程中的这一节,以获取有关EDT以及如何使用SwingWorker更多信息。

If you want to update the GUI while the database access is being done you can then "publish" the updates. 如果要在完成数据库访问时更新GUI,则可以“发布”更新。 This will make sure the GUI is updated on the EDT. 这将确保在EDT上更新GUI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM