List of usage examples for java.util.prefs Preferences put
public abstract void put(String key, String value);
From source file:MainClass.java
public static void main(String[] args) throws Exception { Preferences prefs = Preferences.userNodeForPackage(MainClass.class); prefs.put("key1", "value1"); prefs.put("key2", "value2"); prefs.putInt("intValue", 4); prefs.putBoolean("booleanValue", true); int usageCount = prefs.getInt("intValue", 0); usageCount++;//from ww w. ja v a 2 s . co m prefs.putInt("UsageCount", usageCount); Iterator it = Arrays.asList(prefs.keys()).iterator(); while (it.hasNext()) { String key = it.next().toString(); System.out.println(key + ": " + prefs.get(key, null)); } System.out.println(prefs.getInt("booleanValue", 0)); }
From source file:PreferencesDemo.java
public static void main(String[] args) throws Exception { Preferences prefs = Preferences.userNodeForPackage(PreferencesDemo.class); prefs.put("Location", "Oz"); prefs.put("Footwear", "Ruby Slippers"); prefs.putInt("Companions", 4); prefs.putBoolean("Are there witches?", true); int usageCount = prefs.getInt("UsageCount", 0); usageCount++;//from w ww . ja v a 2 s. c o m prefs.putInt("UsageCount", usageCount); Iterator it = Arrays.asList(prefs.keys()).iterator(); while (it.hasNext()) { String key = it.next().toString(); System.out.println(key + ": " + prefs.get(key, null)); } // You must always provide a default value: System.out.println("How many companions does Dorothy have? " + prefs.getInt("Companions", 0)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(String.class); // Save some values prefs.put("myString", "a string"); // String prefs.putBoolean("myBoolean", true); // boolean prefs.putInt("myInt", 123); // int prefs.putLong("myLong", 123L); // long prefs.putFloat("myFloat", 12.3F); // float prefs.putDouble("myDouble", 12.3); // double byte[] bytes = new byte[10]; prefs.putByteArray("myByteArray", bytes); // byte[] // Export the node to a file prefs.exportNode(new FileOutputStream("output.xml")); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(String.class); // Save some values prefs.put("myString", "a string"); // String prefs.putBoolean("myBoolean", true); // boolean prefs.putInt("myInt", 123); // int prefs.putLong("myLong", 123L); // long // Save some values in the parent node prefs = prefs.parent();/*from www .j a va2 s . c om*/ prefs.putFloat("myFloat", 12.3F); // float prefs.putDouble("myDouble", 12.3); // double byte[] bytes = new byte[10]; prefs.putByteArray("myByteArray", bytes); // byte[] prefs.exportSubtree(new FileOutputStream("output.xml")); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save/*from www . j av a2s . c o m*/ prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
From source file:Main.java
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(String.class); prefs.addPreferenceChangeListener(new PreferenceChangeListener() { public void preferenceChange(PreferenceChangeEvent evt) { Preferences node = evt.getNode(); String key = evt.getKey(); String newValue = evt.getNewValue(); }//from w ww .j a v a 2 s . c om }); prefs.put("key", "a string"); prefs.put("key", "a new string"); prefs.remove("key"); }
From source file:MainClass.java
public static void main(String[] args) { // Setup the Preferences for this application, by class. Preferences prefs = Preferences.userNodeForPackage(MainClass.class); // Retrieve some preferences previously stored, with defaults in case // this is the first run. String text = prefs.get("A", "a"); String display = prefs.get("B", "b"); System.out.println(text);/*w ww . j a va 2 s. c o m*/ System.out.println(display); // Assume the user chose new preference values: Store them back. prefs.put("A", "aa"); prefs.put("B", "bb"); }
From source file:LookAndFeelPrefs.java
/** * Create a menu of radio buttons listing the available Look and Feels. When * the user selects one, change the component hierarchy under frame to the new * LAF, and store the new selection as the current preference for the package * containing class c./* w w w .j ava 2s .com*/ */ public static JMenu createLookAndFeelMenu(final Class prefsClass, final ActionListener listener) { // Create the menu final JMenu plafmenu = new JMenu("Look and Feel"); // Create an object used for radio button mutual exclusion ButtonGroup radiogroup = new ButtonGroup(); // Look up the available look and feels UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels(); // Find out which one is currently used String currentLAFName = UIManager.getLookAndFeel().getClass().getName(); // Loop through the plafs, and add a menu item for each one for (int i = 0; i < plafs.length; i++) { String plafName = plafs[i].getName(); final String plafClassName = plafs[i].getClassName(); // Create the menu item final JMenuItem item = plafmenu.add(new JRadioButtonMenuItem(plafName)); item.setSelected(plafClassName.equals(currentLAFName)); // Tell the menu item what to do when it is selected item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // Set the new look and feel try { UIManager.setLookAndFeel(plafClassName); } catch (UnsupportedLookAndFeelException e) { // Sometimes a Look-and-Feel is installed but not // supported, as in the Windows LaF on Linux platforms. JOptionPane.showMessageDialog(plafmenu, "The selected Look-and-Feel is " + "not supported on this platform.", "Unsupported Look And Feel", JOptionPane.ERROR_MESSAGE); item.setEnabled(false); } catch (Exception e) { // ClassNotFound or Instantiation item.setEnabled(false); // shouldn't happen } // Make the selection persistent by storing it in prefs. Preferences p = Preferences.userNodeForPackage(prefsClass); p.put(PREF_NAME, plafClassName); // Invoke the supplied action listener so the calling // application can update its components to the new LAF // Reuse the event that was passed here. listener.actionPerformed(event); } }); // Only allow one menu item to be selected at once radiogroup.add(item); } return plafmenu; }
From source file:au.org.ala.delta.editor.EditorPreferences.java
/** * Removes the specified file from the most recently used file list * @param filename The filename to remove *//* w w w .j a v a2 s .c om*/ public static void removeFileFromMRU(String filename) { String[] existingFiles = getPreviouslyUsedFiles(); StringBuilder b = new StringBuilder(); for (int i = 0; i < existingFiles.length; ++i) { if (!existingFiles[i].equalsIgnoreCase(filename)) { if (b.length() > 0) { b.append(MRU_SEPARATOR); } b.append(existingFiles[i]); } } Preferences prefs = Preferences.userNodeForPackage(DeltaEditor.class); prefs.put(MRU_PREF_KEY, b.toString()); try { prefs.sync(); } catch (BackingStoreException e) { throw new RuntimeException(e); } }
From source file:au.org.ala.delta.editor.EditorPreferences.java
/** * Adds the supplied filename to the top of the most recently used files. * //from w w w .j a v a2 s. c o m * @param filename */ public static void addFileToMRU(String filename) { Queue<String> q = new LinkedList<String>(); q.add(filename); String[] existingFiles = getPreviouslyUsedFiles(); if (existingFiles != null) { for (String existingFile : existingFiles) { if (!q.contains(existingFile)) { q.add(existingFile); } } } StringBuilder b = new StringBuilder(); for (int i = 0; i < MAX_SIZE_MRU && q.size() > 0; ++i) { if (i > 0) { b.append(MRU_SEPARATOR); } b.append(q.poll()); } Preferences prefs = Preferences.userNodeForPackage(DeltaEditor.class); prefs.put(MRU_PREF_KEY, b.toString()); try { prefs.sync(); } catch (BackingStoreException e) { throw new RuntimeException(e); } }