エラー内容
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
1package application; 2 3import javafx.application.Application; 4import javafx.fxml.FXMLLoader; 5import javafx.scene.Scene; 6import javafx.scene.layout.AnchorPane; 7import javafx.stage.Stage; 8 9 10public class Main extends Application { 11 @Override 12 public void start(Stage primaryStage) { 13 try { 14 AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("TablePractice.fxml")); 15 Scene scene = new Scene(root,600,400); 16 scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 17 primaryStage.setScene(scene); 18 primaryStage.show(); 19 } catch(Exception e) { 20 e.printStackTrace(); 21 } 22 } 23 24 public static void main(String[] args) { 25 launch(args); 26 } 27} 28
controller
1/** 2 * "TablePractice.fxml"コントローラ・クラスのサンプル・スケルトン 3 */ 4/** 5 * "TablePractice.fxml"コントローラ・クラスのサンプル・スケルトン 6 */ 7 8package application; 9 10import java.net.URL; 11import java.util.ResourceBundle; 12 13import javafx.collections.FXCollections; 14import javafx.collections.ObservableList; 15import javafx.fxml.FXML; 16import javafx.scene.control.TableColumn; 17import javafx.scene.control.TableView; 18import javafx.scene.control.cell.PropertyValueFactory; 19 20public class TablePracticeController { 21 22 @FXML // ResourceBundle that was given to the FXMLLoader 23 private ResourceBundle resources; 24 25 @FXML // URL location of the FXML file that was given to the FXMLLoader 26 private URL location; 27 28 @FXML // fx:id="table" 29 private TableView<Person> table; // Value injected by FXMLLoader 30 31 @FXML // fx:id="nameColumn" 32 private TableColumn<Person, String> nameColumn; // Value injected by FXMLLoader 33 34 @FXML // fx:id="ageColumn" 35 private TableColumn<Person, String> ageColumn; // Value injected by FXMLLoader 36 37 @FXML // fx:id="sexColumn" 38 private TableColumn<Person, String> sexColumn; // Value injected by FXMLLoader 39 40 private ObservableList<Person> tvObservableList = FXCollections.observableArrayList(); 41 42 43 @FXML // This method is called by the FXMLLoader when initialization is complete 44 void initialize() { 45 assert table != null : "fx:id=\"table\" was not injected: check your FXML file 'TablePractice.fxml'."; 46 assert nameColumn != null : "fx:id=\"nameColumn\" was not injected: check your FXML file 'TablePractice.fxml'."; 47 assert ageColumn != null : "fx:id=\"ageColumn\" was not injected: check your FXML file 'TablePractice.fxml'."; 48 assert sexColumn != null : "fx:id=\"sexColumn\" was not injected: check your FXML file 'TablePractice.fxml'."; 49 50 51 Person person1 = new Person("田中", "20歳", "男"); 52 53 54 nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); 55 ageColumn.setCellValueFactory(new PropertyValueFactory<>("age")); 56 sexColumn.setCellValueFactory(new PropertyValueFactory<>("sex")); 57 58 59 tvObservableList.add(person1); 60 table.setItems(tvObservableList); 61 } 62} 63 64
fxml
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.control.TableColumn?> 4<?import javafx.scene.control.TableView?> 5<?import javafx.scene.layout.AnchorPane?> 6 7 8<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"> 9 <children> 10 <TableView fx:id="table" layoutX="55.0" layoutY="44.0" prefHeight="312.0" prefWidth="491.0"> 11 <columns> 12 <TableColumn fx:id="nameColumn" prefWidth="165.0" text="name" /> 13 <TableColumn fx:id="ageColumn" prefWidth="129.0" text="age" /> 14 <TableColumn fx:id="sexColumn" prefWidth="196.0" text="sex" /> 15 </columns> 16 </TableView> 17 </children> 18</AnchorPane> 19
class
1package application; 2 3import javafx.beans.property.SimpleStringProperty; 4 5public class Person { 6 7 private SimpleStringProperty name; 8 private SimpleStringProperty age; 9 private SimpleStringProperty sex; 10 11 public Person(String name, String age, String sex) { 12 super(); 13 this.name = new SimpleStringProperty(name); 14 this.age = new SimpleStringProperty(age); 15 this.sex = new SimpleStringProperty(age); 16 } 17 public String getName() { 18 return name.get(); 19 } 20 public void setName(String name) { 21 this.name.set(name); 22 } 23 public String getAge() { 24 return age.get(); 25 } 26 public void setAge(String age) { 27 this.age.set(age); 28 } 29 public String getSex() { 30 return sex.get(); 31 } 32 public void setSex(String sex) { 33 this.sex.set(sex); 34 } 35 36 37 38} 39
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/09/26 05:36