Java Swing save and load workspace/settings
Asked Answered
E

1

7

I have a Java Swing application that contains a bunch of frames which in turn predominantly contains tables that display large amounts of data. Since it is always a hassle and its time consuming to arrange all windows and tables on startup, I would like to implement 'workspace'-functionality so that the user can save a setup of preference and on startup choose to automatically load the stored workspace to have all windows and tables appear as previously saved. Specifically, the settings that I wish to store in a workspace are:

  • Active windows (JFrame) and their sizes and positions on screen
  • Table settings, incl selected columns, column order, column width, sorting, filtering

Does anyone know of a smart and easy way to accomplish this without the obvious, and what seems like a very complex and cumbersome, solution of iterating over all open windows and saving each piece of information with the Preferences api? Thanks

Emissive answered 28/2, 2012 at 10:23 Comment(4)
There is always 'serialize the entire GUI', but I suspect that 1) there will be non serializable parts of those GUIs 2) that it will be a hassle to get working right.Heredity
I believe that you are correct on both suspicions.Emissive
you might consider using (Better) Swing Application Framework which supports storing application state - among other niceties - painlessly (though switching to a new framework might be too invasive :-)Turnery
BSAF might have been a good option if I had gone that route when I started, but suspect that it would be painful to switch now as you suggest.Emissive
D
7

In this case, the obvious solution, java.util.prefs.Preferences, is probably the correct one. RCPrefs from this game is a simple example that demonstrates saving a variety of data types, including enum. The exact implementation is highly dependent on the application. While tedious, it needn't be especially complex. For expedience, the example uses static methods; frame and table preferences are probably worth a class each.

Dynasty answered 28/2, 2012 at 12:1 Comment(2)
Thanks. The example was useful and I thought that the idea of making workspace-classes made sense. I created a WsFrame class that contains generic settings like position and size. This object can contain one or multiple specific settings like WsTable (for table settings). I opted against storing this information via Preferences and decided to simply serialize the list of WsFrame-objects via XStream and store as xml (as I am lazy and it is convenient to view/edit the output). Actually works pretty good. Thanks.Emissive
Excellent. I may be spoiled, as my platform happens to use XML to implement Preferences. Ping me if you add this as contrasting answer; I think it would be useful.Dynasty

© 2022 - 2024 — McMap. All rights reserved.