Search code examples
javanetbeanspropertiesjavafxencapsulation

Encapsulate JavaFX properties in NetBeans 8?


this is my first question on StackOverflow, so I hope this is not a dumb one :-)

Is there a way to automaticaly encapsulate JavaFX properties in NetBeans 8 ?

Let's say we want to encapsulate this field :

private SimpleIntegerProperty id;

When I goes to Refactor > Encapsulate Fields, I obtain these lines :

public SimpleIntegerProperty getId() {
    return this.id;
}

public setId(SimpleIntegerProperty id) {
    this.id = id;
}

But I'd like to get that :

public Integer getId() {
    return id.get();
}

public void setId(Integer id) {
    this.id.set(id);
}

public SimpleIntegerProperty idProperty() {
    return id;
}

Is there a simple way to do it ? Thanks.


Solution

  • Instead of refactoring, you just can go to Source->Insert Code and select Add JavaFX Property. Then, on the dialog, give a name to your property, i.e. id, give a default value if necessary, select the type of property, i.e. IntegerProperty, and click OK.

    Then this is what you get:

    private final IntegerProperty id = new SimpleIntegerProperty();
    
    public int getId() {
        return id.get();
    }
    
    public void setId(int value) {
        id.set(value);
    }
    
    public IntegerProperty idProperty() {
        return id;
    }
    

    Finally, you can manually change simple type int to Integer, if you need to.