java - 我的简单表格 View 在 javaFX 中不起作用

标签 java javafx javafx-2

我是javaFx的初学者,我现在在在线toturials的帮助下将数据从我的Person对象插入到我的tableView中。但是我的代码有一些问题。既没有编译时错误也没有运行时错误。但是没有我的tableView中的数据。作为javaFx的初学者,我检查了我的代码很长时间。但我不知道为什么会这样。你能看一下吗?这是我的模态类,Person.java

public class Person {
        public  SimpleStringProperty firstName;
        public SimpleStringProperty lastName;

        public Person(String fName, String lName) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
        }
}

这是我的 Controller 类

public class FXMLDocumentController implements Initializable {

    private ObservableList<Person> personData = FXCollections.observableArrayList();

   @FXML
   private TableView<Person> table;
     @FXML
    private TableColumn<Person, String> col1;
    @FXML
    private TableColumn<Person, String> col2;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
         col1.setCellValueFactory(
            new PropertyValueFactory<Person,String>("firstName")
        );

        col2.setCellValueFactory(
            new PropertyValueFactory<Person,String>("lastName")
            );  
        personData.add(new Person("John","Leon"));
        System.out.println("data to collection added");
        table.setItems(personData);
        System.out.println("collection  to table added");


    }    

}

这是我的 FXML 代码

<AnchorPane id="AnchorPane" prefHeight="332.0" prefWidth="409.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="fucktable.FXMLDocumentController">
    <children>
      <TableView fx:id="table" layoutX="42.0" layoutY="54.0" prefHeight="200.0" prefWidth="200.0">
        <columns>
          <TableColumn fx:id="col1" prefWidth="75.0" text="C1" />
          <TableColumn fx:id="col2" prefWidth="75.0" text="C2" />
        </columns>
      </TableView>
    </children>
</AnchorPane>

感谢您的关注

最佳答案

您应该在 Person 类中包含这些方法:

public String getFirstName() {
    return firstName.get();
}

public void setFirstName(String s) {
    firstName.set(s);
}

public String getLastName() {
    return lastName.get();
}

public void setLastName(String s) {
    lastName.set(s);
}

关于java - 我的简单表格 View 在 javaFX 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33950921/

相关文章:

java - 如何将 javafx 正确导入 eclipse? ("The import cannot be resolved")

java - Java 中的正则表达式差异

CSS 对 JavaFX GUI 没有影响

JavaFX 场景生成器菜单对象不可拖动

JavaFX:为什么 FilteredList 没有实现 add()?

javafx - 按名称对 TreeView 排序

mysql - 所有值未显示在组合框中

java - 将值发送到线性布局

java - MVC :resources Spring mapping not working

java - Eclipse 3.7 Unresolved Gradle依赖关系错误