Threads and modal dialog behaviour question

  • Thread starter =?ISO-8859-1?Q?Aloys_Oberth=FCr?=
  • Start date
?

=?ISO-8859-1?Q?Aloys_Oberth=FCr?=

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
 
B

Babu Kalakrishnan

Aloys said:
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
 
?

=?ISO-8859-1?Q?Aloys_Oberth=FCr?=

Babu said:
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.


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
 
B

Babu Kalakrishnan

Aloys said:
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
 
?

=?ISO-8859-1?Q?Aloys_Oberth=FCr?=

Babu said:
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
 
B

Babu Kalakrishnan

Aloys said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top