how do i close a frame yet open a new frame? - java

how do i close a frame yet open a new frame?
i have a frame, (help)
when i click on my menu item
i want to open (mainForm)
exit from help.
new mainForm().setVisible(true);
System.exit(0);
i know this closes the whole program however how do i get it to only close the current frame
thanks

If you no longer want to use the frame you could use frame.dispose()
If you just want to hide it use frame.setVisible(false).
If you extended a Frame and are trying to close it from within use this.dispose or this.setVisible(false).

You should rethink your requirments. For the user, it would be best to have both the program and the help window visible at the same time. Closing the main window when showing the help screen and vice versa is really, really bad for usability - you shouldn't do it. We've had window-based GUIs for almost 30 years now - showing several windows on screen at the same time is what they're for!

Let's say you created your frame as so:
JFrame mainframe = new JFrame("Radio Notes");
//show Frame
mainframe.setVisible(true);
//close the frame
mainframe.dispose();

I think you should hide the frame you do not wish shown with setVisible(false). System.exit(0) stops the JVM ending the entire program.
In short, the semantics of setVisible has nothing to do with starting / stopping the application.
If you want to start an entire new application you'd have to look at Runtime.exec(). I don't really know if you can close the parent process (the Help application) with exit() though.

try setting the default close operation for the JFrame like so.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Then implement a WindowListener that performs the actions you want when a window closing event is fired.

Related

Closing java swing window manually

I have a small game in which one window opening the other when the game over.
I want to close only the game over window.
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
But this closes the both windows.
How to close only the second window?
It can be closed from the game over window itself by
this.dispose();
or hidden by
this.setVisible(false);
You can also change the closing behavior with (assuming a JDialog where the default is HIDE_ON_CLOSE)
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
If you need to close it from the parent JFrame, dont use this, but instead target the object
gameOverDialog.dispose();

Java GUI app frame doesn't display correctly when launched?

I have made this TicTacToe app using the Java Swing library. Ever since I've added the menu, it wouldn't launch as expected. I mean, the functionality is fine but it would sometimes display as one of the three undesirable methods I have in the image when I launch it. However, once I maximize and minimize the frame, it would display in the desired manner.
Kindly help me fix this.
You are adding components to the frame once its already visible. Call frame.setVisible(true); only after you have added all components or you will have to revalidate the container. Once a container is visible and layed out, you have to call validate/revalidate if you add or remove components.
You are showing the frame before all the items are added
public void setUpFrame() {
...
frame.setVisible(true);
}
just move the frame.setVisible(true); to the end of the setUpFrame() method :)
i know that i already answer your question but i made this answer because i saw that if i press on the menu item of the exit, it "close" the window, it stays open in the background.
if you want to close the program completly use System.exit(0); instead of frame.setVisible(false); in the item_exit ActionListener
nice game and keep programming ;)

Blocking frame switch with Java

I would like to block the frame switch request of the user in my java Application; Example:
I have the main frame with full size and the setup frame with a smaller size (400,400 for example). While the setup frame is opened I wouldnt like to let the user to acess the Main Frame, he can do this only if he closes the setup frame.
That might one duplicated question and I'm sorry for that, but I couldnt find the specific term to research what I want, I was try something like "Window focus on java" but I think I was researching in the wrong direction..
Thanks in advance for the help
The best solution is to use a modal dialog. If this is not an option, you will have to create a handler yourself that fires whenever a frame gets focus and checks if it is the right frame. If not, the handler must focus on the important frame.
set child jframe or whatever swing component as modal window

How to properly hide a JFrame

I have a very simple JFrame window that contains one button: No.
In the main function I set setVisible(true); my JFrame and in the No button listener I want to close the window so I set the visibility to false: setVisible(false); and after that I do System.exit(0); in order to prevent possible memory leaks when running the program many times.
I have two questions:
Do I really need to System.exit(0); in the above case?
If I have this JFrame as a popup window, I can't really use System.exit(0); because this will terminate the whole program. So how can I properly close the popup window and stay in the main JFrame window? (Now I close it only by setVisible(false); and when I do it several times through the program execution, the program turns very slow).
use CardLayout
if is there real reason for another popup container
use JDialog with parent to JFrame, with setModal / ModalityTypes
create only one JDialog and to reuse this one JDialog by getContentPane#removeAll()
use JOptionPane for simple users interaction
put both together, above two points, to use CardLayout for popup JDialog with parent to JFrame, notice after switch from one card to another could be / is required to call JDialog.pack()
setVisible will cause slowdown
dispose will cause slowdown
System.exit will close entire JVM
Therefore, you should reuse a single JFrame or JDialog.
In the button's ActionListener, invoke frame.setVisible(false);. Then instead of creating a new frame just do frame.setVisible(true);. If you want to change the contents of the frame, there is the function frame.getContentPane().removeAll();.
Just add this: JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE).
Note: The default option for JFrame is HIDE_ON_CLOSE.
You can use the dispose() method of the JFrame class to close the frame and release all resources associated with it, including its child components.

Link between two JFrames

Link between two JFrame using JMenu
I have an application where I have to move to another frame by clicking a menu. for example, on a file menu, I click on add which will bring out a new frame where operations can be carried out. what code(s) can i use in Netbeans?
I used it to open another JFrame but when I exit the new JFrame both frames are closed ....
there is something wrong in ma code plz help me in that prob....
You have to set the defaultCloseOperation to something else then EXIT_ON_CLOSE. Please refer to the documentation for JFrame for more information. I suspect that the second JFrame should only have HIDE_ON_CLOSE though.
do you need to use jframes? why not jdialogs? (the dialog window (which can pop up) does not have to be modal.
if you have two different frames, that can make parentage (which window belongs to which frame) something you need to keep track of more assiduously.
please tell us in more detail what you are trying to accomplish; that will help in picking the correct widgets to be used. if things are hard to do/code in swing, then there's probably an easier way with other widgets/components, imho.
good luck!

Categories