java.lang.IllegalAccessException:模块javafx.base无法访问类sample.model.Artists,因为模块未将sample.model打开到javafx.base

Nit*_*tin 5 java javafx thread-safety

即使我使用了 SimpleStringProperty 和 SimpleIntegerProperty,每次单击“列出艺术家”按钮时都会出现以下错误。

May 03, 2021 9:33:22 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'name' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@7bcaa928 with provided class type: class sample.model.Artists
java.lang.RuntimeException: java.lang.IllegalAccessException: module javafx.base cannot access class sample.model.Artists (in module MusicUI) because module MusicUI does not open sample.model to javafx.base
    at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:176)
    at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(PropertyValueFactory.java:184)
    at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:154)
    at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:133)
    at ....
Run Code Online (Sandbox Code Playgroud)

控制器类具有“列出艺术家”按钮的事件处理程序:当我们调用 new Thread(task).start() 时,会出现此错误

public class Controller {
    @FXML
    public TableView<Artists> artistTable;

    @FXML
    public void listArtists(){
        Task<ObservableList<Artists>> task = new GetAllArtistsTask();
        artistTable.itemsProperty().bind(task.valueProperty());
        new Thread(task).start();
    }

}
class GetAllArtistsTask extends Task {
    // following method works correct as I printed the observable list.
    @Override
    protected ObservableList<Artists> call() throws Exception {
        return FXCollections.observableArrayList(DataSource.getInstance().
                getArtists(DataSource.ORDER_BY_ASC));
    }
}
Run Code Online (Sandbox Code Playgroud)

XML 文件:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
            fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml/1">
    <center>
        <TableView fx:id="artistTable" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
            <columns>

                <TableColumn prefWidth="${artistTable.width}" text="Name" >
                    <cellValueFactory>
                        <!--                        This maps to name column in Table artists-->
                        <PropertyValueFactory property="name"/>
                    </cellValueFactory>
                </TableColumn>
            </columns>
            <BorderPane.margin>
                <Insets right="10.0" />
            </BorderPane.margin>
        </TableView>
    </center>
    <right>
        <VBox alignment="CENTER" prefHeight="200.0" prefWidth="170.00" spacing="20.0" BorderPane.alignment="CENTER">
            <BorderPane.margin>
                <Insets right="10.0"/>
            </BorderPane.margin>
            <Button onAction="#listArtists" maxWidth="Infinity" mnemonicParsing="false" text="List Artists"/>
            <Button maxWidth="Infinity" mnemonicParsing="false" text="Show Albums (artist)"/>
            <Button maxWidth="Infinity" mnemonicParsing="false" text="Update Artists"/>
        </VBox>
    </right>
    <bottom>
        <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
            <ProgressBar prefWidth="200.0" progress="0.0">
                <HBox.margin>
                    <Insets left="50.0"/>
                </HBox.margin>
            </ProgressBar>
        </HBox>
    </bottom>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)

艺术家类:我使用 SimpleIntegerProperty 和 SimpleStringProperty 而不是 int 和 String,如以下指定:JavaFX 11:创建标签时的 IllegalAccessError

    public class Artists {
    private final SimpleIntegerProperty _id;
    private final SimpleStringProperty name;

    public int get_id() { return _id.get(); }
    public String getName() { return name.get();}

    public Artists(int _id, String name) {
        this._id = new SimpleIntegerProperty();
        this._id.set(_id);
        this.name = new SimpleStringProperty();
        this.name.set(name);
    }
    @Override
    public String toString(){
        return "ID: "+ get_id() +", Name: "+ getName();
    }
}
Run Code Online (Sandbox Code Playgroud)

ank*_*nko 14

需要module-info.java修改一下。当您将 定义groupId为“样本”(您可以在 中找到它pom.xml)时,这应该可以工作:

模块信息.java

module sample {
    requires javafx.controls;
    requires javafx.fxml;

    opens sample to javafx.fxml;
    opens sample.model to javafx.fxml;

    exports sample;
    exports sample.model;
}
Run Code Online (Sandbox Code Playgroud)