Revision as of 21:34, 4 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

How can I change my TopComponent to not be a singleton?

The "New Window Component" wizard in the NetBeans IDE generates a singleton TopComponent. That's fine for some uses, but often you will want to create multiple instances of your TopComponent.

The good news is that you won't have to write any code -- you'll just have to delete some of the code that was generated for you.

In your TopComponent's .java source file:

  • Delete the static instance variable, which ought to be declared a few lines above the constructor.
  • Make constructor public
  • Delete the getDefault() method (typically somewhere around the middle of the file)
  • Delete the findInstance() method (which typically follows the getDefault() method)
  • Delete the writeReplace() method (typically towards the end of the file)
  • Delete the ResolvableHelper inner class (typically towards the end of the file)
  • Only singleton TopComponents can have a persistence mode of PERSISTENCE_ALWAYS, so you must locate the getPersistenceType method and change its return value to either TopComponent.PERSISTENCE_NEVER or TopComponent.PERSISTENCE_ONLY_OPENED.

Next, open the settings file that the wizard generated for your TopComponent. This is typically named something like FooTopComponentSettings.xml. Locate the instance XML element (NOT one of the instanceof elements and remove method="getDefault" from the end of that line.

Finally, you will need to change any code, such as from the action which opens the TopComponent, which called the getDefault() method. It should now simply create a new instance of your TopComponent instead.

NOTE: These instructions should apply to NetBeans 5.0 through 6.5. You may need to adapt them slightly for newer versions.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo