[ Back | Previous | Next ]

How to create a bean editor?

Package:
java.beans.*
Product:
JDK
Release:
1.1.x
Related Links:
PropertyEditorSupport
Comment:
Step 2. Create NervousText07TextPropertyEditor

                     Start by creating a file called:
NervousText07TextPropertyEditor.java. Implement the class skeleton:
                        package sun.beanbox.beans;
                         import java.beans.*;

                         public class NervousText07TextPropertyEditor
                             extends PropertyEditorSupport {
                             ....
                           }
                         }

 
Note that NervousText07TextPropertyEditor.java extends the PropertyEditorSupport class that implements the PropertyEditor interface. Next override the getTags method. This method simply returns a list of strings that the property editor will display when a programmer tries to edit the text property of NervousText07 in a builder tool.
                           public String[] getTags() {
                             String values[] = {
                               "Nervous Text",
                               "Anxious Text",
                               "Funny Text",
                               "Wobbly Text"};
                             return values;
                           }
Another example:
import java.beans.*;
                       import PlotSpot;
                       // Note we have to import all PlotSpot subclasses here, since
                       // we "new" them.
                       import PSBox;
                       import PSHappy;
                       import PSTri;

                       public class PlotSpotEditor extends PropertyEditorSupport 
                       {
                           protected String myName;

                           // Create an editor.
                           public PlotSpotEditor()
                           {
                               super();
                               myName = new String("Box");
                           }

                           // Return valid names of PlotSpotEditor subclasses.
                           public String[] getTags()
                           {
                               System.out.println("PlotSpotEditor.getTags");
                               String[] spots = {
                                   "Box",
                                   "Happy",
                                   "Tri"
                               };
                               return spots;
                           }

                           // Given the name of a PlotSpot, set the value of the
                           // object we're editing, and then fire a property change event
                           // at the bean.
                           public void setAsText(String sValue)
                           {
                               System.out.println("PlotSpotEditor.setAsText(" + sValue + ")");
                               myName = sValue;
                               if (sValue == "Box") {
                                   setValue(new PSBox());
                               } else if (sValue == "Happy") {
                                   setValue(new PSHappy());
                               } else if (sValue == "Tri") {
                                   setValue(new PSTri());
                               }
                               firePropertyChange();
                           }

                           // Get the name of the PlotSpot being edited
                           public String getAsText()
                           {
                               System.out.println("PlotSpotEditor.getAsText");
                               return myName;
                           }

                           // Override this just so we can print a message (to demonstrate
                           // that the IDE is calling it.)
                           public void addPropertyChangeListener(PropertyChangeListener p)
                           {
                               System.out.println("PlotSpotEditor.addPropertyChangeListener");
                               super.addPropertyChangeListener(p);
                           }
                       };
1