Close

Java - PropertyEditorManager Examples

Java 

This example show how to use PropertyEditorManager to find a property editor for a given type. The JDK provided editor implementations, BooleanEditor in this case, are in unofficial package com.sun.beans.editors.BooleanEditor.

package com.logicbig.example;

import java.awt.*;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;

public class DefaultPropEditorExample {
public static void main(String[] args) {
PropertyEditor editor = PropertyEditorManager.findEditor(Font.class);
//font to text conversion
editor.setValue(new Font(Font.DIALOG, Font.BOLD, 12));
String asText = editor.getAsText();
System.out.println(asText);

//text to font conversion
PropertyEditor editor2 = PropertyEditorManager.findEditor(Font.class);
editor2.setAsText("SansSerif ITALIC 14");
Object value = editor2.getValue();
System.out.println(value);
}
}

Output:

true




Registering a PropertyEditor by calling PropertyEditorManager.registerEditor(..). This call will tie the provided type to the custom property editor globally.

public class OverridingDefaultEditorExample {

public static void main (String[] args) {
//overriding default jdk boolean editor
PropertyEditorManager.registerEditor(Boolean.class, CustomBooleanEditor.class);
PropertyEditor editor = PropertyEditorManager.findEditor(Boolean.class);
editor.setAsText("yes");
System.out.println(editor.getValue());
}

public static class CustomBooleanEditor extends BooleanEditor {
@Override
public void setAsText (String text) throws IllegalArgumentException {
if ("yes".equalsIgnoreCase(text)) {
super.setAsText("true");
} else if ("no".equalsIgnoreCase(text)) {
super.setAsText("false");
} else {
super.setAsText(text);
}
}
}
}

Output:

true




A custom property editor can be auto registered. In this example a new custom UserEditor.java is at the same package location as bean User.java, that's way it's auto-discovered but it can be overridden if we use PropertyEditorManager.registerEditor method to register a new one.

package com.logicbig.example;

import com.sun.beans.editors.StringEditor;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.beans.PropertyEditorSupport;


public class PropEditor2 {

public static void main (String[] args) {
//auto discovering UserEditor
PropertyEditor editor = PropertyEditorManager.findEditor(User.class);
System.out.println(editor.getClass());

//uncomment the following to override the default editor
/* PropertyEditorManager.registerEditor(User.class, UserEditor2.class);
editor = PropertyEditorManager.findEditor(User.class);
System.out.println(editor.getClass());*/

editor.setAsText("Joe");
System.out.println(editor.getValue());
}

public static class UserEditor extends PropertyEditorSupport {

@Override
public String getAsText () {
User user = (User) getValue();
return user.getName();
}

@Override
public void setAsText (String s) {
User user = new User();
user.setName(s);
setValue(user);
}
}

public static class UserEditor2 extends StringEditor {

@Override
public String getAsText () {
User user = (User) getValue();
return user.getName();
}

@Override
public void setAsText (String s) {
User user = new User();
user.setName(s);
setValue(user);
}
}

public static class User {
private String name;


public String getName () {
return name;
}

public void setName (String name) {
this.name = name;
}

@Override
public String toString () {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
}

Output:

 class com.logicbig.example.PropEditor2$UserEditor
User{name='Joe'}




See Also