Results 1 to 9 of 9
Thread: Close a window
- 10-27-2011, 05:22 PM #1
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
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); }
-
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 05:30 PM.
- 10-27-2011, 05:38 PM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
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.
-
Re: Close a window
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.
- 10-27-2011, 05:45 PM #5
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
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?
-
Re: Close a window
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.
No, you can use a CardLayout as well.Is that the only way?
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.If I use a Jdialog for the main program, can the form still stay jframe?
-
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.
- 10-27-2011, 05:54 PM #8
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
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?
-
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
-
How can i close a Jframe window using a jbutton applied in my program?
By epsi2430 in forum AWT / SwingReplies: 10Last Post: 09-17-2011, 11:59 AM -
Close "One" Window In Java!
By Alerhau in forum New To JavaReplies: 1Last Post: 06-14-2011, 02:58 PM -
Will the connection.close() and statement.close() ever be called???
By Stephen Douglas in forum New To JavaReplies: 13Last Post: 04-09-2010, 11:15 AM -
Close Active Window (?)
By dcnorman07 in forum New To JavaReplies: 2Last Post: 10-03-2008, 05:55 PM -
How to close an open JFrame window from a jsp page?
By kasisaiganesh in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-27-2008, 06:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks