Search This Blog

How to Close or Exit a Java App When some Button is Clicked

To close a Java app, System.exit() can be used. This function can be used to close the app if an error occurs or no error is occurred. If no, error occurred, the argument  of the function should be 0. This method terminates the currently running Java Virtual Machine. The argument stands for status code; by convention, a nonzero status code indicates abnormal termination. If you are calling the System.exit() function without any errors (in normal case), just give the argument as zero. This method calls the exit method in class Runtime.
This method never returns normally. The call System.exit(n) is effectively equivalent to the call:
Runtime.getRuntime().exit(n) To make the app to exit on clicking some button, the ActionPerformed code of the button can be used as follows:
private void myCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {
        
System.exit();

            
          //This causes the app to exit on pressing the button myCloseButton
           
    }

No comments:

Post a Comment