使用PropertyEditor(ControlsFX)的属性表示例 [英] Property sheet example with use of a PropertyEditor (ControlsFX)

查看:243
本文介绍了使用PropertyEditor(ControlsFX)的属性表示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been searching a lot for any good examples of the use of a ControlsFX PropertySheet but couldn’t find anything but this.

https://www.google.nl/search?q=Main.java+amazonaws.com&ie=utf-8&oe=utf-8&aq=t& ; RLS = org.mozilla:NL:官方&安培;客户=火狐-A和信道= SB&安培; gfe_rd = CR安培; EI = d5aeU5bvBI3k-gan94HQBA#信道= SB&安培; q = HTTPS%3A%2F%2Fbitbucket-assetroot.s3.amazonaws .com%2Fcontrolsfx%2Fcontrolsfx + Main.java& rls = org.mozilla:nl:official

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

In this example, the ObservableList items which includes NameItem objects, is added to the PropertySheet object in its constructor just like the documentation tells.

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

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

However, as this documentation says, a column of a PropertySheet "provides a PropertyEditor that allows the end user the means to manipulate the property". It even says that there is a "CheckEditor, ChoiceEditor, TextEditor and FontEditor, among the many editors that are available in the Editors package.".

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

I don’t want to be limited to my NameItem example. I also want to add check boxes, choice boxes and other dynamic editor elements. Can anyone please give an example on how to use one or more of the editors to build a simple PropertySheet?

推荐答案

PropertySheet 支持很少的属性编辑器,具体取决于属性类型。

PropertySheet does support few property editors out of the box, depending on a property type.

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

Following example is an extension from ControlsFX sample application. It shows how String, LocalDate, Enum, Boolean and Integer types are mapped to the TextField, DatePicker, ChoiceBox, CheckBox and NumericField respectively.

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< String>创建ChoiceBox。类型。对于其他类型,它将编辑器创建委托给默认工厂。

This behavior can be further extended in two ways. First, an existing editor can be used for types which are not supported by default property editor factory. Following example sets new property editor factory which will create ChoiceBox for List<String> type. For other types it delegates editor creation to the default factory.

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);
    }
});

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

And finally, we can create custom editor and override getPropertyEditorClass() method from the PropertySheet.Item to return the custom editor type. In that case, default property editor factory will create the editor, and there is no need to override the factory method.

这篇关于使用PropertyEditor(ControlsFX)的属性表示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆