Difference between revisions of "Java: Preference API"
Onnowpurbo (talk | contribs) (New page: Abstract This article gives a introduction into the Java Preference API to save key/value pairs for an application. Table of Contents 1. Java Preferences API 2. Using the API 3. Thank y...) |
Onnowpurbo (talk | contribs) |
||
Line 1: | Line 1: | ||
+ | Sumber: http://www.vogella.de/articles/JavaPreferences/article.html | ||
+ | |||
Abstract | Abstract | ||
This article gives a introduction into the Java Preference API to save key/value pairs for an application. | This article gives a introduction into the Java Preference API to save key/value pairs for an application. | ||
− | + | ==Java Preferences API== | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
The Preferences API provides a systematic way to handle program preference configurations, e.g. to save user settings, remember the last value of a field etc. | The Preferences API provides a systematic way to handle program preference configurations, e.g. to save user settings, remember the last value of a field etc. | ||
Line 24: | Line 16: | ||
The actual storage of the data is dependent on the platform. | The actual storage of the data is dependent on the platform. | ||
− | + | ||
+ | ==Using the API== | ||
java.util.prefs.Preferences can be easily used. You have to define a node in which the data is stored. Then you can call the getter and setter methods. The second value is the default value, e.g. if the preference value is not set yet, then this value will be used. | java.util.prefs.Preferences can be easily used. You have to define a node in which the data is stored. Then you can call the getter and setter methods. The second value is the default value, e.g. if the preference value is not set yet, then this value will be used. | ||
Line 30: | Line 23: | ||
Create the following program. | Create the following program. | ||
− | + | import java.util.prefs.Preferences; | |
− | import java.util.prefs.Preferences; | + | |
− | + | public class PreferenceTest { | |
− | public class PreferenceTest { | + | private Preferences prefs; |
− | + | ||
− | + | public void setPreference() { | |
− | + | // This will define a node in which the preferences can be stored | |
− | + | prefs = Preferences.userRoot().node(this.getClass().getName()); | |
− | + | String ID1 = "Test1"; | |
− | + | String ID2 = "Test2"; | |
− | + | String ID3 = "Test3"; | |
− | + | ||
− | + | // First we will get the values | |
− | + | // Define a boolean value | |
− | + | System.out.println(prefs.getBoolean(ID1, true)); | |
− | + | // Define a string with default "Hello World | |
− | + | System.out.println(prefs.get(ID2, "Hello World")); | |
− | + | // Define a integer with default 50 | |
− | + | System.out.println(prefs.getInt(ID3, 50)); | |
− | + | ||
− | + | // Now set the values | |
− | + | prefs.putBoolean(ID1, false); | |
− | + | prefs.put(ID2, "Hello Europa"); | |
− | + | prefs.putInt(ID3, 45); | |
− | + | ||
− | + | // Delete the preference settings for the first value | |
− | + | prefs.remove(ID1); | |
− | + | ||
− | + | } | |
− | + | ||
− | + | public static void main(String[] args) { | |
− | + | PreferenceTest test = new PreferenceTest(); | |
− | + | test.setPreference(); | |
− | + | } | |
− | + | } | |
− | } | ||
Latest revision as of 09:17, 5 September 2011
Sumber: http://www.vogella.de/articles/JavaPreferences/article.html
Abstract
This article gives a introduction into the Java Preference API to save key/value pairs for an application.
Java Preferences API
The Preferences API provides a systematic way to handle program preference configurations, e.g. to save user settings, remember the last value of a field etc.
Preferences are key / values pairs where the key is an arbitrary name for the preference. The value can be a boolean, string, int of another primitive type. Preferences are received and saved by get and put methods while the get methods also supply a default value in case the preferences is not yet set.
This Java Preferences API is not indented to save application data.
The Java Preference API removes the burden from the individual programmer to write code to save configuration values on the different platforms his program may be running.
The actual storage of the data is dependent on the platform.
Using the API
java.util.prefs.Preferences can be easily used. You have to define a node in which the data is stored. Then you can call the getter and setter methods. The second value is the default value, e.g. if the preference value is not set yet, then this value will be used.
Create the following program.
import java.util.prefs.Preferences; public class PreferenceTest { private Preferences prefs; public void setPreference() { // This will define a node in which the preferences can be stored prefs = Preferences.userRoot().node(this.getClass().getName()); String ID1 = "Test1"; String ID2 = "Test2"; String ID3 = "Test3"; // First we will get the values // Define a boolean value System.out.println(prefs.getBoolean(ID1, true)); // Define a string with default "Hello World System.out.println(prefs.get(ID2, "Hello World")); // Define a integer with default 50 System.out.println(prefs.getInt(ID3, 50)); // Now set the values prefs.putBoolean(ID1, false); prefs.put(ID2, "Hello Europa"); prefs.putInt(ID3, 45); // Delete the preference settings for the first value prefs.remove(ID1); } public static void main(String[] args) { PreferenceTest test = new PreferenceTest(); test.setPreference(); } }
Run the program twice. The value of "ID1" should be still true as we delete it. The value of "ID2" and "ID2" should have changed after the first call.