Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6648185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:37:31+00:00 2026-05-26T00:37:31+00:00

I created a window and want to intercept the exit with the method windowStateChanged

  • 0

I created a window and want to intercept the exit with the method windowStateChanged to save the data before the application closes. However, it doesn’t appear to be saving the data before it closes. How can I correct this?

see code below:

public class InventoryMainFrame extends JFrame implements WindowStateListener{
    //set up the main window - instantiate the application
  private InventoryInterface inventoryInterface;    //panel that contains menu choices and buttons

  public InventoryMainFrame(){   //main window        
      setTitle("Inventory System");
      setSize (500,500);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new BorderLayout());
      //setLocationRelativeTo(null);    //center window on the screen           
      inventoryInterface = new InventoryInterface();    //set up the panel that contains menu choices and buttons
      add(inventoryInterface.getMainPane());         //add that panel to this window                   
      pack();
      setVisible(true); 

      //display window on the screen           
  }

public static void main(String[] args) {        
    //sets up front end of inventory system , instantiate the application
    InventoryMainFrame aMainWindow = new InventoryMainFrame( );
}

@Override
public void windowStateChanged(WindowEvent w) {
    //intercept the window close event so that data can be saved to disk at this point
    if (w.getNewState()==WindowEvent.WINDOW_CLOSED){
        //save the index file
        try{
          inventoryInterface.getInventory().saveIndexToFile();
          System.out.println("saving");
          dispose();  //dispose the frame
         }
        catch(IOException io){
            JOptionPane.showMessageDialog(null,io.getMessage());
        }
    }       
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:37:32+00:00Added an answer on May 26, 2026 at 12:37 am

    You should try registering a WindowAdapter and override its windowClosing method. For more information, see How to Write Window Listeners.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to a create dialog box like window before displaying the console window.
I want to create an application which main window has canvas (or something where
I created a console application, but I want to turn it into a windows
I have created a Windows CE6 Image through Platform Builder, and I now want
I want to create a draggable and resizable window in JavaScript for cross browser
I want to create a separate thread that runs its own window. Frankly, the
I want to create windows application in c# 2008. The problem is I don't
How to create a windows registry watcher application using .Net, I want this application
how can create a windows move maker like application in vb.net.... if we want
I have an javascript application which I created on windows for windows browsers and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.