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

hgus1294
  • 747
  • 14
  • 26
  • 1
    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. – Andrew Thompson Feb 28 '12 at 10:47
  • I believe that you are correct on both suspicions. – hgus1294 Feb 28 '12 at 10:50
  • 1
    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 :-) – kleopatra Feb 28 '12 at 12:35
  • 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. – hgus1294 Feb 28 '12 at 14:36

1 Answers1

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 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. – hgus1294 Mar 01 '12 at 11:25
  • 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. – trashgod Mar 01 '12 at 11:31