At 08:32 PM 3/9/2001 +0100, Perico wrote:
>I have a method that shows a modal dialog asking the user for
>certain information when he/she double clicks in a window.
>
>The problem is that if the user clicks so fast that
>he/she produces two consecutives double clicks, the method is
>invoked twice, showing two modal dialogs at the same time (one
>above the another one) instead of only one.
>
>I've checked that since the method is executing in the event
>dispatching thread (since it is called when a double click is
>detected), the method is called twice (the second time is called
>before the first call is finished -I don't understand why this
>happens, since, altough it's the event dispatching thread, I
>think it has to call the methods in sequence-) at the same time.
>
>I have to avoid this wrong behaviour. I've tried to define the method
>as synchronized, but this doesn't work since the two calls are executing
>(simultaneously!!!) in the same thread, so no mutual exclusion is
>guaranteed with synchronized (this modifier only guarantees mutual
>exclusion between different threads -anyway, I couldn't imagine before a
>thread executing twice the same method at the same time-).
>
>I hope you can help me, because I have to solve this problem and I don't
>even understand what is happen.
You're correct that the EDT must execute its operations sequentially.
If it didn't, it wouldn't be a single thread.
Note that the EDT pulls things out of an EventQueue, one at a time,
and executes them. Things go -into- the EventQueue from all over the
place. By that, I mean any number of other threads.
This includes whatever native code detects mouse events, such as
clicks on a particular button. Apparently a second one can make it
into the event queue before the EDT is able to process the first one,
which doesn't surprise me at all.
Assuming that your application is designed to have only one of these
dialogs up at all, ever, the simplest trick is to have your frame (or
button or whatever's handling the dialog) store this dialog as a
field, and when that button is clicked, show the dialog.
If multiple dialogs are allowed (for instance, you can click the
button, see the dialog, move it to one side and then click the button
again to get another dialog), you'll need a bit more thought on
usability. I might do something like this whenever the button is
clicked:
1) Immediately disable the button.
2) Build and show the dialog.
3) Enable the button.
The trick here is that building and showing a dialog is often a slow
enough operation that a quick user can get multiple clicks in, but
a button-disable operation tends to be too fast to allow this.
Normally I'd just give you the fourth paragraph above, but you seemed
interested in the mechanics, so there ya are. :-)
_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing