Geertjan's Blog

  • October 30, 2006

Preferences API and NetBeans IDE (Part 1)

Geertjan Wielenga
Product Manager
In 6.0, NetBeans will come with its own Preferences API so that, for example, preferences will be stored in the NetBeans user directory. In 5.5, however, module developers must use the standard JDK Preferences API. (There are other approaches, such as the System Option class, which is deprecated in everything but name. But all other approaches are harder than using the Preferences API.) Today I learnt how to do that and it is really easy (especially for those who recall the tortuous approach described here).

Here's what you do.

  1. Create a module project and use the Options Panel wizard to generate an Options Panel extension. (Doesn't matter whether you choose "Primary Panel" or not, the procedure for adding options is the same.)

  2. Next, design the visual panel. This is what the user will see and what the user will interact with. Let's say, for example, that you need them to set the executable used by a tool that is integrated by your module. In this scenario, you might have a visual panel that looks like this:

  3. Now open the visual panel in the Source mode. Assuming your TextField is called jTextField1 and that your panel is called MymodulePanel, stick this into the load() method:

    jTextField1.setText(Preferences.userNodeForPackage(MymodulePanel.class).get("SOME_KEY", ""));

    Now put this in the store() method:

    Preferences.userNodeForPackage(MymodulePanel.class).put("SOME_KEY", jTextField1.getText());

    For details on the above two lines of code, see the mountains of documentation on the Preferences API, all over the web. The OptionsPanelController class, one of the classes created by the Options Panel wizard, uses the update() and applyChanges() methods to call the load and store() methods in the visual component.

  4. Now create something that's going to use your persisted setting. For example, let's assume we have a TopComponent that lets the user do lots of stuff, including being able to see the selected executable:

  5. Now go to the Source view of the TopComponent. In the constructor, add these lines of code at the end:

    Preferences pref = Preferences.userNodeForPackage(MymodulePanel.class);
    String s = pref.get("SOME_KEY", "");
    pref.addPreferenceChangeListener(new PreferenceChangeListener() {
    public void preferenceChange(PreferenceChangeEvent evt) {
    if (evt.getKey().equals("SOME_KEY")) {

    Here, we use the same key as used in the Options window to find the related value. Then we put the value in the TextField. We also use a preferences change listener, and if the change comes from our key, we get the new value from the key.

  6. Install the module. Open the TopComponent. Go to the Options window and choose an executable. Close the Options window and notice that the specified executable appears in the TopComponent, thanks to the preferences change listener. Close the IDE and start it again. Notice that the executable is persisted and that it appears in the TopComponent and in the Options window.

  7. Now you can look at the preference, where it is stored in your system by the Preferences API. Under Windows, you can look at regedit:

    Notice that we have a node there called "SOME_KEY". That's the one we created in the Options window and worked with in the TopComponent. In 6.0, you won't need to go digging around in regedit (or wherever your system puts the preference). Instead, there'll be a new entry in config/preferences in your user directory.

Thanks to Radek and Martin for help with this topic.

Join the discussion

Comments ( 3 )
  • bernhard huber Monday, October 30, 2006

    Interesting blog, creating a preference is really simple.

    But ....

    The preference API might be easy to use, but in some corporate environment, client PCs are not allowed to access the registry at all.

    So keep this in mind before using the Preference API.
  • Michel Graciano Monday, October 30, 2006
    Another thing, for example, today we can copy a userdir for another machine and all things are similar as source IDE. There is a way, to copy or share this things with another machines or do backups?

  • Geertjan Tuesday, October 31, 2006
    Thanks for the comments. Yes, there are disadvantages with this approach. See part 2, about the NbPreferences class, which solves this problem.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.