Closing on a single window closes all the frames in java - java

I would like to make possible the navigation of frames in java.Whenever i close a frame the remaining frames which are also opened get closed;and the entire program stops.
Please help...

You probably used
//this will terminate or exit your application
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Maybe you want to use this instead,
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
for your reference go to this link

If you want to close only that one frame, you should do something like this: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
If you want to close all frames whenever a single frame closes you can do the following:
You could use a window listener and call System.exit(0); when the JFrame closes, or try setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); on each JFrame. That way your program would close all frames and end.
If you need to perform some tasks before application quits, you should probably use the window listener.

You can also do that graphicaly. right click on your frame and select properties and from there you can change that like the below picture.

If you were using swing palette. In frame properties choose default close operation as (Dispose). Follow as per the image given in this solution.

My problem was that I used a listener found on the basic tutorials :
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
I know it's dumb. I didn't see it, but some people might have done the same thing, so I'll just leave this here ;)

Related

How to close the active JFrame

I am trying to find a method that can close the active JFrame.
I am unable to use frame.dispose();, as I am declaring the action listener in a toolbar class and the frames I want to close are not static and are declared at runtime.
I have tried using:
java.awt.Window win[] = java.awt.Window.getWindows();
for(int i=0;i<win.length;i++){
win[i].dispose();
}
and whilst this does work, in certain circumstances it will close more than one window even though only 1 window appears to be open, so frames will flash open and closed many times depending on what actions the user has made.
For me to fully recreate my problem would involve posting a significant amount of code which would not be in line with MCVE principles.
I am hoping someone will know of a more simple and reliable way of closing the active frame in the mould of acitveframe.dispose(); - which I now is not a real solution!!
What happens if you try to get the Window ancestor of the source of the action event? i.e.,
#Override
public void actionPerformed(ActionEvent actionEvent) {
Component comp = (Component) actionEvent.getSource();
Window win = SwingUtilities.getWindowAncestor(comp);
win.dispose();
}
This won't work if the source is not a Component or if it is not contained within the top level Window of interest.
Regarding:
For me to fully recreate my problem would involve posting a significant amount of code which would not be in line with MCVE principles.
I'll bet with a bit of effort you could create and post something that comes close.
I am hoping someone will know of a more simple and reliable way of closing the active frame
In your loop you can add:
if (window.isActive())
// do something
Or maybe a simpler approach is:
Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
Also, assuming your active window is a JFrame, instead of using window.dispose(), I have used code like:
WindowEvent windowClosing = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
frame.dispatchEvent(windowClosing);
this will simulate the user clicking on the "Close" button which means that any WindowListener you added to the frame will also be executed. See Closing an Appplication for more information and ideas.
When you are declaring your JFrames, declre them as final if you cannot use static :
final JFrame f = new JFrame();
It would solve the problem.

how to close a single class that contains frame in java

i wan to close the particular classname.java window alone.
I am developing a GUI using netbeans.
I created a main class and defined a button , when clicked will load
new classname().setVisible(true);
this classname.java contains a frame with components to get the input
now when i use System.exit(); in classname.java all the windows are closed
i wan to close the particular classname.java window alone.
how can i do so ?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); // TODO add your handling code here:
}
USE DISPOSE_ON_CLOSE
JFrame f = JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)`
If you just want to hide it use
setVisible(false);
Or to dispose use
dispose();
Make sure you keep a reference to the classname instance you created, and call either setVisible(false) or dispose() on it.
When you do that,it will close all the windows opened, it means that the app will be ended.
I think you should use this method: windowClosing(). You can get more information about this, here:link
I hope that is helpful to you.
Regards, Gil

JFrame On Close Operation

I was wondering if there is a way, by clicking on the "X", to let the program perform some code before closing the JFrame. The setDefaultCloseOperation() method takes only an integer.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
int i=JOptionPane.showConfirmDialog(null, "Seguro que quiere salir?");
if(i==0)
System.exit(0);//cierra aplicacion
}
});
#Jeffrey has a good answer, but you should consider what you're trying to do. If you really want to do something upon the closing of a frame, then a WindowListener is the way to go. However, if you're looking for a place to do some cleanup and graceful shutdown stuff, then you probably want a shutdown hook instead. Using a WindowListener, the code will only be triggered, as you said, by the user "clicking on the X". But what if the user starts the app in the foreground of a terminal and kills it with Ctrl+C? What if the user kills the process from the command line or from a task manager?
You might be interested in using a WindowListener. The WindowListener tutorial.

Detecting Focus on (frame + components) in swing

I have a small dialog frame that appears, and within this frame are a series of buttons and a textbox.
I need the frame to be able to detect when the user has put focus on something else on the screen (being: anything besides the frame and its components), so I can close down the frame.
Any advice on how to go about this? I've been trying at focus solutions for hours, to no solution!
Try using a WindowStateListener
The WindowEvent parameter it provides can tell you if the window has lost focus through the getNewState() method.
class MyFocusLostListener implements WindowStateListener {
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == WindowEvent.WINDOW_LOST_FOCUS) {
e.getWindow().setVisible(false);
}
}
}
need the frame to be able to detect when the user has put focus on something else on the screen
Use a WindowListener and listen for windowDeactivated.
listen to property changes of the property "permanentFocusOwner" of the KeyboardFocusManager. On being notified, check if the new focusOwner is in the child hierarchy under the frame, if not - close the frame.
Edit: seeing the answers suggesting a Window/StateListener - they are better than mine for a top-level window :-) Listening to the keyboardFocusManager is a good approach for containers deeper down in the hierarchy, implemented f.i. in the CellEditorRemover of a JTable (to decide if a pending edit should be terminated)

Java swing "children" windows

Sorry for really simple question, but I wasnt able to find anything in the net, probably because I dont know the right terms to look for.
When you have a desktop application, there are many so called children windows: one for options, one for "about" and so on. How to make them in Java Swing (with NetBeans tools - optional)?
So far I just created another JFrame and on relevant event opened it the same way Main function launches, well, main JFrame. But there's something wrong with this method: when I close child window via x in the upper right corner, whole program terminates as if I was closing the main window. This is probably because NetBeans auto generated code for x and I can find and change it somewhere... but still I have a feeling that there must be a simpler proper way to add children JFrames (or JPanels or whatever is it for children windows) ;)
... But there's something wrong with this
method: when I close child window via
x in the upper right corner, whole
program terminates as if I was closing
the main window. ...
Well, I would say you are on the right path. Just make sure that you do the following for your child frames :
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Then closing the child windows won't shut down your entire application.
HTH ! ;-)
Do you mean a Dialog Window (see How to Make Dialogs) that you likely want to be modal to block user input to all other windows in the program (see An Overview of Dialogs)?
Go to the frame-design mode -> select your frame -> in the properties, the fist option. Select Dispose on close.
If you don't see the props: Ctrl + Shift + 7 or Window (in the frame menu) -> Properties
If you create frames, they aren't considered children windows. You can not set an owner for them as you could a JDialog. If you did want to create another JFrame, then you would have to set the close operation
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
instead of
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The EXIT_ON_CLOSE operation will call
System.exit(0);
which will terminate the JVM. Just remember, that if your main JFrame is set to
DISPOSE_ON_CLOSE
then you must make sure that all your other frames have been properly disposed of, otherwise it won't exit until they have been.

Categories

Resources