Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2006

Tip: Looking for answers? Try searching our database.

Saving Windows Position/Size

Thread view: 
Jason Cavett - 28 Nov 2006 14:58 GMT
I'm writing a program in which I want to save information like window
size, window position, is it maximized, etc.  I have it (mostly)
working, but I've noticed one bug that's been annoying me, and I'm not
entirely sure how to fix it.

If the application window is maximized when I leave the application, it
saves the previous size and position (before the maximization) to an
XML file.

The problem is, when I start the application the next time, it is
maximized.  That's good.  When I click the button to "unmaximize" it,
it doesn't always return to the original size and position (which I
have saved).  Sometimes it will be placed at position -4, -4 (the upper
left corner of the screen).

The only thing I can think of is that, because I am firing two
listeners (I'm watching for component resized and component moved)
something isn't registering correctly.  Here's the portion of my code
that is performing the actual resizing.  (It gets called twice if I
perform a maximize because Java sees a maximization as both a resize
and a position change.)

    private void maximize() {
        if (view.getExtendedState() != JFrame.MAXIMIZED_BOTH
                && config.isMaximized()) {
            config.setMaximized(false);
            this.setLocation(config.getWindowPosition());
            this.setSize(config.getWindowSize());
        } else if (view.getExtendedState() == JFrame.MAXIMIZED_BOTH) {
            config.setMaximized(true);
        } else {
            config.setWindowPosition(this.getLocation());
            config.setWindowSize(this.getSize());
        }
    }

Any suggestions would be appreciated.  If I'm doing this entirely
wrong, I'd appreciate knowing that as well.  Thanks!
Jason Cavett - 28 Nov 2006 18:44 GMT
> I'm writing a program in which I want to save information like window
> size, window position, is it maximized, etc.  I have it (mostly)
[quoted text clipped - 34 lines]
> Any suggestions would be appreciated.  If I'm doing this entirely
> wrong, I'd appreciate knowing that as well.  Thanks!

I figured out what I was doing wrong, and in case anybody was
wondering, there are two issues here.

First of all, I should not have been using the ComponentListener to
monitor state change.  I didn't realize that JFrames had a
WindowStateListener.  However, my code would have still worked.

The other issue I didn't realize is that, in order for a window to go
from maximized state to it's "normal" state, it had to have a previous
size and location already set for it to revert to.  Unfortunately, I
was completely ignoring the fact that I needed to set it's previous
size and location when I created the view from the config file.

...it's always the stupid errors...  :-p


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.