Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes How do I get this listener to wait for this other listener? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I get this listener to wait for this other listener?" Watch "How do I get this listener to wait for this other listener?" New topic
Author

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

Clark Johnson
Greenhorn

Joined: Mar 08, 2007
Posts: 20
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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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

    Joined: Mar 08, 2007
    Posts: 20


    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?
    Craig Wood
    Ranch Hand

    Joined: Jan 14, 2004
    Posts: 1535
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38481
        
      23
    Remove the Listeners and swap them round? Seems so simple when you see it done.
    Clark Johnson
    Greenhorn

    Joined: Mar 08, 2007
    Posts: 20
    Originally posted by Craig Wood:


    Thanks a lot Craig!!! And I thought synchronization would fix my problem
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: How do I get this listener to wait for this other listener?