JavaFX Table Columns, SceneBuilder Not Populating

前端 未结 1 1534
挽巷
挽巷 2021-01-24 01:13

I\'ve been looking at tutorials, and I can\'t seem to get a table to populate. I\'m using net beans and scenebuilder too. Any help would be greatly appreciated! been struggling

1条回答
  •  被撕碎了的回忆
    2021-01-24 01:44

    For PropertyValueFactory to find the property the item class (i.e. Table in this case) needs public as access modifier, not package private. The method returning the property needs to be public as well.

    Furthermore the correct name for the method returning the property itself is Property according to the conventions required for PropertyValueFactory to work.

    Also since the actual type of the property is an implementation detail, it would be better design to use StringProperty as return type instead of SimpleStringProperty

    public class Table {
    
        private final SimpleStringProperty rCountry;
    
        public Table(String country){
            this.rCountry = new SimpleStringProperty(country);
        }
    
        public StringProperty rCountryProperty() {
            return this.rCountry;
        }
    }
    

    In case you used these modifiers to prevent write access to the property, you can still achieve this effect by using a ReadOnlyStringWrapper and return a ReadOnlyStringProperty:

    public class Table {
    
        private final ReadOnlyStringWrapper rCountry;
    
        public Table(String country){
            this.rCountry = new ReadOnlyStringWrapper (country);
        }
    
        public ReadOnlyStringProperty rCountryProperty() {
            return this.rCountry.getReadOnlyProperty();
        }
    }
    

    In case there is no write access to the property at all, simply using a getter for the property is enough. You do not need to use a StringProperty at all in this case:

    public class Table {
    
        private final String rCountry;
    
        public Table(String country){
            this.rCountry = country;
        }
    
        public String getRCountry() {
            return this.rCountry;
        }
    }
    

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