Reputation: 1
Hello I'm a new learner of javaFx fxml and i'm testing table view with fxml.But netbeans show no error in my code and i can't run my GUI application.
My code of FXMLDocument.fxml is here.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="359.0" prefWidth="473.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="table.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<TableView fx:id="tableview" layoutX="137.0" layoutY="90.0" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="id" prefWidth="93.0" text="Id" />
<TableColumn fx:id="name" prefWidth="106.0" text="Name" />
</columns>
</TableView>
</children>
</AnchorPane>
And this is a fxml controller code.
package table;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
/**
*
* @author pyaephyohlaing
*/
public class FXMLDocumentController implements Initializable {
//the table's view and columns
@FXML
TableView<Person>tableview;
@FXML
TableColumn id;
@FXML
TableColumn name;
//the table data
ObservableList<Person>data = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
//setup the table data
id.setCellValueFactory(
new PropertyValueFactory<Person,Integer>("id")
);
name.setCellFactory(
new PropertyValueFactory<Person,String>("name")
);
data.add(new Person(1,"Mg Mg"));
tableview.setItems(data);
System.out.println("hello");
}
}
Here is Person class code. (Person.java)
package table;
import javafx.beans.property.*;
// a class that represents the person data to add into the table view
public class Person {
//a variable that represent the column data neeed to be JavaFx properties(i.e SimpleStringProperty)
//sets as the JavaFx table data property
public SimpleIntegerProperty id;
public SimpleStringProperty name;
public Person(Integer id,String name) {
System.out.println("Hee");
this.id=new SimpleIntegerProperty(id);
this.name=new SimpleStringProperty(name);
}
//getter method
public Integer getId() {
return id.get();
}
public String getName() {
return name.get();
}
}
This is the main Table.java code.
package table;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author pyaephyohlaing
*/
public class Table extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
And when i run my project,GUI is not appeared and it shows many errors like that.
Executing /Users/pyaephyohlaing/NetBeansProjects/table/dist/run1649041788/table.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/bin/java Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: 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$156(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.(TableRowSkin.java:64) at javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212) at javafx.scene.control.Control.impl_processCSS(Control.java:859) 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:1079) at javafx.scene.Parent.layout(Parent.java:1085) at javafx.scene.Parent.layout(Parent.java:1085) 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 table.Table.start(Table.java:27) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) Exception running application table.Table Java Result: 1 Deleting directory /Users/pyaephyohlaing/NetBeansProjects/table/dist/run1649041788 jfxsa-run: BUILD SUCCESSFUL (total time: 6 seconds)
I have no idea what's wrong with my code and can anyone help me to fix the error? Thanks.
Upvotes: 0
Views: 2902
Reputation: 7008
In the FXMLDocumentController you set the cell factory for Id when I think you meant to set the cellValueFactory for name.
Change:
id.setCellFactory(
new PropertyValueFactory<Person,String>("name")
);
To:
name.setCellValueFactory(
new PropertyValueFactory<Person,String>("name")
);
Upvotes: 1