Passing a General Reference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • orangemonkey
    New Member
    • Apr 2007
    • 16

    Passing a General Reference

    In this code, where the ****** are, I want to pass a reference to the jButton8 object. I don't want to pass it to be button specific though, I want the code to be button general, to pass whatever jButton the method is declared in. Is it possible?
    Thanks !

    jButton8 = new javax.swing.JBu tton();
    jButton8.addAct ionListener(new java.awt.event. ActionListener( ) {
    public void actionPerformed (java.awt.event .ActionEvent evt)
    {
    jButtonAction (evt, *************** **** );
    }
    });
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Create a new class, say ActionButton, that extends JButton and implements ActionListener. You can then pass 'this' to that method.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      An ActionEvent has the source of the event as its member. Read the API documentation.

      kind regards,

      Jos

      Comment

      • orangemonkey
        New Member
        • Apr 2007
        • 16

        #4
        Originally posted by JosAH
        An ActionEvent has the source of the event as its member. Read the API documentation.

        kind regards,

        Jos
        Omg. Woohoo. It worked. Thanks Jos and Laharl.

        jButtonAction (evt,(JButton)e vt.getSource()) ;

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by orangemonkey
          Omg. Woohoo. It worked. Thanks Jos and Laharl.

          jButtonAction (evt,(JButton)e vt.getSource()) ;
          You're welcome of course; a little tip: for more substantial guis Actions are a bit
          more convenient than all those ActionListeners all over the place.

          kind regards,

          Jos

          Comment

          Working...