Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property sheet example with use of a PropertyEditor (ControlsFX)

Tags:

javafx

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: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

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

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.”.

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?

like image 757
Lodewijck Avatar asked Jun 16 '14 07:06

Lodewijck


1 Answers

Recently I've been working on Javafx with PropertySheet, below is the simple and quick practice: first we need model class to reflect the gui:

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

Then is the Main class:

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

Here is the result: result gui

Advantages:

  • easy to develop via BeanPropertyUtils, reflect to java bean
  • we can remove some of the setter methods in model class if we don't want user to change any content on GUI
  • if we are not satisfy with the default PropertyEditor, we can overwrite by our own's.

This basically satisfy most scenarios.By the way, it can also support Enum field in model class with ChoiceEditor as Default.😝

like image 59
yimkong Avatar answered Sep 23 '22 09:09

yimkong