Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

managing parent frame from child frame on java swing

I have a jframe (parent) which creates an input frame (child) where I get some parameter.

In the "child" frame I have "ok" and "cancel" buttons.

When "ok" button is pressed, the parent frame needs to be updated with new data.

What is the best way to do that??

like image 335
Giancarlo Avatar asked Jul 11 '26 04:07

Giancarlo


1 Answers

As of Java 1.3

public class MyPanel extends JPanel
{

  public MyPanel() {

    ....

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(
      new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          // <<<< HERE'S THE INTERESTING BIT >>>>
          javax.swing.SwingUtilities.getWindowAncestor(MyPanel.this).dispose();
        }
      }
    );
    add(cancelButton);

    .....

  }

}
like image 187
corlettk Avatar answered Jul 14 '26 14:07

corlettk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!