Parent Window Closing when Closing Child Window - java

I have an application that creates a new window (let's call it Cuprins) when a button of the same name is pressed. The problem I have is that when I close the new window, it also closes the main one. Is there anyway to make the main window not close when closing the Cuprins window?

Check what's the default close operation. Set it to "dispose on close" or "do nothing on close"

The newly opened window should use dispose on close to dispose the frame, or setVisible(false) to hide it temporarily.

Just use this line while you creating child window.
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Usage example :
AddLeagues addLeague = new AddLeagues(); //Child View
addLeague.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Related

Netbean GUI interface window closing

I'm working in the netbeans GUI interface, and I want to know how to close the window. I found the following code:
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
However, I cannot find the name of the frame in the code.
How do I find the frame name, or am I going about this the wrong way? How do I close it?
Put this line in your form no need to use window event
setDefaultCloseOperation(EXIT_ON_CLOSE);
How do I close it?
When you create the frame you need code like:
Jframe frame = new JFrame(...);
frame.setDefaultCloseOperation(JFrame.EXIT__ON_CLOSE);
Now when you click on the "close" button the application will exit.
However you may also want to close the frame by using a menu item. In this case what you want to do is create an Action that you can add to your "Exit" JMenuItem.
Check out the Exit Action found in Closing an Application. The Exit Action shows how you can access the current frame in order to dispatch the windowClosing() event to the frame. So the "Exit" menu item will then function just like the user clicking on the "close" button.
You are using NetBeans IDE, go to JFrame Properties, the very first option is DefaultCloseOperation, use the drop down menu to toggle between options available or you can add custom codes.
Found an answer at https://netbeans.org/kb/articles/gui-functionality.html
Turns out all I needed was
System.exit(0);

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();

Window Close Listener And setDefaultCloseOperation

I have a question about the relationship between swing window listener and default close operation. The question occurs when I am dealing with below scenario:
I add a window listener (WindowAdapter is used for the listener) for a JFrame, and override the windowClosing function: if the user closes the window, a dialog will pop up to confirm, if user chooses CANCEL option, then directly return. However, when I test the code and choose CANCEL when closing the window, the frame window is still closed (or maybe just invisible, because the Java icon is still in the task bar).
Then I add the default close option with DO_NOTHING_ON_CLOSE, with the same test behavior, the frame window is not closed, which is what I expected.
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Then I change the default close option with EXIT_ON_CLOSE, with the same test behavior, the frame window is directly closed(this time Java icon also disappeared).
This makes me confused. Does it mean the window listener can just define what to do when the window is closing but can't determine whether to close the window? Or I need to override other function?
The window listener is just a listener. It does not affect the default close operation, unless you actually change the default close operation in the windowClosing() code.
I use:
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
as the default when the frame is created. Then in the windowClosing(...) method, if the user confirms the closing of the frame I change the default:
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
Check out Closing an Application for more information and more complete example code.

Open New Window And Close Parent

When my program launches, a window (aka "StartWindow") opens that has 3 options: New Game/Load Game/Exit. When New Game or Load Game is clicked (and after some input), the game window (aka "GameWindow") will open so the user can play.
When the GameWindow opens, I would like to have the StartWindow close. And when the GameWindow closes, the StartWindow will open until the actual "EXIT" button is clicked.
Is there a way to do this? Right now, the only way I can achieve something similar to this is having a boolean called "gameRunning". When this is true, the buttons on StartWindow will have no action when clicked. Opposite when false.
For example purposes, suppose that each window has 3 buttons, a text field, and nothing else.
Use setVisible(false) method on parent before opening any child window. When child window closes call setVisible(true). It will solve your problem

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

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.

Categories