Getting sub-nodes of Preferences

suggest change

A Preferences object is one node in a tree:

/userRoot
├── com
│   └── mycompany
│       └── myapp
│           ├── darkApplicationMode=true
│           ├── showExitConfirmation=false
│           └── windowMaximized=true
└── org
    └── myorganization
        └── anotherapp
            ├── defaultFont=Helvetica
            ├── defaultSavePath=/home/matt/Documents
            └── exporting
                ├── defaultFormat=pdf
                └── openInBrowserAfterExport=false

node creates any missing ancestor. getClass() below is an instance method.

To select the /com/mycompany/myapp node:

  1. By convention, from the package of a class:
package com.mycompany.myapp;

// …

// This class is in com.mycompany.myapp, so the node is /com/mycompany/myapp.
Preferences myApp = Preferences.userNodeForPackage(getClass());
  1. By relative path:
Preferences myApp = Preferences.userRoot().node("com/mycompany/myapp");

A path that does not start with / is resolved from the node you call node on. This returns /one/two/three/com/mycompany/myapp:

Preferences prefix = Preferences.userRoot().node("one/two/three");
Preferences myAppWithPrefix = prefix.node("com/mycompany/myapp");
// prefix is /one/two/three
// myAppWithPrefix is /one/two/three/com/mycompany/myapp
  1. By absolute path:
Preferences myApp = Preferences.userRoot().node("/com/mycompany/myapp");

On the root node, an absolute path and the same path without the leading / name the same node. On a sub-node, a leading / still starts at the root:

Preferences prefix = Preferences.userRoot().node("one/two/three");
Preferences myAppWithoutPrefix = prefix.node("/com/mycompany/myapp");
// prefix is /one/two/three
// myAppWithoutPrefix is /com/mycompany/myapp

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents