45905

Java making confirming exit

I have an application. I need to make this, when user click the close button, a confirmation will come ( just like javascript confirm dialog ) , 'Are you sure'? I'm using Swing visual editor in Netbeans and I'm using a listener on window closing. But I can't make it. Any help?

Answer1:

Try this code:

jj4.addActionListener(new ActionListener() // jj4-button's reference to implement exit { public void actionPerformed(ActionEvent ae) { if(!jtextarea.getText().equals("") && !jtextarea.getText().equals(fileContent)) { if(fileName == null) { //this method have 3 options (1 = YES, 2 = NO, 3 = Cancel) option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??"); if(option == 0) { //to save the text into file saveAs(); System.exit(0); } if(option == 1) { //to exit the program without saving System.exit(0); } } else { option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??"); if(option == 0) { save(); System.exit(0); } if(option == 1) { System.exit(0); } } } else { System.exit(0); } } });

Recommend

  • Django ModelForm with Admin Widgets
  • SoundPool is loading sounds but every time I play it it says “sample ___ is not ready”?
  • How to run .java file with Variable Name
  • NetBeans doesn't see style.css [duplicate]
  • Regex for incomplete lines within known start and end strings
  • How to resend request with android volley when not authorized
  • Removing event listeners on automatically created multiple elements
  • Dependable views in Ember
  • PHPStorm Namespace support not working for Symfony2 projects
  • Avoid registering duplicate broadcast receivers in Android
  • Hide buttons on title bar in Java
  • Ubuntu and bcrypt
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance paramet
  • Ajax Upload File: $_FILES is empty but files exists in request header
  • Using JRuby with Rails 3.2
  • Code in Job's Script Block after Start-Process Does not Execute
  • Django simple Captcha “No module named fields” error
  • Set the selected item in dropdownlist in MVC3
  • Alert pop up with LWUIT
  • ilmerge with a PFX file
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Spring Data JPA custom method causing PropertyReferenceException
  • Can I display google adwords (AdView) in javafx on android
  • C# - Serializing and deserializing static member
  • Bug in WPF DataGrid
  • Obtain ObjectIdHex value from mgo query
  • Incrementing object id automatically JS constructor (static method and variable)
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Cannot Parse HTML Data Using Android / JSOUP
  • AngularJs get employee from factory
  • Acquiring multiple attributes from .xml file in c#
  • JTable with a ScrollPane misbehaving
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?