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 9211907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:24:12+00:00 2026-06-18T01:24:12+00:00

I am attempting to create a JFrame that remembers its window position, size and

  • 0

I am attempting to create a JFrame that remembers its window position, size and whether it was maximized or not. It should be simple enough to use a WindowListener for the windowClosing event and save it’s bounds to Preferences.

In order for this to work (as set up below), I would need to extract the JFrame’s normal (NORMAL ExtendedState) bounds while it is maximized (MAXIMIZED_BOTH ExtendedState). Is this at all possible? Considering that the normal bounds are stored somewhere in order for it to revert back.

//...
addWindowListener(new WindowListener() {
    ///...
    @Override
    public void windowClosing(WindowEvent e) {
        prefs.putBoolean("win_max",win_max);
        if(winmax)
        {
            //win_x=?
            //win_y=?
            //win_w=?
            //win_h=?
        }
        else
        {
            win_x=getX();
            win_y=getY();
            win_w=getWidth();
            win_h=getHeight();
        }
        prefs.putInt("win_x",win_x);
        prefs.putInt("win_y",win_y);
        prefs.putInt("win_w",win_w);
        prefs.putInt("win_h",win_h);
    }
});
//...

I apologise if this is a really simple question. Any help is appreciated.

  • 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-06-18T01:24:13+00:00Added an answer on June 18, 2026 at 1:24 am

    Here’s what I did. The model instance saved the frame origin, frame bounds, and frame state.

    protected int           state;
    
    protected Point         frameOrigin;
    
    protected Rectangle     frameBounds;
    

    This is a component listener that I created to capture frame data changes.

        frame.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentMoved(ComponentEvent event) {
                if (isMaximized()) {
                    model.setFrameOrigin(frame.getLocation());
                } else {
                    model.setFrameBounds(frame.getBounds());
                }
                model.setFrameState(frame.getExtendedState());
            }
            @Override
            public void componentResized(ComponentEvent event) {
                model.setFrameState(frame.getExtendedState());
            }
        });
    

    This is the code that I had to perform after packing the frame to set the frame to the last used state.

        frame.pack();
        if (options.getFrameBounds().getWidth() > 0) {
            frame.setExtendedState(options.getState());
            if (isMaximized()) {
                frame.setLocation(options.getFrameOrigin());
            } else {
                frame.setBounds(options.getFrameBounds());
            }
        }
        model.setFrameState(frame.getExtendedState());
        model.setFrameBounds(frame.getBounds());
        model.setFrameOrigin(frame.getLocation());
    

    And here’s the isMaximized method.

    public boolean isMaximized() {
        return (frame.getExtendedState() & JFrame.MAXIMIZED_BOTH) 
                == JFrame.MAXIMIZED_BOTH;
    }
    

    I saved the frame data in a properties file, but you can save and restore the frame data how you want.

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

Sidebar

Related Questions

I'm attempting to create a Spring application (NOT web application) to perform some simple
I'm attempting to create a simple animation in Java that displays a blue ball
I am attempting to create a report, utilizing a matrix, that only displays columns
I'm attempting to create a Google 2 Factor Authorisation just as an experiment not
I'm attempting to create a single Controller class to handle all foreseeable surveys that
I'm attempting to create a class and inherit from Toplevel() so that the GUI
I'm attempting to create a directive that binds specific keypresses to functions specified in
I'm attempting to create an array of strings that represent the directories stored in
I'm attempting to create an ASP.NET/C# page that runs a PowerShell script that will
I am attempting to create a report that contains a list nested within another

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.