Results 1 to 9 of 9

Thread: Close a window

  1. #1
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Unhappy Close a window

    Ok what I want to do is the following: If I click on the button it must open another window and then close the window that is open. The calling the other window part works but when I then say System.exit(0) it closes both.
    Java Code:
     private void button1MouseClicked(MouseEvent e) 
          {
             Scorekeeper score = new Scorekeeper();
             score.pack();
             score.setVisible(true);
             System.exit(0);  
          }

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

    Default Re: Close a window

    Of course. System.exit(...) exits from the JVM, so should expect the behavior that you're seeing: all code will end including GUI code.

    To close a window we sometimes use myWindow.setVisible(false); or myWindow.dispose(); if we're not likely to use it again.

    But first off, why do you want to open and close a bunch of windows? Why not either use a dialog window if necessary or swap views with CardLayout?
    Last edited by Fubarable; 10-27-2011 at 06:30 PM.

  3. #3
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: Close a window

    The first window is a form that the user must first fill in and the second one is the main program, so to say. I thought it would be something like that (the code you supplied). I'm not sure what you mean by using another dialog window or swapping views with card layout.

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

    Default Re: Close a window

    Quote Originally Posted by Johanis View Post
    The first window is a form that the user must first fill in and the second one is the main program, so to say. I thought it would be something like that (the code you supplied). I'm not sure what you mean by using another dialog window or swapping views with card layout.
    Since the input form is being called off of the main window, most programs will keep the main window in view and display the form in a dialog window such as a JDialog or JOptionPane, but not in a separate JFrame, and it would be very unusual to hide the main window while this is going on. I recommend that you consider the use of dialogs such as a JDialog.

    The other option is to have your input form in a JPanel and simply swap JPanels in the main program when it is time for the user to fill in this form. A CardLayout is ideal for doing this. The Swing tutorial on CardLayout will tell you all you need to know.

  5. #5
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: Close a window

    AAAAh damn...So i have to do over my main program in a jdialog?? Is that the only way? If I use a Jdialog for the main program, can the form still stay jframe?

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

    Default Re: Close a window

    Quote Originally Posted by Johanis View Post
    AAAAh damn...So i have to do over my main program in a jdialog??
    You don't have to, but if the data is being entered in a dialog way, especially if in a modal way, then it's often preferred.

    Is that the only way?
    No, you can use a CardLayout as well.

    If I use a Jdialog for the main program, can the form still stay jframe?
    Why would you want to reverse roles here. This makes no sense at all. The form isn't the main application window, and you shouldn't force it to be one.

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

    Default Re: Close a window

    Let me guess -- you hard-coded your program so that this form subclasses JFrame, right?

    Suggestion: don't do this. In fact you shouldn't have any classes subclass JFrame or JDialog. Rather have them produce JPanels which you can place any where you want to, be that in the contentPane of a JFrame or a JDialog, or in another JPanel.

  8. #8
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: Close a window

    I don't know what you mean by hard coded (I'm Afrikaans ok haha) but there's a good chance that you are right. Umm I created the gui in jformdesigner so I can just copy the stuff. Which one must I now make jdialog? the form or the main program? or should both be?

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

    Default Re: Close a window

    I'll try to spell my recommendations out simply:
    • The main GUI should be displayed in a JFrame.
    • A dialog window should be displayed in a JDialog.
    • None of your classes should extend either JFrame or JDialog.
    • Avoid using code-generators until you understand what you're doing. At this stage you're far better off creating your Swing applications by hand as it will give you a much deeper understanding of what you're doing.

Similar Threads

  1. Replies: 10
    Last Post: 09-17-2011, 12:59 PM
  2. Close "One" Window In Java!
    By Alerhau in forum New To Java
    Replies: 1
    Last Post: 06-14-2011, 03:58 PM
  3. Replies: 13
    Last Post: 04-09-2010, 12:15 PM
  4. Close Active Window (?)
    By dcnorman07 in forum New To Java
    Replies: 2
    Last Post: 10-03-2008, 06:55 PM
  5. How to close an open JFrame window from a jsp page?
    By kasisaiganesh in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-27-2008, 07:29 PM

Posting Permissions

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