Reputation: 3
I have created a tableview in my view.fxml which refers to table
(TableView variable) in my controller class. showFilesTableView()
is call in the initialize()
method.
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);
}
The AllInfo class is:
public class AllInfo {
public SimpleStringProperty name = new SimpleStringProperty();
public SimpleStringProperty size = new SimpleStringProperty();
public SimpleStringProperty date = new SimpleStringProperty();
}
But this error is showing all the time....
java.lang.ClassCastException: javafx.scene.control.TableColumn cannot be cast to javafx.scene.control.TableColumn$CellDataFeatures
Full error:
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
I have tried this but still the errors are occuring.
How to resolve this issue?
Upvotes: 0
Views: 1470
Reputation: 82461
You're using PropertyValueFactory
s as cellFactory
s instead of cellValueFactory
s.
Since you're using raw types, the compiler cannot check, if the type parameters of Callback
are correct, which would have caused a compile time error.
Simply replace setCellFactory
with setCellValueFactory
in your code to fix this issue:
Size.setCellValueFactory(new PropertyValueFactory<AllInfo, String>("size"));
...
and better add the type parameters to the TableColumn
declarations:
TableColumn<AllInfo, String> Size = ...
...
Furthermore fields being contained in the item class is insufficient to make the data show up. PropertyValueFactory
looks for methods returning the property nameProperty()
, sizeProperty
and dateProperty
in this case.
Upvotes: 2