JavaFX表列,SceneBuilder未填充 [英] JavaFX Table Columns, SceneBuilder Not Populating

查看:117
本文介绍了JavaFX表列,SceneBuilder未填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看教程,我似乎无法填写表格。
我也在使用net beans和scenebuilder。
任何帮助将不胜感激!一直在挣扎5个小时。

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 for 5 hours.

这是我的控制器类的代码:

public class FXMLDocumentController implements Initializable {

    @FXML
    private TableView<Table> table;
    @FXML
    private TableColumn<Table, String> countriesTab;

    /**
     * Initializes the controller class.
     */

    ObservableList<Table> data = FXCollections.observableArrayList(
            new Table("Canada"),
            new Table("U.S.A"),
            new Table("Mexico")
    );

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        countriesTab.setCellValueFactory(new PropertyValueFactory<Table, String>("rCountry"));
        table.setItems(data);
    }
}

这是

class Table {
    public final SimpleStringProperty rCountry;


    Table(String country){
        this.rCountry = new SimpleStringProperty(country);
    }

    private SimpleStringProperty getRCountry(){
        return this.rCountry;

    }
}

这是我的主要:

public class Assignment1 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}


推荐答案

对于 PropertyValueFactory 要查找属性,项类(即本例中)需要 public 作为访问修饰符,而不是包私有。返回属性的方法也需要 public

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.

此外,根据所需的约定,返回属性本身的方法的正确名称是< nameOfProperty> Property PropertyValueFactory 才能工作。

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

此外,由于属性的实际类型是实现细节,因此使用 StringProperty 作为更好的设计返回类型而不是 SimpleStringProperty

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;
    }
}

如果您使用这些修饰符来阻止对该属性,您仍然可以通过使用 ReadOnlyStringWrapper 并返回 ReadOnlyStringProperty

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();
    }
}

如果没有对物业的写入权限所有,只需使用吸气剂就足够了。在这种情况下,您根本不需要使用 StringProperty

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;
    }
}

这篇关于JavaFX表列,SceneBuilder未填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆