Is it possible to disable JFrame close operation?
EDIT.
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(
new WindowAdapter() {
#Override
public void windowClosing(final WindowEvent e) {
}
});
And this code doesn't solve the problem.
EDIT
Is it possible to consume WindowClosing event?
Related
Here is a piece of code that I wrote but the close button on the top of the application doesn't work please help
Code:
import java.awt.*;
import java.awt.event.*;
public class App extends Frame implements MouseMotionListener {
App() {
addMouseMotionListener(this);
setSize(200, 200);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.setColor(Color.RED);
g.fillRect(e.getX(), e.getY(),10, 10);
}
public void mouseMoved(MouseEvent e) {
}
public static void main(String[] args)throws Exception {
App a = new App();
}
}
Image:
You need to add Listener and call dispose while initializing your applet.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
You need to add above line of code inside the constructor.
Try it with that little code:
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
You have to insert it in "App()". It will close the program "System.exit(0);"
when you press the close button.
im not using AWT often but heres my Solution :)
addWindowListener(new WindowAdapter(){
#Override
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
Add a new WindowAdapter to your App Constructor and call system.exit(0) on the Window Close Event
You can use WindowContants on JFrame#setDefaultCloseOperation to achieve the desired action. This allows you to dispose the frame, entirely terminate the application and a few more with a single line in the constructor:
this.setDefaultCloseOperation(WindowContants.DISPOSE_ON_CLOSE);
will dispose the frame containing the app. This is sufficient to terminate the program you presented.
This approach doesn't allow handling any events though, but simply closes the frame.
I have a JFrame and if I press the close button in the top right corner I invoke a JDialog and ask if the user really wants to close.
This is the ActionListener on the close Button in my main JFrame:
...
//close listener
addWindowListener(new WindowAdapter() {//invoke "wirklich schließ3n" window if Alt+F4
#Override
public void windowClosing(WindowEvent event) {
MainGuiWindow.this.saveSettings();
CloseDialog cd = new CloseDialog(MainGuiWindow.this);
if (cd.getResult()) {
System.exit(0);
} else {
//MainGuiWondow is "setVisible(false)" but still running | I don't know how to fix it
MainGuiWindow.this.setVisible(true);//doesn't work
}
}
});
The JDialog has just two buttons (YES/NO) (after pressing any button i call dispose(); in the ActionListeners of the Buttons in the JDialog and after the dispose(); i return the answers:
yes returns true
no returns false
Can anybody tell me what to do in the else case of my Close ActionListener in my MainGuiWindow.
The CloseDialog is closed cause of the dispose(); but the main window is is setVisible(false); but still running
This my help you
You need to set default close operation on the JFrame to “do nothing”:
jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
add a WindowListener for the frame.
jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
handleClosing();
}
});
And Refer Here
How do I do action when I'm going to terminates the programme.
For example :
When I'm going to terminates the programme, I want to set
if( the x button is clicked or something )
deleteRow from database;
else
just end.
Is it should be done inside the ui frame ?
Use window listeners for that. windowClosing method is used to execute code when window is closing, windowClosed method can be used if you want to do something after the window has closed.
JFrame window=new JFrame("Window");
window.addWindowListener(new WindowAdapter(){
#Override
public void windowClosed(WindowEvent evt)
{
//Do something after window has closed.
}
public void windowClosing(WindowEvent evt)
{
//Do something when window is closing.
//Useful when you have to access data in window(buttons, textfields etc)
}
});
There are also other window event listeners.Documentation of WindowListener
addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent e)
{
.....
}
});
I am seeing the following code in a Swing demo application:
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
Why would you use this code to close the application? What would happen without it, and/or are there other (shorter) options to do this?
Why would you use this code to close the application?
As TimH said, you can log information, write out properties, or do any other clean up you want to do before exiting your Swing application.
Here's how the window closing code is normally used.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
exitProcedure();
}
};
frame.addWindowListener(wndCloser);
public void exitProcedure() {
frame.dispose();
System.exit(0);
}
Because exitProcedure is a separate method, you can execute it from a JMenuItem action listener, like "Exit".
What would happen without it, and/or are there other (shorter) options to do this?
A shorter option is
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
If you don't specify any default close operation, your Swing application continues to run after you close the JFrame. You wind up with dozens of running applications, doing nothing.
You can use it to log some infos like this:
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
log.info("Programm exit.");
System.exit(0);
super.windowClosed(e);
}
});
A shorter version would be:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I'm making a program with a logger. The logger has its own JFrame.
I'm trying to override the reaction from clicking on the minimize-button of that frame.
I would like the frame to either setVisible(false) or do the defaultCloseOperation (as i set that to hide earlier).
How should I do this?
Thanks in advance
Use a JDialog instead of a JFrame. JDialogs don't have a minimize button.
You can add a WindowListener and add a iconified handler that will react when the window is minimized.
Maybe:
frame.addWindowListener(new WindowAdapter(){
public void windowIconified(WindowEvent e){
frame.setVisible(false);
}
});
You can use the WindowStateListener like this
f.addWindowStateListener(new WindowStateListener() {
#Override
public void windowStateChanged(WindowEvent arg0) {
if (arg0.getNewState() == Frame.ICONIFIED) {
// do stuff
}
}
});
Try this:
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowIconified(WindowEvent event)
{
//do your stuff
}
});