I think that writing listeners in the same class is not OOP-way. So, I try to write listener in another file.
Code that works is:
class MyPanel extends JPanel{
private String tString = "Test String";
private JLabel tLabel;
public MyPanel(){
tLabel = new JLabel("Label");
JButton tButton = new JButton("Click me");
tButton.addActionListener(new ActionListener(){
public void ActionPerformed(ActionEvent e){
tLabel.setText(tString);
}
});
}
But when I write listener in separated file:
public class MyListener implements ActionListener{
copy code here
}
and change
tButton.addActionListener(new ActionListener(){
to
tButton.addActionListener(new MyListener());
it doesn't work. Of course i have tried different combinations.
For example, sending "this" to listener's constructor and calling methods from received object in listener's class.
Error:
MyListener: cannot find symbol "tLabel"