close window event in java - java

I added an window state listener as follow:
this.addWindowStateListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ExitAction.getInstance().actionPerformed(null);
}
});
But when I'm using the X close button the event is not called.
I think it's something to do with netbean jdesktop framework. But I can't find what could be the problem.
Thanks for your help.

windowClosing is part of the WindowListener interface. Use addWindowListener instead of addWindowStateListener.

Normally you use a WindowListener for this.
Check out Closing an Application for an approach I use, although I must admit I've never tried it with Netbeans since I don't use an IDE.

Not answering your question directly (since an answer has already been given), but I assume you want to quit your program (or just hide a window) on exit. There is a shorter solution for these situations:
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Just debugged a similar problem in my Swing program. It turned out to be a Java bug that kills the system UI events when ImageIO is called before the Swing UI is created. See here for a minimal example -
https://stackoverflow.com/questions/18964138/osx-specific-new-java-ui-bug-reproducable-java-imageio-close-window-event-bug
This bug stops the system UI events, such as window-close, from being delivered to Java.

As the Java documentation says, to actually close the window the listener should invoke the window's dispose or setVisible(false) method.

Thank you everyone for helping me solve the problem. I don't fully understend it but the following code solve the problem:
Frame[] frames = Frame.getFrames();
for(Frame f: frames){
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ExitAction.getInstance().actionPerformed(null);
}
});
}
It look as if the framework add additional frames.
Thanks,

As others have pointed out the WindowListener is what you are after... but you should do this from now on when overriding a method:
this.addWindowStateListener(
new WindowAdapter()
{
#Overrides
public void windowClosing(WindowEvent e)
{
ExitAction.getInstance().actionPerformed(null);
}
});
Then the compiler will tell you when you are not actually overriding a method and are instead adding a new method (that in this case will never be called).

Related

How to close a called class using JFrame without closing the class that called it?

I seem to have a fairly unique problem, and I searched for a while for an answer on here without finding one. I have a class that has a simple JFrame with two buttons. Each button calls the Main method of a different class, as such:
checkRuling = new JButton("Check Your Deck's Rulings");
checkRuling.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
ReadHtmlCardDatabase.main(null);
}
});
One calls a class that takes a series of inputs into a text field and creates a formatted html document from the inputs, and the other loads the html document into a JEditorPane. My problem is that when I close one of the JFrames for the subclasses (either the input or html loader one), it exits my program completely, and I want to keep the main class (with the two buttons) open. I've tried using:
close = new JButton("CLOSE");
close.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
System.exit(1);
}
});
On a button in the subclasses, to no avail. When the button is clicked it simply exits everything. I've also tried using:
JFrame.DISPOSE_ON_EXIT
For the subclasses, but this causes the JFrames to go away without the subclasses actually closing, so the first one that saves the html document never actually saves it, and the second subclass that opens that same html document won't work, because it wasn't saved. Any help would be appreciated, because I can't figure out how to do this.
As Fast Snail says in the comments, you shouldn't be calling a main method. Instantiate a class that does each functonality. Set the frame to visible using setVisible(true) when you start using it, then setVisible(false) when you're done. So, in the action listener, just change the visibility.
Then, assuming you don't have anything too wild going on, the frame you just set to invisible should go out of scope and get freed so that memory isn't chewed up. You just instantiate a new copy of the ReadHtmlCardDatabase class each time you need one. Or you could have one static copy that you set visible/invisible as needed.
one of the JFrames
You should use only one JFrame in Your GUI. For other windows You can use for example JDialog or JWindow.
This should help, if not You can always use frame.setVisible(false) instead of dispose on close, but it' s not very neat.
Thanks to someone who posted a comment and then deleted it, I've figured out my own problem. I just had to replace my main call with this:
setDeck = new JButton("Set Deck");
setDeck.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
WriteHtmlCardDatabase w = new WriteHtmlCardDatabase();
w.main(null);
}
});
Thank you!

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.

Closing on a single window closes all the frames in 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 ;)

Hide JDialog window when the window lost focus

Hi I have only one JDialog box in my Java application.I want to make it invisible if it lost the focus.
I've tried different method, But didn't able to trigger any of the window focus events. Here is my code:
public void windowGainedFocus(WindowEvent e) {
System.out.println("gained focus");
}
public void windowLostFocus(WindowEvent e) {
System.out.println("lost focus");
}
Responding to Focus events can be really tricky. My experience has been that pretty much any time someone has attempted to do non-standard things with focus, they come to regret it eventually. Not least among the issues is that it's not really all that portable - a lot of X-Windows based displays use focus-follows-mouse, which can result in the focus being transferred away when you're not expecting it to, resulting in early dismissal of your dialog.
That said, Sun's official tutorial is here: http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html . If I remember right, you can attach a PropertyChangeListener to the KeyboardFocusManager, and that will be triggered for focus changes: http://java.sun.com/javase/6/docs/api/java/awt/KeyboardFocusManager.html#addPropertyChangeListener%28java.beans.PropertyChangeListener%29
Use a WindowListener and handle the windowDeactivated event.

JInternalFrame and Keybinding

How do I bind a function key say(F11 etc) to a JInternalFrame, so it can activate a particular action.
I tried with the following
this.getInputMap().put(KeyStroke.getKeyStroke("F11"), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println("Called");
}
});
but it never gets called?
Or please suggest how to activate a button inside a JInternalFrame using function key.
Thanks
Azlam
Well, focus is never on the internal frame itself, focus is on a component on the internal frame.
So you should probably be adding the binding by using
internalFrame.getRootPane()....
You may also need to use
"WHEN_ANCESTOR_OF_FOCUSED_COMPONENT"
input map.
The blog entry on Key Bindings explains this in more detail.
JInternalFrame has troubles with KeyListeners and KeyBindings. To solve this problem try using JDialog instead of JInternalFrame.

Categories

Resources