Results 1 to 8 of 8
  1. #1
    coollucky is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Closing Child Frame

    Hi everyone!
    I'm new in Java and I have some trouble in closing child frame:

    I've got a main JFame and JPanel. From JFrame I'm creating another frame (lets say frame1 ) and object of JPanel ( panel1 ). Further panel1 is added to frame1. When user clicks on button in main frame frame1 opens. On Jpanel I've got a EXIT button, and when user clicks it the whole program is closing. My question is how to close only frame1 without closing main frame when the user clicks EXIT button on JPanel.

    Please give me any ideas to achieve that.
    Sorry for my fluent English. Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Closing Child Frame

    The child "frame" shouldn't be a JFrame at all, but rather it should be a JDialog. Note that a JDialog can hold complex GUI's as complex as anything held by a JFrame.

  3. #3
    coollucky is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Closing Child Frame

    OK, but how to close JDialog from JPanel?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Closing Child Frame

    Quote Originally Posted by coollucky View Post
    OK, but how to close JDialog from JPanel?
    You should provide more information since I'm not sure how to answer this question with the information you've given. What JPanel? One that is displayed in the JDialog? Another? How were you trying to close the JFrame in your previous code?

    You may wish to show some code.

  5. #5
    coollucky is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Closing Child Frame

    Now I am using JDialog instead of "frame1", then I'm adding panel1 to JDialog. When the button from panel1 is pressed it should close JDialog only. Previously I have been using System.exit(0) that closes the whole program.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Closing Child Frame

    Quote Originally Posted by coollucky View Post
    Now I am using JDialog instead of "frame1", then I'm adding panel1 to JDialog. When the button from panel1 is pressed it should close JDialog only. Previously I have been using System.exit(0) that closes the whole program.
    Yeah, System.exit(...) will shut all down.

    What you need to do is call either setVisible(false) or dispose() on a reference to the displayed JDialog.

  7. #7
    coollucky is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Closing Child Frame

    to use dispose my JPanel class should extend JDialog isn't it?
    When I extend JDialog I can't refence it to JDialog in main frame. Maybe I'm doing something wrong?

    here what I'm doing:

    main frame{
    ...
    jButtonActionPerformed
    {
    JDialog m = new JDialog();
    panel = new SendMessage();
    m.add( sendMessagePanel );
    m.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOS E);
    m.setSize(400, 340);
    m.setVisible(true);
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Closing Child Frame

    Quote Originally Posted by coollucky View Post
    to use dispose my JPanel class should extend JDialog isn't it?
    No. A JPanel should remain a JPanel and a JDialog a JDialog. Don't use inheritance to solve what is essentially a missing reference problem.

    What you need to do is have a valid reference to the JDialog. This can be done by passing the JDialog object into a method or constructor or by using SwingUtilities method:

    Java Code:
    JDialog myDialog = (JDialog)SwingUtilities.getWindowAncestor(myJPanel); // parameter may be this
    myDialog.dispose();

Similar Threads

  1. frame closing
    By manish007g in forum AWT / Swing
    Replies: 4
    Last Post: 09-24-2011, 10:15 PM
  2. how to create a "child" frame.
    By Shellback3 in forum AWT / Swing
    Replies: 4
    Last Post: 02-02-2011, 04:48 AM
  3. Java slave Frame access to its owner main frame problem
    By cagdaseckin in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 10:40 AM
  4. Replies: 2
    Last Post: 05-28-2010, 02:48 PM
  5. Replies: 3
    Last Post: 01-28-2010, 05:13 AM

Posting Permissions

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