I have a parent JFrame and two 'child' JFrames in package JFrame. Imagine I open parent JFrame and then it's child JFrames - currently all 3 forms are open. Now I close parent JFrame.
What should I do to close all child frames automatically after close their parent JFrame?
Here is my code:
class Parent:
public class Parent extends JFrame {
public Parent() {
JButton child1 = new JButton("child1");
child1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Child1().setVisible(true);;
}
});
JButton child2 = new JButton("child2");
child2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Child2().setVisible(true);
}
});
JButton closeAllJframe = new JButton("closeAllJframe");
closeAllJframe.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
this.setBounds(500, 200, 400, 200);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
}
});
JPanel jPanel = new JPanel();
jPanel.add(child1);
jPanel.add(child2);
jPanel.add(closeAllJframe);
this.add(jPanel);
}
@Override
public void dispose() {
super.dispose();
}
public static void main(String[] args) {
new Parent().setVisible(true);
}}
class Child1:
public class Child1 extends JFrame {
public Child1() {
this.setBounds(200, 300, 300, 200);
this.setTitle("child 1");
}}
class Child2:
public class Child2 extends JFrame {
public Child2() {
this.setBounds(200, 300, 300, 200);
this.setTitle("child 1");
}}