To call methods in the parent frame, you need a reference to the parent frame. Therefore, your JPanel constructor can be declared as follows:
public MyPanel(MyFrame frame){
super();
this.frame = frame;
}
And in JFrame, you call this constructor as follows:
panel = new MyPanel(this);
In the case when the handlers attached to your buttons now have access to the frame and can use various methods if necessary.
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.someMethod();
}
});
source
share