Hi Chris,
The problem you are seeing is because JOptionPane.showInputDialog is
being called from a thread other than the AWT event thread. In this
case, you are making the call instead from the SWT event thread. (Since
the main menu is an SWT menu, the listener is called by SWT from within
its event handling loop.)
To avoid deadlocks and other problems, it's important that most calls to
the AWT/Swing APIs be made from within the AWT event thread. This is
actually a general rule, not just for the case of mixing SWT and Swing.
It's called the single-thread rule. More info:
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html.
(SWT also has this rule and enforces it much more strictly.)
To solve the problem, you just need to transfer control to the AWT event
thread and show the dialog from there. Since you're using article code,
the easiest way to do this, is to change the tryme() method to:
public void tryMe(){
AwtEnvironment.getInstance(display).invokeAndBlockSwt(new
Runnable() {
public void run() {
JOptionPane.showInputDialog("after");
}
});
}
You can also use AWT APIs directly:
public void tryMe(){
EventQueue.invokeLater(new Runnable() {
public void run() {
JOptionPane.showInputDialog("after");
}
});}
However, this code does not block SWT during the brief period between
the call to invokeLater and the eventual call to showInputDialog on the
AWT thread. So the first example is preferable.
Cklewis wrote:
Here is the example (attached),
click on 'file' -> 'try me' to get the 2nd JOptionPane to come up...
this way the gui and embedded swing composite are already loaded when
it comes up.
You also need the piccolo library to get this project to work.
downloadable at:
http://www.cs.umd.edu/hcil/jazz/download/piccolo/piccolo-1.2.zip
you can add the piccolo jars or put the piccolo portion as a project
and link via a buildpath.
Gordon Hirsch wrote:
If you make your example available, I can take a look. There's no
specific problem that I know of with JOptionPane, and I have
successfully used JOptionPane dialogs in the past.
Cklewis wrote:
I just wrote a seperate program to test if the error was still
happening and it is.
Here is exactly what i'm doing.
1)Create a Shell, set layout etc
2)Try JOptionPane.showInputDialog (works fine)
3)Create swing component (a piccolo "canvas", which is an extended
JComponent)
4)Add my EmbeddedSwingComposite
5)Let the GUI load
6)Click on "file" menu and click on an item I have preset with an
action lisener to call a function that loads another
JOptionPane.showInputDialog
The result is the first JoptionPane is fine, but the second one
freezes the entire program as soon as I click a button.
If you would like, I could upload the project so you can try it.
It's just one file along with the folder with all the swing
integration stuff from Gordon's example.
James Peltzer wrote:
You may need to provide a little more information. I know that I have
successfully used JOptionPane and JDialogs both before and after
creating
EmbeddedSwingComposites. The code I am working from is modified from
Gordon's original article code.
Originally I was often having this problem when an invisible dialog
was
added to the SwtInputBlocker... but I believe the fix for that one
has since
been applied to the sample code.
I would also suspect that you are getting pop-under windows (your
swing
window could be trapped behind your SWT Window) but it is hard to
say. You
could also try the changes Gordon and I discuss in the September
19th post
to this group.
In short, I would hazard a guess that compatibility issues such as
this are
vital to the project and will eventually be worked out.
-James
"Cklewis" <chriskwonlewis@xxxxxxx> wrote in message
news:fgg8p4$5ko$1@xxxxxxxxxxxxxxxxxxxx
Will there be a fix for letting you use JOptionPane and
JDialogs(etc) ?
I noticed if I use JOptionPane BEFORE creating my
EmbeddedSwingComposite
it works just fine. But if I attempt to use JOptionPane or a JDialog
after I've created the EmbeddedSwingComposite that it will
display, but
it tends to freeze the whole program.
Not sure if you were aware of this problem, but it would be nice
to be
able to use JOptionPane, and in my case a JDialog since the person
before me wrote a lot of code for that particular section which
uses a
JDialog and JTrees. Initially i thought the problem was setting the
JDialog's frame owner to the awt frame in EmbeddedSwingComposite.
But I
experimented with a regular JOptionPane(doesn't ask for the parent
component/frame) and noticed the same problem.