Search code examples
javajavafxcontrolsfxpropertysheet

Set category name for bean fields in PropertySheet JavaFX


ContolsFX example for PropertySheet have several options. First, create a bean with beaninfo. In this case I can't set category for each field in PropertySheet. I can choose only between basic and expert categories. Second option is to use Map, where I can set category for each field. But I want to use beans. Is it possible to set category for bean fields?

public class SampleBeanBeanInfo extends SimpleBeanInfo {

    private static final BeanDescriptor beanDescriptor = new BeanDescriptor(SampleBeanBeanInfo.class);
    private static PropertyDescriptor[] propDescriptors;

    static {
        beanDescriptor.setDisplayName("Sample Bean");
    }

    @Override
    public BeanDescriptor getBeanDescriptor() {
        return beanDescriptor;
    }

    @Override
    public int getDefaultPropertyIndex() {
        return 0;
    }

    @Override
    public PropertyDescriptor[] getPropertyDescriptors() {
        if (propDescriptors == null) {
            propDescriptors = new PropertyDescriptor[5];
            try {
                CustomPropertyDescriptor cdp = new CustomPropertyDescriptor("id", SampleBean.class, "getId", "setId");
                cdp.setDisplayName("Id");
                cdp.setEditable(false);
                propDescriptors[0] = cdp;
                propDescriptors[1] = new PropertyDescriptor("firstName", SampleBean.class, "getFirstName", "setFirstName");
                propDescriptors[1].setDisplayName("First Name");
                propDescriptors[2] = new PropertyDescriptor("lastName", SampleBean.class, "getLastName", "setLastName");
                propDescriptors[2].setDisplayName("Last Name");
                propDescriptors[3] = new PropertyDescriptor("address", SampleBean.class, "getAddress", "setAddress");
                propDescriptors[3].setDisplayName("Address");
                propDescriptors[3].setPropertyEditorClass(PopupPropertyEditor.class);
                propDescriptors[4] = new PropertyDescriptor("hiddenValue", SampleBean.class, "getHiddenValue", "setHiddenValue");
                propDescriptors[4].setDisplayName("Hidden Value");
                propDescriptors[4].setHidden(true);
            } catch (IntrospectionException ex) {
                ex.printStackTrace();
            }
        }
        return propDescriptors;
    }

}

Solution

  • After investigation, i found that categories are hardcoded and could not be changed when we are using beans. So you should use your custom PropertySheet.Item implementation. In this case we can set any category name for properties. Check out official example in controlsFX project how to do it.