Wednesday, October 15, 2008

How to save/load JTable settings

Preamble 

This articles will show how to save JTable preferences as per user preferences.

JDK and eclipse compatibility

JDK version 1.3 with eclipse 3.3 is the minimum requirement.

Project overview 

The downloadable zip file contains an eclipse project which has a MainFrame class for GUI. By modifying the size of the colums and doing a rerun will create a new Table which will have the saved preferences from the last run.

To save the preferences click the save button or the save menu item. The next load will have the changes reflected. Unzip the downloadable and load in the eclipse project. Make a run configuration for MainFrame class and you are all set.

Enjoy, if you like it please appreciate!

The snippet below is for sample usage.

table = new StarkTable(data, columnNames);
table.setName("Prefs table");
JTablePrefsPersister prefsTable = JTablePrefsPersister.getInstance();
prefsTable.loadPreferences(System.getProperty("user.home")
    + File.separator + "table.pref");
prefsTable.registerTable(table);
contentPane.add(new JScrollPane(table), BorderLayout.CENTER);

No comments:

Post a Comment