Your Computer Science Resource

HOW-TO: Save User Preferences with Java’s Preferences API

Users have certain expectations of your program. One of those expectations might be to have their preferences saved. This might include their username and password, the location of the application window, the size of the window, etc. Java’s Preferences API is an easy way to accomplish tasks like this without having to manually write and later read / parse a config file. This method is also platform independent; we don’t have to worry about where or how the data is saved — just that it is being saved and that it is persistent. Let’s see how to do it!

The Code

In the below class, I will demonstrate how to use the Preferences API, allowing us to save a user’s username, password, and the application’s window size and location.

(Note that for this example, we’re not using any sort of encryption to store the password. I would not recommend this in a real application).

import java.util.prefs.Preferences;

public class AppPreferences {

	private String password = null;
	private Preferences preferences = null;
	private String username = null;
	private int windowHeight;
	private int windowWidth;
	private int windowX;
	private int windowY;

	/**
	 * Constructor
	 */

	public AppPreferences() {

		this.username = getPreferences().get("username", null);
		this.password = getPreferences().get("password", null);
		this.windowHeight = getPreferences().getInt("window_height", -1);
		this.windowWidth = getPreferences().getInt("window_width", -1);
		this.windowX = getPreferences().getInt("window_x", -1);
		this.windowY = getPreferences().getInt("window_y", -1);

	} // end constructor

	/**
	 * This method returns the password.
	 *
	 * @return String password
	 */

	public String getPassword() {

		return password;

	} // end method getPassword

	/**
	 * This method returns a reference to
	 * the Preferences object.
	 *
	 * @return Preferences preferences
	 */

	public Preferences getPreferences() {

		if ( preferences == null ) {

			preferences = Preferences.systemNodeForPackage( AppPreferences.class );

		}

		return preferences;

	} // end method getPreferences

	/**
	 * This method gets the user name.
	 *
	 * @return String username
	 */

	public String getUsername() {

		return username;

	} // end method getUsername

	/**
	 * This method returns the
	 * window's height;
	 *
	 * @return int windowHeight
	 */

	public int getWindowHeight() {

		return windowHeight;

	} // end method getWindowHeight

	/**
	 * This method returns the window's
	 * width.
	 *
	 * @return int windowWidth
	 */

	public int getWindowWidth() {

		return windowWidth;

	} // end method getWindowWidth

	/**
	 * This method returns the window's
	 * X position.
	 *
	 * @return int windowX
	 */

	public int getWindowX() {

		return windowX;

	} // end method getWindowX

	/**
	 * This method returns the window's
	 * Y position.
	 *
	 * @return int windowY
	 */

	public int getWindowY() {

		return windowY;

	} // end method getWindowY

	/**
	 * This method sets the password.
	 *
	 * @param String password
	 */

	public void setPassword( String password ) {

		this.password = password;
		getPreferences().put("password", password);

	} // end method setPassword

	/**
	 * This method sets the user name.
	 *
	 * @param String username
	 */

	public void setUsername( String username ) {

		this.username = username;
		getPreferences().put("username", username);

	} // end method setUsername

	/**
	 * This method sets the window's
	 * height.
	 *
	 * @param int height
	 */

	public void setWindowHeight( int height ) {

		this.windowHeight = height;
		getPreferences().putInt("window_height", height);

	} // end method setWindowHeight

	/**
	 * This method sets the window's
	 * width.
	 *
	 * @param int width
	 */

	public void setWindowWidth( int width ) {

		this.windowWidth = width;
		getPreferences().putInt("window_width", width);

	} // end method setWindowWidth

	/**
	 * This method sets the window's
	 * X position.
	 *
	 * @param int x
	 */

	public void setWindowX( int x ) {

		this.windowX = x;
		getPreferences().putInt("window_x", x);

	} // end method setWindowX

	/**
	 * This method sets the window's
	 * Y position.
	 *
	 * @param int y
	 */

	public void setWindowY( int y ) {

		this.windowY = y;
		getPreferences().putInt("window_y", y);

	} // end method setWindowY

} // end class AppPreferences

Then in your GUI class file, you can add a method to be called upon exiting, such as:

	/**
	 * This method saves all of the window
	 * properties in the preferences.
	 */

	public void saveWindowPreferences() {

		// Assuming getPreferences() returns a reference to the AppPreferences class

		getPreferences().setWindowHeight( getHeight() );
		getPreferences().setWindowWidth( getWidth() );
		getPreferences().setWindowX( getX() );
		getPreferences().setWindowY( getY() );

	} // end method saveWindowPreferences

To save the user’s name and password, you can do something like this:

	/**
	 * This method saves the user's name and password
	 */

	public void saveUser() {

		// Assuming getPreferences() returns a reference to the AppPreferences class

		getPreferences().setUsername( getUsername() ); // where getUsername() returns the user's name
		getPreferences().setPassword( getPassword() ); // where getPassword() returns the user's password

	} // end method saveUser

Where are preferences saved?

It depends on the platform. For Windows, this is saved in the registry as:

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs

On *nix systems, this is usually saved as a file:

/etc/.java/.systemPrefs

Questions, comments? Leave them below.

Enjoy!

Tags: , ,

Leave a Reply