簡體   English   中英

JavaFX TableView 與多個對象

[英]JavaFX TableView with multiple objects

我正在嘗試用多個數據對象填充一個 TableView,一個帶有標題屬性的 ContentItem object 和一個帶有百分比屬性的 Progress object。

這行不通,我知道我給了我的 TableView 數據類型 ContentItem 所以它只接受該類型的對象,但是有一些解決方法嗎? 所以我可以在這個表中顯示多個對象?

我用的是普通的 JavaFX。

希望有人可以幫助我!

下面的代碼

// Creating tableview for modules and webcasts
TableView<ContentItem> tableViewModulesAndWebcasts = new TableView<>();

// Creating columns for modules and webcasts
TableColumn<ContentItem, String> titleContentItem = new TableColumn<>("Titel");
titleContentItem.setCellValueFactory(new PropertyValueFactory<>("title"));

TableColumn<Progress, Integer> progressColumn = new TableColumn<>("Percentage");
titleContentItem.setCellValueFactory(new PropertyValueFactory<>("percentage"));

// Adding columns to tableview
tableViewModulesAndWebcasts.getColumns().add(titleContentItem);
tableViewModulesAndWebcasts.getColumns().add(progressColumn);

我有 2 個類(類型)和一個 controller:

第一個 Class (Type1)

package application;

public class Type1 {

    private String student_ID;
    
    public Type1(String student_ID) {
        
        this.student_ID = student_ID;   
    }
    public String getStudent_ID() {
        return student_ID;
    }
    public void setStudent_ID(String student_ID) {
        student_ID = student_ID;
    }
    
}

二等(Type2)

package application;

public class Type2 {

    public Type2(String time) {
        this.time = time;
    }
    private String time;

    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
}

第三類(控制器)“不包括導入語句”

public class controller implements Initializable {

    ObservableList<Object> data = FXCollections.observableArrayList();
    
    @FXML
    TableView mytable;
    @FXML
    TableColumn type1col;
    @FXML
    TableColumn type2col;
    
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
        data.add(new Type1("1"));
        data.add(new Type1("1"));
        data.add(new Type1("1"));
        data.add(new Type1("1"));
        data.add(new Type1("1"));
        data.add(new Type2("2"));
        data.add(new Type2("2"));
        data.add(new Type2("2"));
        data.add(new Type2("2"));
        data.add(new Type2("2"));
        
        type1col.setCellValueFactory(
                new PropertyValueFactory<Type1, String>("student_ID"));
        type2col.setCellValueFactory(
                new PropertyValueFactory<Type2, String>("time"));
        
        mytable.setItems(data);
    }   
}

結果:

在此處輸入圖像描述

拋出的錯誤:

Jan 12, 2022 2:21:59 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'time' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@73ac36a9 with provided class type: class application.Type1
java.lang.IllegalStateException: Cannot read from unreadable property time
    at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:167)
    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 javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:593)
    at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:578)
    at javafx.controls/javafx.scene.control.TableCell.updateItem(TableCell.java:670)
    at javafx.controls/javafx.scene.control.TableCell.indexChanged(TableCell.java:474)
    at javafx.controls/javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:120)
    at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:539)
    at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.<init>(TableRowSkinBase.java:159)
    at javafx.controls/javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:89)
    at javafx.controls/javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:213)
    at javafx.controls/javafx.scene.control.Control.doProcessCSS(Control.java:897)
    at javafx.controls/javafx.scene.control.Control$1.doProcessCSS(Control.java:89)
    at javafx.controls/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:146)
    at javafx.graphics/javafx.scene.Parent.doProcessCSS(Parent.java:1400)
    at javafx.graphics/javafx.scene.Parent$1.doProcessCSS(Parent.java:125)
    at javafx.graphics/com.sun.javafx.scene.ParentHelper.processCSSImpl(ParentHelper.java:98)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:146)
    at javafx.graphics/javafx.scene.Parent.doProcessCSS(Parent.java:1400)
    at javafx.graphics/javafx.scene.Parent$1.doProcessCSS(Parent.java:125)
    at javafx.graphics/com.sun.javafx.scene.ParentHelper.processCSSImpl(ParentHelper.java:98)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:146)
    at javafx.graphics/javafx.scene.Node.processCSS(Node.java:9456)
    at javafx.graphics/javafx.scene.Node.applyCss(Node.java:9543)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1814)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1791)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.getOrCreateCellSize(VirtualFlow.java:2966)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.getOrCreateCellSize(VirtualFlow.java:2949)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.recalculateAndImproveEstimatedSize(VirtualFlow.java:3021)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.recalculateEstimatedSize(VirtualFlow.java:3013)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1052)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow$5.invalidated(VirtualFlow.java:885)
    at javafx.base/javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
    at javafx.base/javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:148)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellCount(VirtualFlow.java:899)
    at javafx.controls/javafx.scene.control.skin.TableViewSkinBase.updateItemCount(TableViewSkinBase.java:555)
    at javafx.controls/javafx.scene.control.skin.TableViewSkin.<init>(TableViewSkin.java:136)
    at javafx.controls/javafx.scene.control.TableView.createDefaultSkin(TableView.java:1534)
    at javafx.controls/javafx.scene.control.Control.doProcessCSS(Control.java:897)
    at javafx.controls/javafx.scene.control.Control$1.doProcessCSS(Control.java:89)
    at javafx.controls/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:146)
    at javafx.graphics/javafx.scene.Parent.doProcessCSS(Parent.java:1400)
    at javafx.graphics/javafx.scene.Parent$1.doProcessCSS(Parent.java:125)
    at javafx.graphics/com.sun.javafx.scene.ParentHelper.processCSSImpl(ParentHelper.java:98)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:146)
    at javafx.graphics/javafx.scene.Node.processCSS(Node.java:9456)
    at javafx.graphics/javafx.scene.Scene.doCSSPass(Scene.java:572)
    at javafx.graphics/javafx.scene.Scene.preferredSize(Scene.java:1774)
    at javafx.graphics/javafx.scene.Scene$2.preferredSize(Scene.java:396)
    at javafx.graphics/com.sun.javafx.scene.SceneHelper.preferredSize(SceneHelper.java:66)
    at javafx.graphics/javafx.stage.Window$12.invalidated(Window.java:1114)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
    at javafx.graphics/javafx.stage.Window.setShowing(Window.java:1190)
    at javafx.graphics/javafx.stage.Window.show(Window.java:1205)
    at javafx.graphics/javafx.stage.Stage.show(Stage.java:273)
    at B_Merge_two_types_in_one_table/application.Main.start(Main.java:19)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)

Jan 12, 2022 2:21:59 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'student_ID' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@1c343204 with provided class type: class application.Type2
java.lang.IllegalStateException: Cannot read from unreadable property student_ID
    at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:167)
    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 javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:593)
    at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:578)
    at javafx.controls/javafx.scene.control.TableCell.updateItem(TableCell.java:670)
    at javafx.controls/javafx.scene.control.TableCell.indexChanged(TableCell.java:474)
    at javafx.controls/javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:120)
    at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:539)
    at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.<init>(TableRowSkinBase.java:159)
    at javafx.controls/javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:89)
    at javafx.controls/javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:213)
    at javafx.controls/javafx.scene.control.Control.doProcessCSS(Control.java:897)
    at javafx.controls/javafx.scene.control.Control$1.doProcessCSS(Control.java:89)
    at javafx.controls/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:146)
    at javafx.graphics/javafx.scene.Node.processCSS(Node.java:9456)
    at javafx.graphics/javafx.scene.Node.applyCss(Node.java:9543)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1814)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.addTrailingCells(VirtualFlow.java:2191)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1327)
    at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1207)
    at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1214)
    at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1214)
    at javafx.graphics/javafx.scene.Scene.doLayoutPass(Scene.java:579)
    at javafx.graphics/javafx.scene.Scene.preferredSize(Scene.java:1777)
    at javafx.graphics/javafx.scene.Scene$2.preferredSize(Scene.java:396)
    at javafx.graphics/com.sun.javafx.scene.SceneHelper.preferredSize(SceneHelper.java:66)
    at javafx.graphics/javafx.stage.Window$12.invalidated(Window.java:1114)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
    at javafx.graphics/javafx.stage.Window.setShowing(Window.java:1190)
    at javafx.graphics/javafx.stage.Window.show(Window.java:1205)
    at javafx.graphics/javafx.stage.Stage.show(Stage.java:273)
    at B_Merge_two_types_in_one_table/application.Main.start(Main.java:19)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)


要克服此錯誤,請將缺少的屬性添加到每個 class,並為其創建一個吸氣劑並讓它返回null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM