Studyon Minte9.com
Java SE




Get parent frame




package testexample; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Test extends JFrame { static Frame2 frame2 = new Frame2(); public static void main(String[] args) { Test frame = new Test() {{ setBounds(200, 200, 400, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); }}; } public Test() { JPanel panel = new JPanel() {{ JButton button = new JButton("Open") {{ addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame2.setVisible(true); } }); }}; add(button); }}; add(panel); } public static class Frame2 extends JFrame { public Frame2() { setBounds(350, 350, 300, 200); setVisible(false); JPanel panel = new JPanel() {{ JButton button = new JButton ("Cancel") {{ addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // --- Look Here --- // //setVisible(false); // this only hides the button getTopLevelAncestor().setVisible(false); // corect } }); }}; add(button); }}; add(panel); } } }
 


http://www.coderanch.com/t/346777/GUI/java/parent-frame-JMenuItem-ActionListener