views:

621

answers:

3

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.

+3  A: 

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.

Michael Myers
It seems that SWT uses a similar modality system, so this could end up putting you on the right track. But I'm not familiar enough with it to explain in detail.
Michael Myers
Actually the PlugIn and WebStart have multiple EDTs. However, the API is not public and you can't mix components together.
Tom Hawtin - tackline
I have to admit I actually didn't realize that Eclipse is written in SWT, though that makes sense. I'm relatively new to Java, with my background being primarily .NET. I do know however that I can in fact create and show Swing dialogs from my Eclipse plug-in.
dcstraw
I don't have a whole lot of Swing code, so I could potentially convert to SWT. However I don't think that would help because I don't think I can set the IDE to exclude modality, nor would that likely be a good idea.
dcstraw
+2  A: 

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

R. Bemrose
Thanks for the suggestion. It didn't do what I need however. I would need a modality type that is essentially modalless, even to the direct parent, while still blocking on the call to open().
dcstraw
+2  A: 

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.