我在view.fxml中创建了一个tableview,它引用了我的控制器类中的table
(TableView变量)。 showFilesTableView()
方法会调用initialize()
。
public void showFilesTableView() {
TableColumn Name = new TableColumn<AllInfo,String>("Name");
Name.setCellFactory(new PropertyValueFactory<AllInfo, String>("name"));
table.getColumns().add(Name);
TableColumn Size = new TableColumn<AllInfo,String>("Size");
Size.setCellFactory(new PropertyValueFactory<AllInfo, String>("size"));
table.getColumns().add(Size);
TableColumn dmd = new TableColumn<AllInfo,String>("Date Modified");
dmd.setCellFactory(new PropertyValueFactory<AllInfo, String>("date"));
table.getColumns().add(dmd);
ObservableList<AllInfo> data= FXCollections.observableArrayList();
File[] files = (new File(textfield.getText())).listFiles();
for(File f: files) {
AllInfo obj = new AllInfo();
obj.name.set(f.getName());
obj.size.set(String.valueOf(f.length()));
obj.date.set(String.valueOf(f.lastModified()));
data.add(obj);
}
table.setItems(data);
}
AllInfo类是:
public class AllInfo {
public SimpleStringProperty name = new SimpleStringProperty();
public SimpleStringProperty size = new SimpleStringProperty();
public SimpleStringProperty date = new SimpleStringProperty();
}
但是这个错误一直在显示......
java.lang.ClassCastException: javafx.scene.control.TableColumn cannot be cast to javafx.scene.control.TableColumn$CellDataFeatures
完整错误:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: javafx.scene.control.TableColumn cannot be cast to javafx.scene.control.TableColumn$CellDataFeatures
at javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:98)
at com.sun.javafx.scene.control.skin.TableRowSkin.getCell(TableRowSkin.java:87)
at com.sun.javafx.scene.control.skin.TableRowSkin.getCell(TableRowSkin.java:53)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.createCell(TableRowSkinBase.java:698)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.recreateCells(TableRowSkinBase.java:692)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:146)
at com.sun.javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:64)
at javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212)
at javafx.scene.control.Control.impl_processCSS(Control.java:872)
at javafx.scene.Node.processCSS(Node.java:9056)
at javafx.scene.Node.applyCss(Node.java:9153)
at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1964)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1797)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1879)
at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2528)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene.preferredSize(Scene.java:1646)
at javafx.scene.Scene.impl_preferredSize(Scene.java:1720)
at javafx.stage.Window$9.invalidated(Window.java:846)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
at javafx.stage.Window.setShowing(Window.java:922)
at javafx.stage.Window.show(Window.java:937)
at javafx.stage.Stage.show(Stage.java:259)
at sample.Main.start(Main.java:18)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
我尝试了this,但仍然发生了错误。
如何解决此问题?
答案 0 :(得分:2)
您使用PropertyValueFactory
作为cellFactory
而不是cellValueFactory
s。
由于您正在使用原始类型,因此编译器无法检查Callback
的类型参数是否正确,这会导致编译时错误。
只需在代码中将setCellFactory
替换为setCellValueFactory
即可解决此问题:
Size.setCellValueFactory(new PropertyValueFactory<AllInfo, String>("size"));
...
并最好将类型参数添加到TableColumn
声明:
TableColumn<AllInfo, String> Size = ...
...
此外,项目类中包含的字段不足以显示数据。在这种情况下,PropertyValueFactory
会查找返回属性nameProperty()
,sizeProperty
和dateProperty
的方法。