Results 1 to 4 of 4

Thread: Close a frame from another frame

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    2

    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?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Close a frame from another frame

    Try using JFrame.dispose() method. it's inherited from the Window class.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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);
        }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    2

    Re: Close a frame from another frame

    Ahh yes, I see. Thanks a lot! That helped me on my way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width