2

I'm trying to follow MVC concepts when i develop my application ,but I'm confused between Using String or StringProperty in Model classes.

Example one :

public class User{
 String name;
 String password;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public String getPassword() {
 return password;
 }
 public void setPassword(String password) {
 this.password = password;
 }
 public User(String name, String password) {
 this.name = name;
 this.password = password;
 }

Example Two:

public class User{
 StringProperty name;
 StringProperty password ;
 public StringProperty getName() {
 return name;
 }
 public void setName(StringProperty name) {
 this.name = name;
 }
 public StringProperty getPassword() {
 return password;
 }
 public void setPassword(StringProperty password) {
 this.password = password;
 }
 public User(StringProperty name, StringProperty password) {
 this.name = name;
 this.password = password;
 }

I'm using example one ,Is there difference between these models or are they the same ?

asked Mar 11, 2018 at 11:16

2 Answers 2

2

A JavaFX property is an observable container that facilitates data binding: The view can be connected with properties in the view model, so that it automatically updates when the model is changed.

So – use property fields if you want data binding, use ordinary strings when you don't need data binding.

In any case, the property should be bound to a specific model object. You should see a property as a special kind of variable, not as an ordinary object. So passing a property in a setter or in a constructor is highly unusual. Instead, your getters, setters and constructors would usually operate on the data stored within a property. This way, your property-based classes can also be used as Java Beans.

E.g. code using a property might look like this:

public class User{
 private StringProperty nameProperty = new StringProperty();
 public final String getName() {
 return nameProperty.get();
 }
 public final void setName(String name) {
 nameProperty.set(name);
 }
 // expose the property for data binding
 // – possibly use a read-only interface
 public StringProperty nameProperty() {
 return nameProperty;
 }
 public User(String name) {
 nameProperty.set(name);
 }
}
answered Mar 11, 2018 at 11:26
2
  • So for example when I make a change by using setName(String name) the value in view will change automatically or should i use some bindings methods ? Commented Mar 11, 2018 at 11:36
  • 1
    @XlintXms If the view is set up to be bound to the StringProperty, then setting the name on the model object will update the view automatically. All of the JavaFX widgets support this data binding. Commented Mar 11, 2018 at 11:44
0

One difference that should be noted is JavaFX properties are not Serializable. See @jewelsea answer here.

answered Jun 12, 2019 at 18:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.