I have a class named “FormPanel.java” that extends JPanel. This class create an information entry form with some JTextFields, a ComboBoxes, a JList, and a JButton named "submitBtn". After filling the form, JButton should be pressed for processing that info. So I add an action listener to my JButton. As I have some other components that need listeners for handling their events, so I define an addListeners() method in my FormPanel class, and there I create JButton listener as below:
public class FormPanel extends JPanel {
/* some codes here*/
private void addListeners() {
submitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FormEvent = new FormEvent(this,emptxtField,emptxtField);
/* and there are some lines of codes here */
System.out.println(this);
formEventAccured(ev);
//this.formEventAccured(ev);
}
});
/*and some other listeners in here*/
}
/* some codes here*/
public void formEventAccured(FormEvent e) {
//…
}
}
formEventAccured(FormEvent e) is a public method in FormPanel class. Problem is here: in the actionPerformed method, if I call formEventAccured method as “formEventAccured(ev)” everything goes fine, but if I call it as “this.formEventAccured(ev)” it cause error! Even though I print “this” object and it was FormPanel Class not an ActionListener!
sysout result:
FormPanel$1@1bc7e86
Error: the method formEventAccured(FormEvent) is undefined for the type new ActionListener(){}
Since formEventAccured is a public method of the FormPanel class, and printing "this" show that it is FormPanel object, so why calling it as “this.formEventAccured(ev)” will cause an error?! Thanks.