简体   繁体   中英

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 doesn't play nice with the programmer handling their own asynchronous operations.

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".

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.

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").


Questions

  • Is there conventional method for wrapping a multi-threaded application of this nature in Java Swing?

  • Does that method allow for passive viewing of a changing model? (no UI mouse or keyboard events, but it still updates)

  • 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. Start the process in your implementation of doInBackground() . Give the worker a reference to the listener that will receive "asynchronous update events" from the process. In the listener, invoke publish() as warranted to allow "passive viewing of a changing model." Your implementation of process() will be called on the EDT at a sustainable rate . Several examples may be seen here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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