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