Monday, December 11, 2006

NetBeans Platform: Node.Property custom editor

The property window is a nice quick way to let the user view and edit some values. It's not recommended as a valid approach (one should make its own windows) but it is quick. Just provide some activated nodes, open the Properties windows and all the declared PropertySets will be visible.

The nice part is that we already have a lot of predefined PropertyEditors for boolean/integer/string/font/color/date values.

But if one of the editors doesn't please us, just use Node.Property.getPropertyEditor() .

For example if we need a different date format, it would boil down to a new class:


class DatePropertyEditor extends PropertyEditorSupport{
private SimpleDateFormat sdf=new SimpleDateFormat("yyyy/mm/dd");

public String getAsText(){
if(getValue()==null){
return "";
}else{
return sdf.format((Date)getValue());
}
}
}


We just return a new instance of this class in getPropertyEditor() and that's it.

This gets a little more complicated if you actually need to edit the value as you have to provide a custom editor component but for simple read-only properties, this is all there is !

No comments: