Remember window positions in swing

前端 未结 5 1490
别那么骄傲
别那么骄傲 2021-01-11 23:22

I have a pretty big swing application and i want to make it remember sizes of all windows, jframes etc. So if user resized window how he likes, next time the window looks ex

5条回答
  •  迷失自我
    2021-01-11 23:47

    Is there a better option than to write the position/size of each window in Preferences?

    No, there isn't. Don't forget to write the bounds (position/size) of the main JFrame. You could write the parameters to an XML file instead of a preferences file, but that's an implementation detail.

    Is there any convenient way to store the order of columns in a JTable?

    Write the column names and positions out to your preferences file.

    While this task is common, the implementation of this task is dependent on what you want to save from your GUI.

    The way I would save these GUI parameters would be to create a model class that contains all of the bounds and other parameters that you're interested in saving. I would read a XML file that contains these parameters and populate the fields in the model class. If there's no file, I'd set defaults.

    The GUI would use the fields in the model class to build the GUI. As the user modifies the GUI, I'd update the model class with the new values.

    When the user closes the GUI, I'd write out the model class to an XML file.

    I prefer using XML files over properties files because it's easier to see the structure of the model, and I find XML files easier to modify when the GUI changes.

提交回复
热议问题