Threads and modal dialog behaviour question

Discussion in 'Java' started by =?ISO-8859-1?Q?Aloys_Oberth=FCr?=, Dec 7, 2004.

  1. 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
     
    =?ISO-8859-1?Q?Aloys_Oberth=FCr?=, Dec 7, 2004
    #1
    1. Advertising

  2. 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
     
    Babu Kalakrishnan, Dec 7, 2004
    #2
    1. Advertising

  3. 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
     
    =?ISO-8859-1?Q?Aloys_Oberth=FCr?=, Dec 8, 2004
    #3
  4. 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
     
    Babu Kalakrishnan, Dec 8, 2004
    #4
  5. 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
     
    =?ISO-8859-1?Q?Aloys_Oberth=FCr?=, Dec 9, 2004
    #5
  6. 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
     
    Babu Kalakrishnan, Dec 9, 2004
    #6
  7. 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
     
    Babu Kalakrishnan, Dec 9, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leila
    Replies:
    0
    Views:
    437
    Leila
    Apr 26, 2005
  2. Matt
    Replies:
    1
    Views:
    3,195
    Whitecrest
    Jun 1, 2004
  3. Don
    Replies:
    0
    Views:
    384
  4. Matt
    Replies:
    0
    Views:
    234
  5. VK
    Replies:
    10
    Views:
    195
    Richard Cornford
    Mar 7, 2006
Loading...

Share This Page