Java USES the Preference class to hold the last record

  • 2020-04-01 03:52:18
  • OfStack

This article illustrates how Java USES the Preference class to save the last record. Share with you for your reference. Specific analysis is as follows:

When selecting a file using JFileChooser in Java, we always want to be able to save the record of the last time we viewed it the next time we opened it, that is, when we opened the file dialog box, we always want to be able to trace back to the previous path.

A silly way to do this is to save the path of the selected file to a local file every time you open it, and when you open the JFileChooser dialog, check to see if there is anything in the file, and if there is something in the file, open the dialog by the stored path.

Would you believe me if I told you that you could operate the Windows registry in Java without using JNI's methods? Many software menus have options like "Settings" or "Preferences" to set or modify the configuration of the software. This configuration information can be saved to a configuration file like the one described above or, in the case of Windows, to the system registry. Starting with JDK 1.4, Java included a java.util.prefs package under java.util that deals specifically with user and system configuration information. One of the classes, Preferences, is a more "advanced" thing.

In essence, the Preferences is itself a has nothing to do with the platform, but different OS SPI of it (the Service Provider Interface) implementation is related to the platform, as a result, you may see in the different system Preferences saved as a local file, LDAP directory, database entries, etc., as in Windows platform, it will save the system registry. Not only that, you can also export your preferences to an XML file or import them from an XML file.

The systemNodeForPackage() // gets a Preferences object based on the specified Class object, whose registry path begins at "HKEY_LOCAL_MACHINE\"

SystemRoot () // gets the Preferences object with the registry path HKEY_LOCAL_MACHINE\SOFTWARE\Javasoft \Prefs as the root

UserNodeForPackage () // gets a Preferences object based on the specified Class object, whose registry path begins at "HKEY_CURRENT_USER\"

UserRoot () // gets the Preferences object with the registry path HKEY_CURRENT_USER\SOFTWARE\Javasoft \Prefs as the root

The following code, from the web, briefly illustrates the use of the Preference class


import java.util.prefs.Preferences;
public class PreferrenceTest { 
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); 
System.out.println(prefs.get(ID2, "")); 
}
public static void main(String[] args) { 
PreferrenceTest test = new PreferrenceTest(); 
test.setPreference(); 
} 
}

Here's how to implement select file save last path


Preferences pref = Preferences.userRoot().node(this.getClass().getName()); 
String lastPath = pref.get("lastPath", ""); 
   JFileChooser chooser = null; 
   if(!lastPath.equals("")){ 
   chooser = new JFileChooser(lastPath); 
   } 
   else 
   chooser=new JFileChooser();
//MyFileFilter is a file filter class that only accepts XLS files
    MyFileFilter filter = new MyFileFilter("xls"," We only accept xls Format file , namely Excel 2003 Version of the file ");
   chooser.setFileFilter(filter);
 int state; //The file selector returns status
 state=chooser.showOpenDialog(null);//Displays the open file dialog box
 File file = chooser.getSelectedFile(); //Gets the selected file
 pref.put("lastPath",file.getPath());
import java.io.File;
import javax.swing.filechooser.FileFilter;
//File filter
public class MyFileFilter extends FileFilter
{
 public String ends; //The file suffix
 public String description; //File description text
  public MyFileFilter (String ends, String description)
  { //The constructor
    this.ends = ends; // Set up the The file suffix
    this.description=description; // Set up the File description text
  }
  public boolean accept (File file)
  { //Overload the accept method in the FileFilter
    if (file.isDirectory ()) //Returns true if it is a directory
      return true;
    String fileName = file.getName (); //Get the file name
    if (fileName.toUpperCase ().endsWith (ends.toUpperCase ()))
    // the The file suffix Comparison with acceptable suffixes converted to uppercase 
      return true;
    else
      return false;
  }
 public String getEnds() {
 return ends;
 }
 public void setEnds(String ends) {
 this.ends = ends;
 }
 public String getDescription() {
 return description;
 }
 public void setDescription(String description) {
 this.description = description;
 }
}

I hope this article has been helpful to your Java programming.


Related articles: