使用PropertyEditor(ControlsFX)的属性表示例

时间:2014-06-16 07:49:14

标签: javafx

我一直在搜索使用ControlsFX PropertySheet的任何好例子,但除了这个之外找不到任何东西。

https://www.google.nl/search?q=Main.java+amazonaws.com&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:nl:official&client=firefox-a&channel=sb&gfe_rd=cr&ei=d5aeU5bvBI3k-gan94HQBA#channel=sb&q=https%3A%2F%2Fbitbucket-assetroot.s3.amazonaws.com%2Fcontrolsfx%2Fcontrolsfx+Main.java&rls=org.mozilla:nl:official

在此示例中,包含NameItem对象的ObservableList项在其构造函数中添加到PropertySheet对象中,就像文档所述。

http://controlsfx.bitbucket.org/org/controlsfx/control/PropertySheet.html

但是,正如本文档所述,PropertySheet的一列“提供了一个PropertyEditor,允许最终用户操纵该属性”。它甚至说在编辑器包中有许多编辑器中都有“CheckEditor,ChoiceEditor,TextEditor和FontEditor。”。

我不想仅限于我的NameItem示例。我还想添加复选框,选择框和其他动态编辑器元素。任何人都可以举例说明如何使用一个或多个编辑器来构建一个简单的PropertySheet吗?

4 个答案:

答案 0 :(得分:8)

PropertySheet支持开箱即用的少数属性编辑器,具体取决于属性类型。

以下示例是来自ControlsFX示例应用程序的扩展。它显示了String,LocalDate,Enum,Boolean和Integer类型如何分别映射到TextField,DatePicker,ChoiceBox,CheckBox和NumericField。

public class PropertySheetExample extends VBox {
    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);
    }
}

此行为可以通过两种方式进一步扩展。首先,现有编辑器可用于默认属性编辑器工厂不支持的类型。下面的示例设置了新的属性编辑器工厂,它将为List&lt; String&gt;创建ChoiceBox。类型。对于其他类型,它将编辑器创建委托给默认工厂。

SimpleObjectProperty<Callback<PropertySheet.Item, PropertyEditor<?>>> propertyEditorFactory = new SimpleObjectProperty<>(this, "propertyEditor", new DefaultPropertyEditorFactory());

propertySheet.setPropertyEditorFactory(new Callback<PropertySheet.Item, PropertyEditor<?>>() {
    @Override
    public PropertyEditor<?> call(PropertySheet.Item param) {
        if(param.getValue() instanceof List) {
            return Editors.createChoiceEditor(param, (List) param.getValue());
        }

        return propertyEditorFactory.get().call(param);
    }
});

最后,我们可以从PropertySheet.Item创建自定义编辑器并覆盖getPropertyEditorClass()方法,以返回自定义编辑器类型。在这种情况下,默认属性编辑器工厂将创建编辑器,而无需覆盖工厂方法。

答案 1 :(得分:3)

即使在查看样本并试图了解更多信息之后,我也对如何更改属性表中的用户输入类型感到非常困惑。我花了一段时间才解决这个问题,但事实证明答案相对简单。

键是PropertyEditorFactory,它可以使用setPropertyEditorFactory(Callback<PropertySheet.Item,PropertyEditor<?>> factory)直接设置到PropertySheet上。在此回调中,该方法返回一个包含在Editors包中的PropertyEditor类型。当调用提供PropertySheet.Item参数时,您可以检索正在设置的值,在下面的示例中,我检查了返回对象的类型以提供相关的编辑器。

propertySheet.setPropertyEditorFactory(new Callback<PropertySheet.Item, PropertyEditor<?>>() {
    @Override
    public PropertyEditor<?> call(Item param) {
        if(param.getValue() instanceof String[]) {   
            return Editors.createChoiceEditor(param, choices);
         } else if (param.getValue() instanceof Boolean) {
            return Editors.createCheckEditor(param);
         } else if (param.getValue() instanceof Integer) {
            return Editors.createNumericEditor(param);
         } else {
            return Editors.createTextEditor(param);
         }
     }
});

然而,一旦覆盖了这个编辑器工厂,你必须为你需要的每种类型的设置提供一个PropertyEditor,例如,如果你只返回Editors.createNumericEditor(param);但是有String选项,你将得到一个例外。也不要覆盖PropertySheet.Item中的getPropertyEditorClass(),这是我浪费大部分时间的地方。希望这有助于其他人尝试这样做!

答案 2 :(得分:1)

我找不到Editor的自定义PropertySheet的任何示例,但我想我现在已经知道了。在我的情况下,我只想使用Slider作为Number的编辑器。这可能不实用,因为你无法看到Slider的价值,但我想Slider可以简单地被带有滑块和标签的窗格所取代。

首先,我们需要Item的实现,这里是一个稍微修改过的controlsfx-samples版本。 Map已移入此课程,但可能在任何地方。您可能根本不想使用地图,因为将类别和名称组合在一起已经有点不切实际了;加上没有空间来创建项目的描述。

public class CustomPropertyItem implements PropertySheet.Item {
    public static Map<String, Object> customDataMap = new LinkedHashMap<>();
    static {
        customDataMap.put("basic.My Text", "Same text"); // Creates a TextField in property sheet
        customDataMap.put("basic.My Date", LocalDate.of(2016, Month.JANUARY, 1)); // Creates a DatePicker
        customDataMap.put("misc.My Enum", SomeEnum.ALPHA); // Creates a ChoiceBox
        customDataMap.put("misc.My Boolean", false); // Creates a CheckBox
        customDataMap.put("misc.My Number", 500); // Creates a NumericField
        customDataMap.put("misc.My Color", Color.ALICEBLUE); // Creates a ColorPicker
    }

    private String key;
    private String category, name;

    public CustomPropertyItem(String key) 
    {
        this.key = key;
        String[] skey = key.split("\\.", 2);
        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() {
        // doesn't really fit into the map
        return null;
    }

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

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

    @Override
    public Optional<ObservableValue<? extends Object>> getObservableValue() {
        return Optional.empty();
    }

    @Override
    public Optional<Class<? extends PropertyEditor<?>>> getPropertyEditorClass() {
        // for an item of type number, specify the type of editor to use
        if (Number.class.isAssignableFrom(getType())) return Optional.of(NumberSliderEditor.class);

        // ... return other editors for other types

        return Optional.empty();
    }
}

接下来,PropertyEditor实现,它是一个Node和一组将属性项值与控件连接起来的方法。第一个构造函数是实现所必需的第二个构造函数所必需的,因为方法Editors.createCustomEditor(Item)使用反射来查找此构造函数。您不必使用此方法,但默认的PropertyEditorFactory很可能依赖于此。 如果您因某些原因想避免反思,则无需覆盖 getPropertyEditorClass()中的Item,您可以使用setPropertyEditorFactory(Callback)并创建你PropertyEditor的新实例。 请注意,您根本不需要使用setPropertyEditorFactory(Callback)(在此示例中)。

public class NumberSliderEditor extends AbstractPropertyEditor<Number, Slider> {

    public NumberSliderEditor(Item property, Slider control) 
    {
        super(property, control);
    }

    public NumberSliderEditor(Item item)
    {
        this(item, new Slider());
    }

    @Override
    public void setValue(Number n) {
        this.getEditor().setValue(n.doubleValue());
    }

    @Override
    protected ObservableValue<Number> getObservableValue() {
        return this.getEditor().valueProperty();
    }

}

从这里开始,您只需创建PropertySheet并添加所有地图条目。

PropertySheet propertySheet = new PropertySheet();
for (String key : CustomPropertyItem.customDataMap.keySet()) propertySheet.getItems().add(new CustomPropertyItem(key));

我希望这对任何人都有帮助,因为似乎没有任何示例显示使用自定义Editor。对此的唯一提示是Item.getPropertyEditorClass()的javadoc,它具有默认的接口实现,因此您通常不会查看它。这里有一个答案说不要看这个方法的事实也没有帮助:/

答案 3 :(得分:1)

最近我一直在用 PropertySheet 开发 Javafx,下面是简单快速的练习: 首先我们需要模型类来反映 gui:

public class Person {
    int age;
    boolean isLive = true;
    String name = "Yimkong";
    // ... getter and setter method
}

然后是主类:

public class PropertiesSheetTest extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Person bean = new Person();
        ObservableList<PropertySheet.Item> properties = BeanPropertyUtils.getProperties(bean);
        PropertySheet propertySheet = new PropertySheet(properties);
        propertySheet.setSearchBoxVisible(false);
        propertySheet.setModeSwitcherVisible(false);
        DefaultPropertyEditorFactory defaultPropertyEditorFactory = new DefaultPropertyEditorFactory();
        propertySheet.setPropertyEditorFactory(new Callback<PropertySheet.Item, PropertyEditor<?>>() {
            @Override
            public PropertyEditor<?> call(PropertySheet.Item param) {
                if(param.getName().equals("age")){
                    List<Integer> ageList = new ArrayList<>();
                    ageList.add(3);
                    ageList.add(5);
                    ageList.add(10);
                    return Editors.createChoiceEditor(param,ageList);
                }
                return defaultPropertyEditorFactory.call(param);
            }
        });
        VBox vBox = new VBox(propertySheet);
        Scene scene = new Scene(vBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

结果如下: result gui

优点:

  • 通过BeanPropertyUtils易于开发,反映到java bean
  • 如果我们不希望用户更改 GUI 上的任何内容,我们可以删除模型类中的一些 setter 方法
  • 如果我们对默认的 PropertyEditor 不满意,我们可以用我们自己的来覆盖。

这个基本满足大部分场景了。顺便说一句,它也可以支持模型类中的Enum字段,ChoiceEditor为Default。?