JavaFX TableView and ObservableList - How to auto-update the table?

后端 未结 1 1494
心在旅途
心在旅途 2021-01-19 10:55

I know questions similar to this have been asked, and on different dates, but I\'ll put an SSCCE in here and try to ask this simply.

I would like to be able to updat

1条回答
  •  生来不讨喜
    2021-01-19 11:14

    Your model class Crew has the "wrong" name for the property accessor methods. Without following the recommended method naming scheme, the (somewhat legacy code) PropertyValueFactory will not be able to find the properties, and thus will not be able to observe them for changes:

    package application;
    
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    
    public class Crew {
        private final IntegerProperty crewId = new SimpleIntegerProperty();
        private final StringProperty crewName = new SimpleStringProperty();
    
        Crew(int id, String name) {
            crewId.set(id);
            crewName.set(name);
        }
    
        public IntegerProperty crewIdProperty() { return crewId; }
        public final int getCrewId() { return crewId.get(); }
        public final void setCrewId(int id) { crewId.set(id); }
    
        public StringProperty crewNameProperty() { return crewName; }
        public final String getCrewName() { return crewName.get(); }
        public final void setCrewName(String name) { crewName.set(name); }
    
    }
    

    Alternatively, just implement the callback directly:

    crewIdCol.setCellValueFactory(cellData -> cellData.getValue().crewIdProperty());
    

    in which case the compiler will ensure that you use an existing method name for the property.

    0 讨论(0)
提交回复
热议问题