0

I have a class LabelTextField which does as it indicates, it create a label followed by text field. I want this class to act as JTextField does, ie responding to actionPerformed when an action listener is added. I am adding an action listener with LabelTextField so the JTextField in this class will need to receive the callback and forward on the LabelTextField object name and not the JTextField object name.

I have show a simple JTextField and how it works, the LabelTextField is also there and how I expect to use it.

I have provided a code snippet of what I want to do.

I am having trouble with this type of callback and detail information would be useful.

//

class C_one extends JPanel
{

       private JTextField      mjtfOne;
       private LabelTextField  m_nameFind;

       public C_one ()
       {
          setLayout(new FlowLayout());

          mjtfOne = new JTextField(20);
          mjtfOne.addActionListener(this);

          m_nameFind = new LabelTextField("Name", 20);
          m_nameFind.addActionListener(this);

          add(mjtfOne);
          add(m_nameFind);

       }

       // This is called when return is pressed in either object.
       public void actionPerformed(ActionEvent ae) 
       {
          // This works.
          if (ae.getSource() == mjbnOne) {
             System.out.println("Text field one!");

          // ****** This doesn't work. *****
          } else if (ae.getSource() == m_nameFind) {
             System.out.println("Label Text Field name");
          }
       }

}

// // This create a label and textfield as a widget. class LabelTextField extends JPanel {

   private  JTextField     mjtf;
   private ActionListener  mal;


   public LabelTextField(String label, int textfieldWidth)
   {
      setLayout(new FlowLayout());

      JLabel lbl = new JLabel(label);
      add(lbl);

      mjtf = new JTextField(textfieldWidth);    
      add(mjtf);    
   }

   // The caller of this class will call this as he would for
   // JTextField.
   public void addActionListener(ActionListener al) 
   {
      mjtf.addActionListener(this);
      mal = al;     
   }

   // This will receive the return from JTextField and needs
   // to forward this on to calling class with caller object
   // not mjtf. 
   //
   // Problem: I can not figure out how to save caller object 
   // name so when this is called I can forward it on just like 
   // what JTextField would have done, ie caller can use 
   // ae.getSource() == m_nameFind.
   public void actionPerformed(ActionEvent ae) 
   {
      mal.actionPerformed(ae); // This does call the caller.
   }

}

1

2 Answers 2

1

the value of ae.getSource() will be mjtf, because that is the component that generated the original ActionEvent. If you want to have the source of the event to be m_nameFind, i.e. your custom LabelTextField object, you need to set it manually:

  public void actionPerformed(ActionEvent ae) 
  {
    ae.source=this; // this makes it look like the LabelTextField generated the event
    mal.actionPerformed(ae); 
  }

Is this what you want to do?

--

edit

Thanks @Madprogrammer.. Perhaps you should recreate the ActionEvent like this

  public void actionPerformed(ActionEvent ae) 
  {
      mal.actionPerformed(new ActionEvent(this, ae.getID(),
         ae.getCommand(), ae.getWhen(), ae.getModifiers() )); 
  }
3
  • Oh... oops :-( well that puts a damper on my answer. I can't seem to downvote myself tho! Jun 29, 2013 at 1:34
  • Adding mal.actionPerformed(new ActionEvent(....) ); Did the trick. My C_one class now see m_nameFind as the variable. Question is what was the mechanism that change if from mjtf to nameFind???? Jul 1, 2013 at 22:08
  • I want to thank you all for helping with my problem. I guess the mechanism for indicating m_nameFind was because mal was added the listener. Jul 3, 2013 at 4:47
1

Since you can't change the source of the ActionEvent, you need to create a new ActionEvent:

public void actionPerformed(ActionEvent e)
{
    e = new ActionEvent(
        this,
        ActionEvent.ACTION_PERFORMED,
        e.getActionCommand(),
        e.getWhen(),
        e.getModifiers());

    mal.actionPerformed(e);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.