2021-06-19_PropertySheet

2021-06-18  本文已影响0人  微笑碧落

0.前言

1.PropertySheet的简单使用

PropertySheet propertySheet = new PropertySheet(BeanPropertyUtils.getProperties(new Mission()));
rootLayoutBorderPane.setCenter(propertySheet);

2.PropertySheet使用代码的详细解释

public static interface Item {
public Class<?> getType();
public String getCategory();
public String getName();
public String getDescription();
public Object getValue();
public void setValue(Object value);
public Optional<ObservableValue<? extends Object>> 
default public Optional<Class<? extends PropertyEditor<?>>> getPropertyEditorClass() {
            return Optional.empty();
        }
default public boolean isEditable() {
            return true;
        }
}
 private static Map<String, Object> customDataMap = new LinkedHashMap<>();
    static {
        customDataMap.put("Group 1#My Text", "Same text"); // Creates a TextField in property sheet
        customDataMap.put("Group 1#My Date", LocalDate.of(2000, Month.JANUARY, 1)); // Creates a DatePicker
        customDataMap.put("Group 2#My Enum Choice", SomeEnumType.EnumValue); // Creates a ChoiceBox
        customDataMap.put("Group 2#My Boolean", false); // Creates a CheckBox
        customDataMap.put("Group 2#My Number", 500); // Creates a NumericField
    }

    class CustomPropertyItem implements PropertySheet.Item {
        private String key;
        private String category, name;

        public CustomPropertyItem(String key) {
            this.key = key;
            String[] skey = key.split("#");
            category = skey[0];
            name = skey[1];
        }

        @Override
        public Class<?> getType() {
            return customDataMap.get(key).getClass();
        }

        @Override
        public String getCategory() {
            return category;
        }

        @Override
        public String getName() {
            return name;
        }

        @Override
        public String getDescription() {
            return null;
        }

        @Override
        public Object getValue() {
            return customDataMap.get(key);
        }

        @Override
        public void setValue(Object value) {
            customDataMap.put(key, value);
        }
    }

    public PropertySheetExample {
        ObservableList<PropertySheet.Item> list = FXCollections.observableArrayList();
        for (String key : customDataMap.keySet())
            list.add(new CustomPropertyItem(key));

        PropertySheet propertySheet = new PropertySheet(list);
        VBox.setVgrow(propertySheet, Priority.ALWAYS);
        getChildren().add(propertySheet);
    }
}

3. PropertySheet自定义编辑器

3.1 编辑器接口

public interface PropertyEditor<T> {
    public Node getEditor();
    public T getValue();
    public void setValue(T value);
}

3.2 AbstractPropertyEditor<T, C extends Node> implements PropertyEditor<T>编辑器

3.3 Editors类和DefaultPropertyEditorFactory类

4. 自定义编辑器

5. 自定义编辑器的例子

public class NumberSliderEditor extends AbstractPropertyEditor<String, TextArea> {
    public NumberSliderEditor(PropertySheet.Item property, TextArea control) {
        super(property, control);
    }

    public NumberSliderEditor(PropertySheet.Item property) {
        super(property, new TextArea());
    }

    @Override
    protected ObservableValue<String> getObservableValue() {
        return getEditor().textProperty();
    }

    @Override
    public void setValue(String value) {
        getEditor().setText(value);

    }
}
public Optional<Class<? extends PropertyEditor<?>>> getPropertyEditorClass() {
      return Optional.of(NumberSliderEditor.class);
}

参考文章

1.javafx – 使用PropertyEditor(ControlsFX)的属性表示例
2.Property sheet example with use of a PropertyEditor (ControlsFX)
3.

上一篇 下一篇

猜你喜欢

热点阅读