My blog has moved! Redirecting...

You should be automatically redirected. If not, visit http://mindsiview.wordpress.com/ and update your bookmarks.

Tuesday, January 30, 2007

Using the Preferences API

Like many Java developers, I tend to find something that works for me and use it for years without researching better approaches. For example, I've used the Properties class for years to load and save configuration information in my Java programs. The Properties class works fine, but does have some drawbacks.

Last week I stumbled across another API, the Preferences class, that's been available since 1.4. It can be used the same way as the Properties class, to load and save configuration stuff. The big difference between it and Properties is that the Preference class allows you to store data in a consistent format, it allows you to take advantage of centralized repositories such as the Windows registry, and provides more flexibility with regards to storing the preferences in user folders.

Without going into a lot of detail, I've created a simple swing application that demonstrates the basics of the Preferences class. All the program TestPrefs does is save an loads the most recent window size and location.

Looking at the code below, I've highlighted the Preferences api calls for you. As you can see, the first thing to do is to decide where you want the preferences file to be located. In this case, I've chosen user root, which means that I've decided to store the preferences file under the user tree rather than the system tree. Next, I've told the program to store the file in the root folder of TestPrefs class. You can also specify a folder with something like node = root.node("/com/prefs/...").

Now that the class knows where to locate the preferences file, how do we write information to it? Looking at the windowClosing() method, you'll see node.putInt(..). The putInt() method has two parameters: name and value. For example, if you wanted to save a frame's width, you might create a name value/pair like this: node.putInt("width", jFrame.getWidth()).

Next, we want to read information from the preferences file and use it to position our window on the next execution of the program. looking at the getJFrame() method, you can see the jFrame.setSize() method. This time I use node.getInt(). Like putInt(), getInt() has a name/value pair. This time it works a little differently. In this case, the value parameter acts as a default value.The name parameter tries to find the associated value within the preferences file. If it can't, then the value parameter is used.

There are other methods associated with the Preferences api, and I encourage you to explore these. Hopefully, I've whetted your appetite enough to get started.

___________________

package test;

import java.awt.BorderLayout;
import javax.swing.SwingUtilities;
import java.util.prefs.Preferences;

import javax.swing.JPanel;
import javax.swing.JFrame;

public class TestPrefs {

private JFrame jFrame = null;

private JPanel jContentPane = null;

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
public static final int DEFAULT_LEFT = 0;
public static final int DEFAULT_TOP = 0;

Preferences root = Preferences.userRoot();
 final Preferences node = Preferences.userNodeForPackage(this.getClass());

/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(node.getInt("width", DEFAULT_WIDTH),node.getInt("height", DEFAULT_HEIGHT));
jFrame.setLocation(node.getInt("left",DEFAULT_LEFT), node.getInt("top",DEFAULT_TOP));
jFrame.setContentPane(getJContentPane());
jFrame.setTitle("Test Preferences API");
jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
node.putInt("left", jFrame.getX());
     node.putInt("top", jFrame.getY());
     node.putInt("width",jFrame.getWidth());
     node.putInt("height", jFrame.getHeight());
}
});
}
return jFrame;
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
}
return jContentPane;
}



/**
* Launches this application
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestPrefs application = new TestPrefs();
application.getJFrame().setVisible(true);
}
});
}

}

3 comments:

Anonymous said...

thanks a lot for the code example, very helpful

Rick said...

Great. I usually try to post stuff that I think will be useful to others...and that I might need to refer to later.

Anonymous said...

helped me a lot in my program, wanted something to help me store. this definitely helped.
thanks.

Technorati search