File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Clark Johnson
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
    • 0
    • Mark post as helpful
    • send pies
    • 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?
     
    Craig Wood
    Ranch Hand
    Posts: 1535
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47300
    52
    • 0
    • Mark post as helpful
    • send pies
    • 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
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Craig Wood:


    Thanks a lot Craig!!! And I thought synchronization would fix my problem
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic