简体   繁体   English

在Java Swing中为多线程应用程序创建可视化

[英]Creating a visualization for a multi-threaded application in Java Swing

I'm running into the problem I've always had with Swing time and time again. 我遇到了Swing一次又一次遇到的问题。 Swing doesn't play nice with the programmer handling their own asynchronous operations. 对于处理自己的异步操作的程序员来说,Swing并不是很好。

In front of me now, I have the quintessential problem that is often blasted with answers of the sort "Have Swing's EDT handle async tasks" or "Use invoke later". 现在,在我面前,我有一个典型的问题,经常会被“让Swing的EDT处理异步任务”或“稍后使用调用”这类答案所引发。

Normally, that would be the way to go if I was building the entire application from the ground up. 通常,如果我从头开始构建整个应用程序,那将是一种方法。 However, I'm not. 但是,我不是。 What I currently have is a program (that I should not modify in any way) that runs in its own thread and fires off asynchronous update events of its own design. 我目前拥有的是一个程序(我不应该以任何方式修改)在自己的线程中运行并触发其自己设计的异步更新事件。 I want to use Swing to make a UI wrapper for it, to display the application in a panel while providing buttons to interact with the underlying program. 我想使用Swing为它创建一个UI包装器,在面板中显示应用程序,同时提供与底层程序交互的按钮。

Now, I don't really want to lose my hair over this. 现在,我真的不想失去我的头发。 I'm not going to tackle this problem head-on for hours at a time with no progress. 我不打算在没有进展的情况下正面解决这个问题几个小时。 If an acceptable answer to this question doesn't arise, I will simply use an OpenGL wrapper (that's right "simply"). 如果没有出现这个问题的可接受的答案,我将简单地使用一个OpenGL包装器(这是“简单”)。


Questions 问题

  • Is there conventional method for wrapping a multi-threaded application of this nature in Java Swing? 是否有传统的方法在Java Swing中包装这种性质的多线程应用程序?

  • Does that method allow for passive viewing of a changing model? 该方法是否允许被动查看不断变化的模型? (no UI mouse or keyboard events, but it still updates) (没有UI鼠标或键盘事件,但它仍然更新)

  • If there isn't a conventional way, is there a hack, bodge, or otherwise duct tape solution to what I'm trying to do? 如果没有传统的方式,是否有黑客,躲避或其他胶带解决方案,我正在尝试做什么?


Answers which fail to identify that the asynchronous portion of the application is in another program and suggest modifying that program will be deemed nonconstructive and will be flagged as off-topic. 无法识别应用程序的异步部分在另一个程序中并建议修改该程序的答案将被视为非建设性的,并将被标记为偏离主题。

If you need additional information, make a comment and I'll disclose as much as possible. 如果您需要其他信息,请发表评论,我会尽可能地披露。

Use SwingWorker for this. 使用SwingWorker进行此操作。 Start the process in your implementation of doInBackground() . doInBackground()实现中启动该过程。 Give the worker a reference to the listener that will receive "asynchronous update events" from the process. 为worker提供对将从进程接收“异步更新事件”的侦听器的引用。 In the listener, invoke publish() as warranted to allow "passive viewing of a changing model." 在监听器中,调用publish()作为保证允许“被动查看更改的模型”。 Your implementation of process() will be called on the EDT at a sustainable rate . 您将以可持续的速度在EDT上调用您的process() 实现 Several examples may be seen here . 这里可以看到几个例子。

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

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