来自模型类的正确获取器的PropertyValueFactory错误 [英] PropertyValueFactory error on proper getter from model class

查看:270
本文介绍了来自模型类的正确获取器的PropertyValueFactory错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从fxml文件填充示例javafx TableView.

Im trying to populate sample javafx TableView from fxml file.

这是我的控制器方法:

public class TestController implements Initializable {


       @FXML private TableView<user> tableView;
        @FXML private TableColumn<user, String> UserId;
        @FXML private TableColumn<user, String> UserName;


        public void initialize(URL location, ResourceBundle resources) {
            UserId.setCellValueFactory(new PropertyValueFactory<user, String>("userId"));
            UserName.setCellValueFactory(new PropertyValueFactory<user, String>("userName"));


            tableView.getItems().setAll(parseUserList());
        }
        private List<user> parseUserList(){

            List<user> l_u = new ArrayList<user>();

            user u = new user();
            u.setUserId(1);
            u.setUserName("test1");
            l_u.add(u);

            u.setUserId(2);
            u.setUserName("test2");
            l_u.add(u);

            u.setUserId(3);
            u.setUserName("test3");
            l_u.add(u);


            return l_u;
        }

}

和fxml文件:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="369.0" prefWidth="505.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="viewmodel.TestController ">

   <center>
      <TableView fx:id="tableView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn prefWidth="75.0" text="UserId" fx:id="UserId"/>
          <TableColumn prefWidth="75.0" text="UserName" fx:id="UserName"/>
        </columns>
      </TableView>
   </center>
</BorderPane>

最后是我的模型:

包装型号;

public class user {
    private int userId;

    public int getUserId() { return this.userId; }
    public void setUserId(int userId) { this.userId = userId; }

    private String userName;

    public String getUserName() { return this.userName; }
    public void setUserName(String userName) { this.userName = userName; }
}

现在,当我尝试填充它时,出现此错误:

now when i try to populate it gives me this error :

Jun 28, 2019 12:17:35 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'userId' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@638db851 with provided class type: class model.user
java.lang.RuntimeException: java.lang.IllegalAccessException: module javafx.base cannot access class model.user (in module JavaFXTest) because module JavaFXTest does not open model to javafx.base
    at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:176)

关于SO的一些文章提到,getter和setter属性在获取单词后必须以大写字母开头,但这不能解决问题.

Some articles on SO mentioned that ,the getter and setter property must be Started with Uppercase after get word but that didn't fix the problem.

推荐答案

这是一个很好的示例,说明为什么要使用 Callback 而不是 PropertyValueFactory .暂时我没有任何理由使用PVF代替Callback.

this is a very good example for "why you should use Callback instead of PropertyValueFactory. At the moment I don't see any reason to use PVF instead of Callback.

在这里您可以看到一个缺点,那就是您在运行应用程序之前并没有真正看到编码错误.由于PVF与反射一起使用,因此如果您未正确声明它们,它将找不到合适的字段.如您在例外中所见,PVF期望 Property -es需要一个 PropertyRefference ,并且 user 类中的所有字段都不是 Property es

Here you can see one disadvantage which is you don't really see that you did a coding mistake before you run the app. Since PVF works with reflection, it doesn't find the appropriate fields if you don't declare them correctly. The PVF expects Property-es as you can see in the exception a PropertyRefference is needed and none of the fields delcared in your user class are Propertyes

可以通过这种方式使用它,但是您必须像这样重写 user 类:

It can be used this way but you have to rewrite the user class like:

public class User { // use java naming conventions 
    private IntegerProperty userId;

    public int getUserId() { return this.userId.get(); }
    public void setUserId(int userId) { this.userId.set(userId); }
    public IntegerProperty userIdProperty() { return this.userId; }

    private StringProperty userName;

    public String getUserName() { return this.userName.get(); }
    public void setUserName(String userName) { this.userName.set(userName); }
    public StringProperty userNameProperty() {return this.userName; }

    public User(int userId,String userName){
         this.userId = new SimpleIntegerProperty(userId);
         this.userName = new SimpleStringProperty(userName);
    }
}

现在,由于字段是属性,PropertyValueFactory会找到它们,但我不建议使用它,因为正如您所看到的那样,它可能导致您甚至在运行它之前都没有注意到的问题.

Now since the fields are properties the PropertyValueFactory would find them but I don't recommend using it, because as you can see it can lead to problems you din't even notice before you run it.

所以不是:

UserId.setCellValueFactory(new PropertyValueFactory<user, String>("userId"));
UserName.setCellValueFactory(new PropertyValueFactory<user, String>("userName"));

使用:

// use java naming conventions (`userId` instead of `UserId`)
userId.setCellValueFactory(data -> data.getValue().userIdProperty().asString()); 
// same here user naming convention
userName.setCellValueFactory(data -> data.getValue().userNameProperty());

我已经写过几次评论,但是在这里我将再次提及以使用Java命名约定.像User而不是user一样,UserId而不是UserId等等,等等...

I have wrote as comments a few time, but I will mention it here again to use java naming conventions. Like User instead of user and userId instead of UserId and so on...

这篇关于来自模型类的正确获取器的PropertyValueFactory错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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