Home Java Example Java Util Access the User's node and Retrieve the Preference Information in Class
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Access the User's node and Retrieve the Preference Information in Class

Advertisement
In this section you will learn how to access the user's node and retrieve the stored information in the class.

Listening for Changes to Preference Values in a Preference Node

     

In this section you will learn how to access the user's node and retrieve the stored information in the class. Here we are going to make it easier to understand by the complete example on this topic here we illustrate all the methods used in this example and also explain that what are the uses of them so be attentive on all the step for fully trained in preferences.

In this section you will see about the way of the access user's preference node and and retrieving all information for the appropriate node. Here an example is also provided for learning about the topic in detailed form with line by line explanation.

First of all we import a package for using it's classes and methods which are not accessible by default. For using specific class we have to import it's package i.e. as follows:

import java.util.prefs.*;

The second step is in the main part we use the NodeChangeListener by definition "A listener which is for receiving preference node change events". In The given code line we are using the NodeChangeListenr and also make a object of that and then after adding new we make a constructer and the we define the next two part of the program. 

NodeChangeListener nodeChangeListener = new NodeChangeListener()

 They are work in there separate manner the descriptions of them is as under first of them is the childAdded(NodeChangeEvent event) and by the definition It is the method which gets called when a child node is added .

childAdded(NodeChangeEvent event)

As we earlier mention that the two part of the Node change listener have two method and they are work in there separate manner the descriptions of them is as under Second of them is the childRemoved(NodeChangeEvent event) and by the definition It is the method which gets called when a child node is removed .

childRemoved(NodeChangeEvent event)

In above assumptions, both have the event which is a parameter and by definition "It is A node change event object describing the parent and child node".

Preferences parent = event.getParent();

Preferences child  = event.getChild();

In the above lines of the code getparent() and getchild() are method used to gets the parent items that contains the collection and the same work done by the getchild() method.

Retrieving a Preference Node:
  

Preferences prefs = Preferences.userRoot().node("roseindia");

In the same example we can easily understand that how a Preference node can be access using a class object or a string and the string used to access a preference node is called path and same as a file path. In the given example the root node of the class is define that is why here it is access by the absolute path. There are so many way to access or retrieve the root of the reference node. All of them are define as under while one of them are simply show in our example.
There are also the two type of the Preference node first is the System Preference nodes and the second is the User Preference nodes and there many different method to retrieve the nodes are as under --

  //  System Preference nodes
  
    // Retrieve method of Using a Class
  Preferences prefs = Preferences.systemNodeForPackage(
java.lang.String.class);
  
  // Retrieve method of Using an absolute path
  prefs = Preferences.systemRoot().node(
"/java/lang/String");
  
  //  Retrieve method of Using a relative path
  prefs = Preferences.systemRoot().node(
"/javax/swing");
  prefs = prefs.node(
"text/html");
  
  // User preference nodes
  
  // Retrieve method of Using a class
  prefs = Preferences.userNodeForPackage(
roseindia.MyClass.class);
  
  // Retrieve method of Using an absolute path
  prefs = Preferences.userRoot().node(
"roseindia");
  
  // Retrieve method of Using a relative path
  prefs = Preferences.userRoot().node(
"/javax/swing");
  prefs = prefs.node(
"doc/html");

All of the above mention method of the retrieving of the node either it is related to the user node or it is related to the system root it can be access by the same process and you can easily know that what is the procedure of that method for work.

Here is the complete code of the program:

import java.util.prefs.*;
public class Prefs
  {
  public static void main(String args[]) 
  {
  String employee[] {"HR""Programmer""Team Leadere"};
  String post[] {"Deepak""Amit""Chandan"};
  NodeChangeListener nodeChangeListener = new NodeChangeListener()
  {
  public void childAdded(NodeChangeEvent event) {
  Preferences parent = event.getParent();
  Preferences child  = event.getChild();
 }
 public void childRemoved(NodeChangeEvent event
 {
  Preferences parent = event.getParent();
  Preferences child  = event.getChild();
 }
  };
  PreferenceChangeListener preferenceChangeListener = 
  new 
PreferenceChangeListener() 
  {
  public void preferenceChange(PreferenceChangeEvent event
  {
  String key = event.getKey();
  String value = event.getNewValue();
  Preferences node = event.getNode();
  System.out.println(node.name() 
  + 
" company has a employee named " 
 
+ value + " As a " + key);
  }
  };
  // Here is the user root define
  Preferences prefs = Preferences.userRoot().node("roseindia");
  // Here we are just add the listeners
  prefs.addNodeChangeListener(nodeChangeListener);
  prefs.addPreferenceChangeListener(preferenceChangeListener);
  // Save a bunch of key-value pairs
  for (int i=0, n=employee.length; i < n; i++) {
  prefs.put(employee[i], post[i]);
  }

 }
}
 

After thoroughly knowing the code you should save the file by "Prefs.java" file name and when after compiling the code you run your program you will get the output given below .

Here is the Output of the program:


C:\anshu>javac prefs.java

C:\anshu>java prefs
roseindia company has a employee named Deepak As a HR
C:\anshu>

Downoload this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 27, 2008

Ask Questions?    Discuss: Access the User's node and Retrieve the Preference Information in Class  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com