Playing with Persistence Across Sessions

There's an interesting bunch of code that you get when you use the "Window Component wizard", code that I had never understood before. When you use the wizard, you get a subclass of the TopComponent class, including a lot of code, which are stubs for your own implementations of the TopComponent abstract methods. One of these methods is writeReplace(), which instantiates a new serializable class called ResolvableHelper. This is exactly what this part of the generated code looks like by default:

public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;

public Object writeReplace() {
    return new ResolvableHelper();

final static class ResolvableHelper implements Serializable {
    private static final long serialVersionUID = 1L;
    public Object readResolve() {
        return DemoTopComponent.getDefault();

Let's say you have two JTextFields in your TopComponent. You would then modify the generated writeReplace() method as follows:

public Object writeReplace() {
    return new ResolvableHelper(

So, now you're passing the value of your two text fields to the ResolvableHelper, which means you need to create a constructor so that this class can receive the two strings. All the changes to the code are highlighted:

final static class ResolvableHelper implements Serializable {

    private static final long serialVersionUID = 1L;
    private final String text1;
    private final String text2;

    ResolvableHelper(String text1, String text2) {
        this.text1 = text1;
        this.text2 = text2;

    public Object readResolve() {
        return DemoTopComponent.getDefault();

So, now that we have our strings in the ResolvableHelper class, let's use the Serializable's readResolve() method:

public Object readResolve() {
    DemoTopComponent result = DemoTopComponent.getDefault();
    return result;

Now install the module, type some text in both text fields and notice that, when you restart the IDE, the text that you typed has been preserved. The key to the whole story is this: Because of TopComponent.PERSISTENCE_ALWAYS in the getPersistenceType() method, you are indicating that the TopComponent should be saved when the IDE (or your own application on the NetBeans Platform!) shuts down. If you don't define a writeReplace() method, nothing will be preserved when the application starts again. However, when you define the writeReplace() method, you can create a serializable class that will store your data across restarts.

I think this is a pretty powerful mechanism, especially since it is so easy to use (once you know how to use it). By the way, this is one of the cool things I'm learning while reviewing one of the chapters in the upcoming NetBeans Platform book. Yes, this is the level of detail that this book covers. No lofty theoretical treatise, but a practical handbook for every day use. Hurray.


Great! That's exactly the kind of information we need. I'm really looking forward to your book! cheers, Toni

Posted by Toni Epple on December 17, 2006 at 08:42 PM PST #

Looking forward to read the book!

Posted by Superfrog on February 07, 2007 at 09:45 PM PST #

I've tried it in NetBeans 6.7.
The wizard creating the Top Component does not insert the method writeReplace neither the inner class ResolvableHelper in the source code.
I've pasted the code of the example anyway, inserting the @override annotation on the writeReplace method as suggested by the ide.
It doesn't work: no errors, but the text is not preserved.
What I've got wrong?

Posted by Cencio on August 16, 2009 at 11:13 PM PDT #

Superfrog, try removing the annotation @ConvertAsProperties ...

I haven't tried but that's what I read somewhere..

Posted by cheng on August 17, 2009 at 11:05 PM PDT #

I've tried it and now it works.

Posted by Cencio on August 18, 2009 at 03:13 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2016