0

I want to have my windows and dialogs open in the same size and location as their last usage. I am saving and restoring the size and location, but I'm not sure where best to restore them.

If I call setSize and setLocation inside the constructor, I get an "Overridable method call in constructor" warning. I understand why that is bad, so I want to make the call elsewhere. I've tried various window events such as windowOpened, componentShown, windowStateChanged, but they all have the effect of the window being brought up in the corner of the screen and then jumping to the desired location.

The best option I've come across is to make the class final. Then I can safely put the setSize and setLocation in the constructor. That feels like a cop out. The only other thing I've thought of is to put the burden on the caller, so instead of the usual:

new myDialog().setVisible (true);

make it

var dlg = new MyDialog ();
dlg.setSize (...);
dlg.setLocation (...);
dlg.setVisible (true);

But that's really putting the burden in the wrong place.

Is there an event that I'm missing that is invoked before the window is initially painted?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
PhotoKevin
  • 97
  • 2
  • 9
  • Why don't you pass a `Dimension` and a `Point` objects as parameters in its constructor? – George Z. May 15 '20 at 19:12
  • For tips, see the [mre] in my answer to [Best practice for setting JFrame locations](https://stackoverflow.com/q/7777640/418556). – Andrew Thompson May 15 '20 at 23:55
  • @GeorgeZ.- Then I'd end up with `MyDialog (Dimension d, Point p) { setSize (d); // This gives the compiler warning I'm trying to avoid setLocation (p); // This also gives the compiler warning I'm trying to avoid }` Sorry, I can't seem to get that to format correctly in a comment. – PhotoKevin May 17 '20 at 15:17
  • @AndrewThompson - I must not be understanding. Your example seems to be placing the burden on the caller which was one of the things I mentioned as being undesirable. – PhotoKevin May 17 '20 at 15:23

0 Answers0