简体   繁体   English

如何从另一个JPanel / JFrame / etc添加String JList元素?

[英]How can I add String JList element from another JPanel/JFrame/etc?

I know how to actually add the element to the JList itself, but I am unsure how to make my Event Log (which uses the JList along with a DefaultListModel) update as the updates actually occur. 我知道如何将元素实际添加到JList本身,但是我不确定如何在更新实际发生时使我的事件日志(使用JList和DefaultListModel)更新。 It seems as though my application freezes up then all entries are shown when it's over. 似乎我的应用程序冻结了所有条目,当它结束时显示。 Please help? 请帮忙?

I'm 100% sure that you've got a Swing concurrency issue where you're tying up the Swing event dispatch thread or EDT with a long-running process. 我百分百肯定你有一个Swing并发问题,你正在用一个长时间运行的进程来绑定Swing事件调度线程或EDT。 The problem is that the Swing even thread is responsible for performing all Swing drawing and user interactions. 问题是Swing even线程负责执行所有Swing绘图和用户交互。 If it is tied up with some long running process such as file or database input or output, then it can't perform its responsibilities, and the whole GUI becomes frozen. 如果它与一些长时间运行的进程(如文件或数据库输入或输出)捆绑在一起,则它无法执行其职责,整个GUI将被冻结。 The solution is to use a background thread such as a SwingWorker, so that the long-running process doesn't tie up the EDT, but taking care to update Swing components such as the JList or its model on the EDT. 该解决方案是使用一个后台线程,如SwingWorker的,从而使长期运行过程中不占用的EDT,但照顾更新Swing组件,如JList的还是其 EDT模型。

Please check out the Swing concurrency tutorial for more details on the problem and its solution. 有关该问题及其解决方案的更多详细信息,请查看Swing并发教程

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

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