简体   繁体   English

Java Swing-UI块

[英]Java Swing - UI Block

I have a GUI and on click event, I am calling database queries to generate multiple charts. 我有一个GUI,并且在单击事件时,我正在调用数据库查询以生成多个图表。 I cannot see any chart until all the charts are get completed means there is some problem with GUI components which moves to the next chart before the first chart get complered and show it's contents. 在所有图表都完成之前,我看不到任何图表,这意味着GUI组件存在一些问题,该问题会在编译第一个图表并显示其内容之前移至下一个图表。 What could be the possible reason and how to avoid such problem ? 可能是什么原因?如何避免这种问题?

There is no problem with the components. 组件没有问题。 There is a problem with your design. 您的设计有问题。 You're doing the long-running queries and the updates of the charts in a single method run in the event dispatch thread. 您正在通过事件分发线程中运行的单个方法来进行长时间运行的查询和图表的更新。 By doing so, the EDT is completely blocked and can't repaint anything until all the queries have been run and all the charts have been generated. 这样,EDT将被完全阻止,并且在运行所有查询并生成所有图表之前,无法重绘任何内容。

Use a background thread to execute the queries, and generate a chart, in the EDT, each time one query is finished. 每次完成一个查询时,就使用背景线程在EDT中执行查询并生成图表。 The SwingWorker class helps doing that. SwingWorker类有助于实现这一点。 Read its API doc , and the tutorial about concurrency in Swing. 阅读其API文档 ,以及有关Swing中的并发性的教程

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

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