My program has one button, and the other one is a JTextField. The action listener for the button and the textfield are different. I’m using:
textfield.addActionListener(this);
button.addActionListener(this);
… inside my constructor.
They both do the same actionListener. How can I call their respective methods?
You are implementing
ActionListenerin the class of both components. So, when an action happens,actionPerformedmethod of the class is called for both of them. You can do following to separate them:1-Create a separate class and implement
ActionListenerinterface in it and add it as a actionListener for one of the components.2-In
actionPerformedmethod, there is a parameter withActionEventtype. Call getSource method of it and check if it returns the object ofJTextFieldorJButtonby putting an if statement and do separate things accordingly.