エラー内容
TableViewに値を入れようとすると下記のエラーが出てしまいます。
様々なサイトを見て、色々な方法を試してみたのですが全て同様のエラーが出てしまい、解決することが出来ません。
警告: Can not retrieve property 'name' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@67f43230 with provided class type: class application.Person [日 9月 26 00:35:45 JST 2021] 警告: Can not retrieve property 'age' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@e28a0bf with provided class type: class application.Person [日 9月 26 00:35:45 JST 2021] 警告: Can not retrieve property 'sex' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@2bfa6ab2 with provided class type: class application.Person [日 9月 26 00:35:45 JST 2021]
該当のソースコード
main
package application; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) { try { AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("TablePractice.fxml")); Scene scene = new Scene(root,600,400); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setScene(scene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } }
controller
/** * "TablePractice.fxml"コントローラ・クラスのサンプル・スケルトン */ /** * "TablePractice.fxml"コントローラ・クラスのサンプル・スケルトン */ package application; import java.net.URL; import java.util.ResourceBundle; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; public class TablePracticeController { @FXML // ResourceBundle that was given to the FXMLLoader private ResourceBundle resources; @FXML // URL location of the FXML file that was given to the FXMLLoader private URL location; @FXML // fx:id="table" private TableView<Person> table; // Value injected by FXMLLoader @FXML // fx:id="nameColumn" private TableColumn<Person, String> nameColumn; // Value injected by FXMLLoader @FXML // fx:id="ageColumn" private TableColumn<Person, String> ageColumn; // Value injected by FXMLLoader @FXML // fx:id="sexColumn" private TableColumn<Person, String> sexColumn; // Value injected by FXMLLoader private ObservableList<Person> tvObservableList = FXCollections.observableArrayList(); @FXML // This method is called by the FXMLLoader when initialization is complete void initialize() { assert table != null : "fx:id=\"table\" was not injected: check your FXML file 'TablePractice.fxml'."; assert nameColumn != null : "fx:id=\"nameColumn\" was not injected: check your FXML file 'TablePractice.fxml'."; assert ageColumn != null : "fx:id=\"ageColumn\" was not injected: check your FXML file 'TablePractice.fxml'."; assert sexColumn != null : "fx:id=\"sexColumn\" was not injected: check your FXML file 'TablePractice.fxml'."; Person person1 = new Person("田中", "20歳", "男"); nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); ageColumn.setCellValueFactory(new PropertyValueFactory<>("age")); sexColumn.setCellValueFactory(new PropertyValueFactory<>("sex")); tvObservableList.add(person1); table.setItems(tvObservableList); } }
fxml
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.TableColumn?> <?import javafx.scene.control.TableView?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TablePracticeController"> <children> <TableView fx:id="table" layoutX="55.0" layoutY="44.0" prefHeight="312.0" prefWidth="491.0"> <columns> <TableColumn fx:id="nameColumn" prefWidth="165.0" text="name" /> <TableColumn fx:id="ageColumn" prefWidth="129.0" text="age" /> <TableColumn fx:id="sexColumn" prefWidth="196.0" text="sex" /> </columns> </TableView> </children> </AnchorPane>
class
package application; import javafx.beans.property.SimpleStringProperty; public class Person { private SimpleStringProperty name; private SimpleStringProperty age; private SimpleStringProperty sex; public Person(String name, String age, String sex) { super(); this.name = new SimpleStringProperty(name); this.age = new SimpleStringProperty(age); this.sex = new SimpleStringProperty(age); } public String getName() { return name.get(); } public void setName(String name) { this.name.set(name); } public String getAge() { return age.get(); } public void setAge(String age) { this.age.set(age); } public String getSex() { return sex.get(); } public void setSex(String sex) { this.sex.set(sex); } }
まだ回答がついていません
会員登録して回答してみよう