Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Threads and modal dialog behaviour question

Reply
Thread Tools

Threads and modal dialog behaviour question

 
 
=?ISO-8859-1?Q?Aloys_Oberth=FCr?=
Guest
Posts: n/a
 
      12-07-2004
I have a question on modal dialogs in (non-event-dispatching)Threads. I
do set a flag in the actionPerformed() method of a modal dialog and the
object which displayed the dialog in the first place can question this
flag after "returning" from show(). I would have expected this to be not
timing dependant, but I see it is not which I do not understand


that's the dialog in essence:

class ModalerDialog extends JDialog implements ActionListener {
boolean flagSuccessful = false;
....

public void actionPerformed(ActionEvent aE) {
String cmd = aEvt.getActionCommand();

if(cmd.equals("one")) {
flagSuccessful = true;
this.setVisible(false);
}
else if(cmd.equals("two")) {
flagSuccessful = false;
this.setVisible(false);
}
}

public boolean isSuccessful() {
return flagSuccessful;
}
}


and that is the Thread launched within the actionPerformed()-method of a
menu ActionListener (see // comments)

Thread t = new Thread() {
public void run() {

ModalerDialog md = new ModalerDialog(owner, true);
md.show();


boolean b = md.isSuccessful(); // now on "one" false
try {
Thread.sleep(250);
}
catch (InterruptedException e1) {}
b = = md.isSuccessful(); // and now on "one" true ????

if(md.isSuccessful())
md.dispose();
else {
md.dispose();
owner.showStartupDialog();
}
}
};
t.start();

Who eplain that to me?
Thanks, Aloys
 
Reply With Quote
 
 
 
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      12-07-2004
Aloys Oberthür wrote:
> I have a question on modal dialogs in (non-event-dispatching)Threads. I
> do set a flag in the actionPerformed() method of a modal dialog and the
> object which displayed the dialog in the first place can question this
> flag after "returning" from show(). I would have expected this to be not
> timing dependant, but I see it is not which I do not understand
>
>
> that's the dialog in essence:
>
> class ModalerDialog extends JDialog implements ActionListener {
> boolean flagSuccessful = false;
> ...
>
> public void actionPerformed(ActionEvent aE) {
> String cmd = aEvt.getActionCommand();
>
> if(cmd.equals("one")) {
> flagSuccessful = true;
> this.setVisible(false);
> }
> else if(cmd.equals("two")) {
> flagSuccessful = false;
> this.setVisible(false);
> }
> }
>
> public boolean isSuccessful() {
> return flagSuccessful;
> }
> }
>
>
> and that is the Thread launched within the actionPerformed()-method of a
> menu ActionListener (see // comments)
>
> Thread t = new Thread() {
> public void run() {
>
> ModalerDialog md = new ModalerDialog(owner, true);
> md.show();


If "md" is really a modal dialog, I would expect this thread to stop
right here, and continue on to the next line only after the dialog has
been hidden / disposed off. That's how modal dialogs are expected to
behave.

>
> boolean b = md.isSuccessful(); // now on "one" false
> try {
> Thread.sleep(250);
> }
> catch (InterruptedException e1) {}
> b = = md.isSuccessful(); // and now on "one" true ????
>
> if(md.isSuccessful())
> md.dispose();
> else {
> md.dispose();
> owner.showStartupDialog();
> }
> }
> };
> t.start();
>


Couldn't understand what your comments meant either.

BK
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Aloys_Oberth=FCr?=
Guest
Posts: n/a
 
      12-08-2004
Babu Kalakrishnan wrote:
> Aloys Oberthür wrote:
>
>> I have a question on modal dialogs in (non-event-dispatching)Threads.
>> I do set a flag in the actionPerformed() method of a modal dialog and
>> the object which displayed the dialog in the first place can question
>> this flag after "returning" from show(). I would have expected this to
>> be not timing dependant, but I see it is not which I do not understand
>>
>>
>> that's the dialog in essence:
>>
>> class ModalerDialog extends JDialog implements ActionListener {
>> boolean flagSuccessful = false;
>> ...
>>
>> public void actionPerformed(ActionEvent aE) {
>> String cmd = aEvt.getActionCommand();
>>
>> if(cmd.equals("one")) {
>> flagSuccessful = true;
>> this.setVisible(false);
>> }
>> else if(cmd.equals("two")) {
>> flagSuccessful = false;
>> this.setVisible(false);
>> }
>> }
>>
>> public boolean isSuccessful() {
>> return flagSuccessful;
>> }
>> }
>>
>>
>> and that is the Thread launched within the actionPerformed()-method of
>> a menu ActionListener (see // comments)
>>
>> Thread t = new Thread() {
>> public void run() {
>>
>> ModalerDialog md = new ModalerDialog(owner, true);
>> md.show();

>
>
> If "md" is really a modal dialog, I would expect this thread to stop
> right here, and continue on to the next line only after the dialog has
> been hidden / disposed off. That's how modal dialogs are expected to
> behave.
>
>>
>> boolean b = md.isSuccessful(); // now on "one" false
>> try {
>> Thread.sleep(250);
>> }
>> catch (InterruptedException e1) {}
>> b = = md.isSuccessful(); // and now on "one" true ????
>>
>> if(md.isSuccessful())
>> md.dispose();
>> else {
>> md.dispose();
>> owner.showStartupDialog();
>> }
>> }
>> };
>> t.start();
>>

>
> Couldn't understand what your comments meant either.
>
> BK

It is true, that the Thread stops and displays the modal dialog. But
although I first set the flag within the dialogs actionPerformed method
and then set the dialog to not visible I get two results in the calling
Thread depending on when I invove md.isSuccessful().


The comments referred to the actionCommand, I meant that the command
"one" is the one, where successful is set to true in the
actionPerformed() method above.

Aloys
 
Reply With Quote
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      12-08-2004
Aloys Oberthür wrote:
> Babu Kalakrishnan wrote:
>
>> Aloys Oberthür wrote:
>>
>>> I have a question on modal dialogs in (non-event-dispatching)Threads.
>>> I do set a flag in the actionPerformed() method of a modal dialog and
>>> the object which displayed the dialog in the first place can question
>>> this flag after "returning" from show(). I would have expected this
>>> to be not timing dependant, but I see it is not which I do not
>>> understand
>>>
>>>
>>> that's the dialog in essence:
>>>
>>> class ModalerDialog extends JDialog implements ActionListener {
>>> boolean flagSuccessful = false;
>>> ...
>>>
>>> public void actionPerformed(ActionEvent aE) {
>>> String cmd = aEvt.getActionCommand();
>>>
>>> if(cmd.equals("one")) {
>>> flagSuccessful = true;
>>> this.setVisible(false);
>>> }
>>> else if(cmd.equals("two")) {
>>> flagSuccessful = false;
>>> this.setVisible(false);
>>> }
>>> }
>>>
>>> public boolean isSuccessful() {
>>> return flagSuccessful;
>>> }
>>> }
>>>
>>>
>>> and that is the Thread launched within the actionPerformed()-method
>>> of a menu ActionListener (see // comments)
>>>
>>> Thread t = new Thread() {
>>> public void run() {
>>>
>>> ModalerDialog md = new ModalerDialog(owner, true);
>>> md.show();

>>
>>
>>
>> If "md" is really a modal dialog, I would expect this thread to stop
>> right here, and continue on to the next line only after the dialog has
>> been hidden / disposed off. That's how modal dialogs are expected to
>> behave.
>>
>>>
>>> boolean b = md.isSuccessful(); // now on "one" false
>>> try {
>>> Thread.sleep(250);
>>> }
>>> catch (InterruptedException e1) {}
>>> b = = md.isSuccessful(); // and now on "one" true ????
>>>
>>> if(md.isSuccessful())
>>> md.dispose();
>>> else {
>>> md.dispose();
>>> owner.showStartupDialog();
>>> }
>>> }
>>> };
>>> t.start();
>>>

>>
>> Couldn't understand what your comments meant either.
>>

>
> It is true, that the Thread stops and displays the modal dialog. But
> although I first set the flag within the dialogs actionPerformed method
> and then set the dialog to not visible I get two results in the calling
> Thread depending on when I invove md.isSuccessful().
>
>
> The comments referred to the actionCommand, I meant that the command
> "one" is the one, where successful is set to true in the
> actionPerformed() method above.
>


OK - Check if the problem goes away if the variable flagSuccesful is
declared to be "volatile". (Or alternately declare the isSuccessful
method as synchronized).

BK


 
Reply With Quote
 
=?ISO-8859-1?Q?Aloys_Oberth=FCr?=
Guest
Posts: n/a
 
      12-09-2004
Babu Kalakrishnan wrote:
> Aloys Oberthür wrote:
>
>> Babu Kalakrishnan wrote:
>>
>>> Aloys Oberthür wrote:
>>>
>>>> I have a question on modal dialogs in
>>>> (non-event-dispatching)Threads. I do set a flag in the
>>>> actionPerformed() method of a modal dialog and the object which
>>>> displayed the dialog in the first place can question this flag after
>>>> "returning" from show(). I would have expected this to be not timing
>>>> dependant, but I see it is not which I do not understand
>>>>
>>>>
>>>> that's the dialog in essence:
>>>>
>>>> class ModalerDialog extends JDialog implements ActionListener {
>>>> boolean flagSuccessful = false;
>>>> ...
>>>>
>>>> public void actionPerformed(ActionEvent aE) {
>>>> String cmd = aEvt.getActionCommand();
>>>>
>>>> if(cmd.equals("one")) {
>>>> flagSuccessful = true;
>>>> this.setVisible(false);
>>>> }
>>>> else if(cmd.equals("two")) {
>>>> flagSuccessful = false;
>>>> this.setVisible(false);
>>>> }
>>>> }
>>>>
>>>> public boolean isSuccessful() {
>>>> return flagSuccessful;
>>>> }
>>>> }
>>>>
>>>>
>>>> and that is the Thread launched within the actionPerformed()-method
>>>> of a menu ActionListener (see // comments)
>>>>
>>>> Thread t = new Thread() {
>>>> public void run() {
>>>>
>>>> ModalerDialog md = new ModalerDialog(owner, true);
>>>> md.show();
>>>
>>>
>>>
>>>
>>> If "md" is really a modal dialog, I would expect this thread to stop
>>> right here, and continue on to the next line only after the dialog has
>>> been hidden / disposed off. That's how modal dialogs are expected to
>>> behave.
>>>
>>>>
>>>> boolean b = md.isSuccessful(); // now on "one" false
>>>> try {
>>>> Thread.sleep(250);
>>>> }
>>>> catch (InterruptedException e1) {}
>>>> b = = md.isSuccessful(); // and now on "one" true ????
>>>>
>>>> if(md.isSuccessful())
>>>> md.dispose();
>>>> else {
>>>> md.dispose();
>>>> owner.showStartupDialog();
>>>> }
>>>> }
>>>> };
>>>> t.start();
>>>>
>>>
>>> Couldn't understand what your comments meant either.
>>>

>>
>> It is true, that the Thread stops and displays the modal dialog. But
>> although I first set the flag within the dialogs actionPerformed
>> method and then set the dialog to not visible I get two results in the
>> calling Thread depending on when I invove md.isSuccessful().
>>
>>
>> The comments referred to the actionCommand, I meant that the command
>> "one" is the one, where successful is set to true in the
>> actionPerformed() method above.
>>

>
> OK - Check if the problem goes away if the variable flagSuccesful is
> declared to be "volatile". (Or alternately declare the isSuccessful
> method as synchronized).
>
> BK
>
>


Unfortunately that does not help ;-(
Aloys
 
Reply With Quote
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      12-09-2004
Aloys Oberthür wrote:
> Babu Kalakrishnan wrote:
>
>> Aloys Oberthür wrote:
>>
>>> Babu Kalakrishnan wrote:
>>>
>>>> Aloys Oberthür wrote:
>>>>
>>>>> I have a question on modal dialogs in
>>>>> (non-event-dispatching)Threads. I do set a flag in the
>>>>> actionPerformed() method of a modal dialog and the object which
>>>>> displayed the dialog in the first place can question this flag
>>>>> after "returning" from show(). I would have expected this to be not
>>>>> timing dependant, but I see it is not which I do not understand
>>>>>
>>>>>
>>>>> that's the dialog in essence:
>>>>>
>>>>> class ModalerDialog extends JDialog implements ActionListener {
>>>>> boolean flagSuccessful = false;
>>>>> ...
>>>>>
>>>>> public void actionPerformed(ActionEvent aE) {
>>>>> String cmd = aEvt.getActionCommand();
>>>>>
>>>>> if(cmd.equals("one")) {
>>>>> flagSuccessful = true;
>>>>> this.setVisible(false);
>>>>> }
>>>>> else if(cmd.equals("two")) {
>>>>> flagSuccessful = false;
>>>>> this.setVisible(false);
>>>>> }
>>>>> }
>>>>>
>>>>> public boolean isSuccessful() {
>>>>> return flagSuccessful;
>>>>> }
>>>>> }
>>>>>
>>>>>
>>>>> and that is the Thread launched within the actionPerformed()-method
>>>>> of a menu ActionListener (see // comments)
>>>>>
>>>>> Thread t = new Thread() {
>>>>> public void run() {
>>>>>
>>>>> ModalerDialog md = new ModalerDialog(owner, true);
>>>>> md.show();
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> If "md" is really a modal dialog, I would expect this thread to stop
>>>> right here, and continue on to the next line only after the dialog has
>>>> been hidden / disposed off. That's how modal dialogs are expected to
>>>> behave.
>>>>
>>>>>
>>>>> boolean b = md.isSuccessful(); // now on "one" false
>>>>> try {
>>>>> Thread.sleep(250);
>>>>> }
>>>>> catch (InterruptedException e1) {}
>>>>> b = = md.isSuccessful(); // and now on "one" true ????
>>>>>
>>>>> if(md.isSuccessful())
>>>>> md.dispose();
>>>>> else {
>>>>> md.dispose();
>>>>> owner.showStartupDialog();
>>>>> }
>>>>> }
>>>>> };
>>>>> t.start();
>>>>>
>>>>
>>>> Couldn't understand what your comments meant either.
>>>>
>>>
>>> It is true, that the Thread stops and displays the modal dialog. But
>>> although I first set the flag within the dialogs actionPerformed
>>> method and then set the dialog to not visible I get two results in
>>> the calling Thread depending on when I invove md.isSuccessful().
>>>
>>>
>>> The comments referred to the actionCommand, I meant that the command
>>> "one" is the one, where successful is set to true in the
>>> actionPerformed() method above.
>>>

>>
>> OK - Check if the problem goes away if the variable flagSuccesful is
>> declared to be "volatile". (Or alternately declare the isSuccessful
>> method as synchronized).
>>
>> BK
>>
>>

>
> Unfortunately that does not help ;-(
> Aloys

 
Reply With Quote
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      12-09-2004
Aloys Oberthür wrote:
> Babu Kalakrishnan wrote:
>> OK - Check if the problem goes away if the variable flagSuccesful is
>> declared to be "volatile". (Or alternately declare the isSuccessful
>> method as synchronized).
>>

>
> Unfortunately that does not help ;-(


Sorry I'm out of guesses.. Can take a look if you can post a short
compliable and runnable example that exhibits this behaviour.

BK
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening modal page from modal page Don ASP .Net 0 05-27-2008 11:49 PM
Modal dialog doesn't threads to sleep in Firefox? VK Javascript 10 03-07-2006 09:02 AM
Modal and non modal in same app Leila Java 0 04-26-2005 09:48 PM
post the form data in modal window and close the modal window. Matt HTML 1 06-01-2004 08:22 PM
post the form data in modal window and close the modal window. Matt Javascript 0 06-01-2004 07:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57