Getting sub-nodes of Preferences
suggest changeA 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:
- 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());
- 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
- 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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents