Java - Custom PropertyEditorSupport to display units

Posted by I82Much on Stack Overflow See other posts from Stack Overflow or by I82Much
Published on 2010-05-19T02:17:06Z Indexed on 2010/05/19 2:20 UTC
Read the original article Hit count: 366

Filed under:
|
|
|
|

All,

I'm trying to make the properties of my node have Units associated with the measure. ( I am using the JScience.org implementation of JSR 275) So for instance,

public class Robot extends AbstractNode {
    // in kg
    float vehicleMass;

    @Override
    public Sheet createSheet() {
        Sheet s = Sheet.createDefault();
        Sheet.Set set = s.createPropertiesSet();
        try {
            PropertySupport.Reflection vehicleMassField = new PropertySupport.Reflection(this, float.class, "vehicleMass");
            vehicleMassField.setValue("units", SI.KILOGRAMS);
            vehicleMassField.setName("vehicleMass");
            set.put(vehicleMassField);

            PropertyEditorManager.registerEditor(float.class, UnitInPlaceEditor.class);
        } catch (NoSuchMethodException ex) {
            Exceptions.printStackTrace(ex);
        }
        s.put(set);
        return s;
    }
}

I want my UnitInPlaceEditor to append the units to the end of the string representation of the number, and when the field is clicked (enters edit mode) for the units to disappear and just the number becomes selected for editing. I can make the units appear, but I cannot get the units to disappear when the field enters editing mode.

public class UnitsInplaceEditor extends PropertyEditorSupport implements ExPropertyEditor {

    private PropertyEnv pe;

    @Override
    public String getAsText() {
        // Append the unit by retrieving the stored value
    }

    @Override
    public void setAsText(String s) {
        // strip off the unit, parse out the number
    }

    public void attachEnv(PropertyEnv pe) {
        this.pe = pe;
    }
}

Here's a screenshot of the display - I like it like this.. alt text

but here's the value being edited; note the unit stays there. alt text

Basically I want one value (string) to be displayed in the field when the field is NOT being edited, and a different to be displayed when user starts editing the field. Barring that, I'd like to put a constant jlabel for the units (uneditable) to the right of the text field.

Anyone know how to do this?

© Stack Overflow or respective owner

Related posts about netbeans

Related posts about javabeans