JavaFX displays properties in controls

I am completing the JavaFX tutorial for Oracle myself After years of rocking (a long time ago), I was fascinated by new intelligent features, including Properties I was surprised to find these examples (for example: https://docs.oracle.com/javafx/2/ui_controls/table-view.htm )They are not used in the way I think "right"

This example creates a person class whose properties are fields:

public static class Person {
    private final SimpleStringProperty firstName;
    ...

But getters do not belong to property, but to their values

public String getFirstName() {
        return firstName.get();
    }

So when it binds them to tablecells in a column, it wraps them in a new attribute:

emailCol.setCellValueFactory(
            new PropertyValueFactory<Person,String>("firstName"));

This seems complicated to me and misses the real advantage of event communication, not just using it:

firstNameCol.setCellValueFactory( celldata -> 
        celldata.getValue().firstNameproperty());

My question: is there any reason for this example not to expose and use bean properties directly in the control? Did I miss anything here?

Note: I did change the code in this way, and the example worked better: updates to other controls in the person entity propagated immediately without calling table Refresh(), for example

Solution

First, note that if you follow the expected pattern:

public class Person {

    private final StringProperty firstName = new SimpleStringproperty();

    public StringProperty firstNameproperty() {
        return firstName ;
    }

    public final String getFirstName() {
        return firstNameproperty().get();
    }

    public final void setFirstName(String firstName) {
        firstNameproperty().set(firstName);
    }
}

Then your code version can not call table Refresh() This is the intended use of propertyvaluefactory, which is quite clear from the beginning of documentation

However, you are right. Lambda expression is a better method than propertyvaluefactory In addition to the reason for the reference, there are other major advantages of using lambda expressions on propertyvaluefactory First, and most importantly, propertyvaluefactory just takes the name of the property as a string, which means it has no compile time check So if you misspell the name of the property:

firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstname"));

This will compile well, and you will eventually find blank cells in the column This can be difficult to debug (as evidenced by the number of problems on this website, help is needed to resolve such errors: for example, JavaFX propertyvaluefactory not populating tableview)

Second, propertyvaluefactory works through reflection, which is much slower than lambda expressions This can lead to measurable performance differences, such as when sorting tables with large amounts of data

The reason for introducing propertyvaluefactory is basically historical Before Java 8, of course, there was no lambda expression, so the smallest implementation of a unit factory without this convenience class was through an anonymous inner class:

firstNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person,String>,ObservableValue<String>>() {
    @Override
    public ObservableValue<String> call(TableColumn.CellDataFeatures<Person,String> cellData) {
        return cellData.getValue().firstNameproperty();
    }
});

Because the code is terrible, the JavaFX team introduced propertyvaluefactory just to make the API easier to use

When using java 8 and later, propertyvaluefactory should be regarded as a legacy class, and lambda expressions should be preferred Of course, documents older than Java 8 still exist (in fact, you explicitly link the documents in JavaFX 2 - although most recent version is still not updated), and - frankly - too many other authors have copied the style without thinking about it correctly There may be a good example of completely discarding the propertyvaluefactory class

(so: TL; Dr: No, you haven't missed anything.)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>