Code is collected from Oracle JavaSE Tutorial:
public class Beeper ... implements ActionListener {
...
//where initialization occurs:
//notice this line
button.addActionListener(this);
...
public void actionPerformed(ActionEvent e) {
...//Make a beep sound...
}
}
This is how you typically register a handler right ???
What this means here ???
button.addActionListener(this);
It means ,call current object's (which this refers) *actionPerformed method whenever a action happens with that button object.
So if you pass your MainPanel's reference instead of this and your main panel has a method actionPerformed and it implements ActionListener* , whenever button fires an event , your Mainwindows's *actionPerformed** will be called.
So changed this line like this :
button.addActionListener(RefOfMainPanel);
That's all. Yup , it's that easy :)