问题
I would like to create a new event-dispatch thread in Swing, and I'm having trouble finding any references online for how to do this. I have done this in .NET by creating a new thread and calling Application.run(...). Has anyone done this? Is it possible in Swing?
FYI the reason I am trying to do this is because I am writing an Eclipse plug-in, and I would like to pop up dialogs that are not modal to the IDE but that are modal (blocking) to my UI logic. I could accomplish this using non-modal dialogs and callbacks, but that requires the overhead of making my code multi-threaded. I'll revert to that if the former is not possible.
回答1:
Yes, it's possible. I've have done such multiple EDT dispatch threads logic in Swing. However, net result was that it didn't work reliably.
(a) All JVMs don't work nicely with multiple EDT threads (synchronization problems in graphics rendering logic in native code and such, IBM JVM failed with multiple EDT threads, Sun JVM & Apple JVM did work)
(b) Swing rendering logic has few bugs causing that random rendering errors will occur (for example, http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6727829).
Anyway, doing this requires basically establishing two AppContexts, which each have their own EDT thread.
回答2:
I'm a little confused by your question, because you mention Swing but then say you are writing an Eclipse plugin. Since the question is tagged Swing, I'll give a Swing answer (but posted as CW).
There is one event dispatch thread. There is always one event dispatch thread, unless there is none at all. You cannot create another one.
You can, however, change the ModalityType of your dialogs, or change the ModalExclusionType of a window. In this case, if you were writing this all yourself, you would set your top-level window's ModalExclusionType
to APPLICATION_EXCLUDE.
But again, I don't see how this could help you, since Eclipse uses SWT instead of Swing.
回答3:
I'm going to junk my last answer and start anew.
In SWT, you can create Shells (windows) or custom Dialogs that are modal just to the parent by passing the SWT.PRIMARY_MODAL style flag during creation.
Note that Dialog
is an abstract class, so you'd have to create your own. It's probably just easier to use Shell
.
Edit:
Why SWT? Because that's what Eclipse uses. See: Eclipse Platform Plug-in Developer Guide (zipped PDF) for more details. The most recent version is available in Eclipse's Help system (Help > Help Contents > Plug-in Development Environment Guide.)
来源:https://stackoverflow.com/questions/859274/multiple-swing-event-dispatch-threads