DevFaqNonSingletonTopComponents

(Difference between revisions)
(Netbeans 6.8: formatting)
(How can I change my TopComponent to not be a singleton?)
Line 14: Line 14:
* Delete the <tt>writeReplace()</tt> method (typically towards the end of the file)
* Delete the <tt>writeReplace()</tt> method (typically towards the end of the file)
* Delete the <tt>ResolvableHelper</tt> inner class (typically towards the end of the file)
* Delete the <tt>ResolvableHelper</tt> inner class (typically towards the end of the file)
-
* Only singleton TopComponents [http://openide.netbeans.org/servlets/ReadMsg?list=dev&msgNo=38844 can have a persistence mode of PERSISTENCE_ALWAYS], so you must locate the <tt>getPersistenceType</tt> method and change its return value to either <tt>[http://bits.netbeans.org/dev/javadoc/org-openide-windows/org/openide/windows/TopComponent.html#PERSISTENCE_NEVER TopComponent.PERSISTENCE_NEVER]</tt> or <tt>[http://bits.netbeans.org/dev/javadoc/org-openide-windows/org/openide/windows/TopComponent.html#PERSISTENCE_ONLY_OPENED TopComponent.PERSISTENCE_ONLY_OPENED]</tt>.
+
* '''TimBoudreau: PENDING - this is wrong, but such persistence should be done rarely and carefully and is almost never what you want.''' Only singleton TopComponents [http://openide.netbeans.org/servlets/ReadMsg?list=dev&msgNo=38844 can have a persistence mode of PERSISTENCE_ALWAYS], so you must locate the <tt>getPersistenceType</tt> method and change its return value to either <tt>[http://bits.netbeans.org/dev/javadoc/org-openide-windows/org/openide/windows/TopComponent.html#PERSISTENCE_NEVER TopComponent.PERSISTENCE_NEVER]</tt> or <tt>[http://bits.netbeans.org/dev/javadoc/org-openide-windows/org/openide/windows/TopComponent.html#PERSISTENCE_ONLY_OPENED TopComponent.PERSISTENCE_ONLY_OPENED]</tt>.
Next, open the settings file that the wizard generated for your TopComponent. This is typically named something like <tt>FooTopComponentSettings.xml</tt>.  Locate the <tt>instance</tt> XML element (NOT one of the <tt>instanceof</tt> elements and remove <tt>method="getDefault"</tt> from the end of that line.   
Next, open the settings file that the wizard generated for your TopComponent. This is typically named something like <tt>FooTopComponentSettings.xml</tt>.  Locate the <tt>instance</tt> XML element (NOT one of the <tt>instanceof</tt> elements and remove <tt>method="getDefault"</tt> from the end of that line.   

Revision as of 14:21, 11 March 2010

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)
  • TimBoudreau: PENDING - this is wrong, but such persistence should be done rarely and carefully and is almost never what you want. 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.

Netbeans 6.8

This modified recipe seems to work in 6.8:

  • In your TopComponent's .java source file:
    • Delete the @ConvertAsProperties annotation
    • Delete the static instance variable, which ought to be declared a few lines above the constructor.
    • Delete the getDefault() method (typically somewhere around the middle of the file)
    • Delete the findInstance() method (which typically follows the getDefault() method)
    • Delete the writeProperties() method (typically towards the end of the file)
    • Delete the readProperties 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.
  • In the settings file, locate the instance XML element (NOT one of the instanceof elements and remove method="getDefault" from the end of that line.
  • When creating a new instance of the components use:
      TopComponent win = new TopComponent();
      win.open();
      win.requestActive(); 

<math>Insert formula here</math>

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