• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I get this listener to wait for this other listener?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working with a "Frame" object (see below) which I am not allowed to modify. The JButton in this Frame has its own listener. I need to add a new ActionListener to this button in order to expand its functionality. I can't get this to work the way I want it to, and I think it's due to a synchronization problem. Please look at the following code and tell me what I can do to achieve my goal.

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried it, with the following amendments:-
  • Get rid of "public class A." Just call it the main method inside the class Frame.
  • Change the access of class Frame to public.
  • Add the line following after "addActionListener" in the main method:-
  • JOptionPane.showMessageDialog(frame, frame.button.getActionListeners(), "ActionListeners in main", JOptionPane.INFORMATION_MESSAGE);
  • Add the following similar line after addActionListener in the constructor:-
  • JOptionPane.showMessageDialog(this, button.getActionListeners(), "ActionListeners in constructor", JOptionPane.INFORMATION_MESSAGE);
  • I got two JOptionPanes, one saying Frame$1@901887, the second Frame$2@b1937c Frame1@901887. Obviously their hashCodes. Don't know whether this means no 2 was added before no 1.
    Then I get this output on the shell:-

    [Campbell@dhcppc0 ~]$ javac Frame.java
    [Campbell@dhcppc0 ~]$ java Frame
    Button listener from: A
    Button listener from: Frame
    Button listener from: A
    Button listener from: Frame
    [Campbell@dhcppc0 ~]$ java Frame
    Button listener from: A
    Button listener from: Frame


    and the button changes from "I want this to wait . . ." to "Number: 9999999" after a few seconds.

    Might be because the ActionListeners are added in stack order (first in, last out), but otherwise I am not sure what I can suggest which would make any difference if you aren't allowed to change Frame.
     
    Clark Johnson
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator



    Might be because the ActionListeners are added in stack order (first in, last out), but otherwise I am not sure what I can suggest which would make any difference if you aren't allowed to change Frame.[/QB]



    Hello Campbell. As you pointed out in the paragraph above, I am not allowed to modify the Frame class (the class I am working with is more complex than the sample class posted above). The Frame class is what it is and I need work with what I have. Thanks for taking the time to look into this.

    Does anyone have any idea as to how I can solve this problem? How many of you say synchronization will solve my problem?
     
    Ranch Hand
    Posts: 1535
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    Campbell Ritchie
    Marshal
    Posts: 79180
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Remove the Listeners and swap them round? Seems so simple when you see it done.
     
    Clark Johnson
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Craig Wood:



    Thanks a lot Craig!!! And I thought synchronization would fix my problem
     
    I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic