Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use StringProperty over String?

I am developing an application in JavaFX where I want to represent information of a Person in a single Person class. I came across a tutorial where a Person's name was being represented as a StringProperty instead of String. I have searched for the differences of these and found out this and this but the explanation there is not sufficient for me to grab the concept. Some post on the net said there are advantages of using StringProperty over String but could not mention them.

Now my question is: What conditions require one to use StringProperty over String and what are the advantages of doing that?

Why this:

StringProperty firstName;

Over this:

String firstName;

like image 726
CN1002 Avatar asked Jul 07 '15 10:07

CN1002


People also ask

What is StringProperty?

public abstract class StringProperty extends ReadOnlyStringProperty implements Property<String>, WritableStringValue. This class provides a full implementation of a Property wrapping a String value. The value of a StringProperty can be get and set with ObservableObjectValue.

What is double property Javafx?

public static DoubleProperty doubleProperty(Property<Double> property) Returns a DoubleProperty that wraps a Property and is bidirectionally bound to it. Changing this property will result in a change of the original property. This is very useful when bidirectionally binding an ObjectProperty and a DoubleProperty.


2 Answers

When to use StringProperty firstName over String firstName?

Use it when this firstName variable is going to be observed by others. You also can observe it by attaching a listener. You can use this variable in bindings with other observable objects of JavaFX. In some circumstances it is mandatory to use JavaFX Property, like Person list rendered with tableView which is editable. To reflect the changes immediately in edited cell, the underlying bound field should be a property.

like image 68
Uluk Biy Avatar answered Oct 23 '22 22:10

Uluk Biy


JavaFX tries to enable the MVC-Pattern. The Model should be created with Properties to take advantage of Binding. In your case the Model is the Person class, so you can simply add the StringProperty firstName. But in JavaFX you have to take care of the other naming convention as for simple getter and setter in a Pojo Bean.

Naming Convention for Properties in JavaFX are:

 public class Person {
     private StringProperty firstName;
     public void setFirstName(String value) { firstNameProperty().set(value); }
     public String getFirstName() { return firstNameProperty().get(); }
     public StringProperty firstNameProperty() { 
         if (firstName == null) firstName = new SimpleStringProperty(this, "firstName");
         return firstName; 
     }

     private StringProperty lastName;
     public void setLastName(String value) { lastNameProperty().set(value); }
     public String getLastName() { return lastNameProperty().get(); }
     public StringProperty lastNameProperty() { 
         if (lastName == null) lastName = new SimpleStringProperty(this, "lastName");
         return lastName; 
     } 
 }

After that you be able to bind for example a TableColumn of a TableView to the Property "lastName"

TableView<Person> table = new TableView<Person>();

ObservableList<Person> teamMembers = getTeamMembers();
table.setItems(teamMembers);

TableColumn<Person,String> lastNameCol = new TableColumn<Person,String>("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));

Without a Property this would be much more code and you won't have the advantages of implemented ChangeListener/InvalidationListener support.

The example above comes from JavaFX TableView

So the recommended way in making a Model for JavaFX is using JavaFX-Properties and not the build in types.

like image 10
aw-think Avatar answered Oct 23 '22 22:10

aw-think