JavaFX踩坑日记1_TableView载入数据发生java.lang.IllegalAccessException错误

3 篇文章 0 订阅

最近在做一个业余PC端项目,为了尽可能跨平台,选择了JavaFX构建项目的UI逻辑。其中有一个模块需要用TableView展示数据。我按照教程:

JavaFX 表视图_w3cschool

实现了我的窗体后运行,出现了如下报错:

Caused by: java.lang.IllegalAccessException: module javafx.base cannot access class com.bluepoint.bean.FileItem (in module com.bluepoint.bluepointdisk) because module com.bluepoint.bluepointdisk does not open com.bluepoint.bean to javafx.base
 

并且UI上没有如愿地显示出我的bean对象对应的数据。

回到教程的例子上,有一个地方引起了我的注意:

首先,列数据项中需要设置一个属性名:

        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<>("firstName"));

而JavaBean中也有对应的属性方法:

 public static class Person {
 
        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
 
        private Person(String fName, String lName) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
        }
 
        public String getFirstName() {
            return firstName.get();
        }
 
        public void setFirstName(String fName) {
            firstName.set(fName);
        }
 
        public String getLastName() {
            return lastName.get();
        }
 
        public void setLastName(String fName) {
            lastName.set(fName);
        }
    }

这似乎说明了tableView很可能是通过反射的方法,从bean对象中取出对应关键字的数据,那么根据错误信息,很可能是反射过程中无法找到类文件,可能是需要配置些什么东西。仔细观察InteliJ创建的工程,发现了这个文件: java/module-info.java

内容如下:

module com.bluepoint.bluepointdisk {
    requires javafx.controls;
    requires javafx.fxml;
    requires javafx.web;

    requires org.controlsfx.controls;
    requires com.dlsc.formsfx;
    requires validatorfx;
    requires org.kordamp.ikonli.javafx;
    requires org.kordamp.bootstrapfx.core;
    requires eu.hansolo.tilesfx;

    opens com.bluepoint.bluepointdisk to javafx.fxml;
    exports com.bluepoint.bluepointdisk;
}

按照文件的结构猜测,javafx.fxml之所以可以访问我的工程的文件,应该就是opens中进行了配置。然后我照着格式添加了一行配置:

opens com.bluepoint.bean to javafx.base;

重新编译,执行。数据例子终于跑通了,也没有报错了。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值