Close a frame from another frame
Like the title says, I want to close a frame while i'm inside another frame.
I've got 2 frames. Frame 1 is called Create and Frame 2 is called Manual.
Inside Frame 1 is a button that should close Frame 2.
This is the piece of code for the button that should close Frame 2.
Code:
class btnHandleidingUitHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Handleiding.setVisible(false);
}
}
For some odd reason this doesn't work. Anyone out there that can help me with this? :)
Re: Close a frame from another frame
Try using JFrame.dispose() method. it's inherited from the Window class.
Re: Close a frame from another frame
here is an example:
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
public static void main(String args[]) {
JFrame src = new JFrame();
JButton btnOpen = new JButton("Open new");
final JFrame dest = new JFrame();
dest.setSize(100, 100);
JButton btnClose = new JButton("Close it");
btnOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dest.setVisible(true);
}
});
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dest.dispose();
}
});
src.getContentPane().add(btnOpen, BorderLayout.NORTH);
src.getContentPane().add(btnClose, BorderLayout.SOUTH);
src.setSize(200, 200);
src.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
src.setVisible(true);
}
}
Re: Close a frame from another frame
Ahh yes, I see. Thanks a lot! That helped me on my way. :)