title: netbeans setDefaultCloseOperation
body: <p>I am trying to set the default close operation in NetBeans 8.0.2 (in Ubuntu 14.04 on an older Asus gaming laptop.) My program is very large but uses no JFrame or java.swing components.</p>
  
  <p>I merely need to save some values when the "x" in the lower right corner is clicked (this is one usual way to stop execution of a java program in NetBeans.)</p>
  
  <p>I found suggestions that involved swing &amp; JFrame, but it wasn't clear just where to insert the code:</p>
  
  <pre><code>    DefaultApplicationView view = new DefaultApplicationView(this);
      javax.swing.JFrame frame = view.getFrame();
      frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
      frame.addWindowListener(new WindowAdapter(){
          public void WindowClosing(WindowEvent e){
               System.out.println("CLOSING");
          }
      }
  
      show(view);  
  </code></pre>
  
  <p>I also found a set of instructions that I think I would prefer to use, but the post is old enough that my NetBeans doesn't have the tabs/menu-items referred to:</p>
  
  <ol>
  <li>Set Window to Design Mode by clicking the 'Design' Tab</li>
  <li>In the Navigator: Right click the 'JFrame' -> 'Properties'</li>
  <li>In the Properties Tab: Set 'defaultCloseOperation' (top of the list) to 'DO_NOTHING'</li>
  <li>Select 'Events' Tab</li>
  <li>Scroll down to 'windowClosing'</li>
  <li>Click on the "..." button on the right of the event to bring up the custom editor</li>
  <li>Click 'Add...' and name the handler (i.e. custom function that you want to have execute on click of the 'X', or window close event).</li>
  <li>Click 'Ok'</li>
  <li>Netbeans now automatically creates the function and takes to you the function body in the source view</li>
  <li>Now simply add what you want to do here: eg. dispose(), or system.exit or pintln(), or whatever your heart desires, as long as its JAVA and makes sense to the app.</li>
  </ol>
  
  <p>Then there are a few other possibly relevant posts, but they all explicitly involve JFrame and/or swing. (Am I ignorant of some fact such as "<strong>All</strong> NetBeans java applications use JFrame", or some such?)</p>
  
  <p>A pared down example of code for what I'm trying to do would be:</p>
  
  <pre><code>public class MyApp{
  public static void main(String[] args){
      loadMyVariables();
      // do some work that changes variables' values
      // during this work user clicks the 'x' box to halt execution
      // I need then automatically to save the variables' new values
  }
  
  // needs to be called by the OS or GUI when execution is halted by user
  public static void saveMyVariables{
      // here the usual printStream stuff saves some values to a file
      System.exit(0);
  }
  
  public static void loadMyVariables{
      // here the usual Scanner stuff reads some values from a file
  }
  
  }
  </code></pre>
  
  <p>(I need help setting the tags for this, so I'm doing as instructed and asking the community.)</p>
  
  <p>THANKS</p>