我试图使该表视图正常工作,但是由于某种原因,当我运行该程序时,该表为空。我将其与Oracle和Youtube教程中给出的示例进行了多次比较,但是我无法弄清缺少的内容。
代码编译没有错误。在这里:
public class JavaFXcourseNewBoston extends Application {
Stage window;
TableView<Person> table;
public static void main(String[] args) {
launch(args); // übernimmt den String args aus main
}
@Override
public void start(Stage ersteStage) throws Exception {
window = ersteStage;
window.setTitle("Tables!");
// public Person(String fName, String lName, int age)
//TableColumn first name
TableColumn<Person, String> fnameC = new TableColumn<>("Name");
fnameC.setMinWidth(100);
fnameC.setCellValueFactory(new PropertyValueFactory<>("fName"));
//TableColumn last name
TableColumn<Person, String> lnameC = new TableColumn<>("Last Name");
lnameC.setMinWidth(100);
lnameC.setCellValueFactory(new PropertyValueFactory<>("lName"));
//TableColumn age
// WRAPPER NOT PRIMITIVE DATA TYPES!!
TableColumn<Person, Integer> ageC = new TableColumn<>("Age");
ageC.setMinWidth(50);
ageC.setCellValueFactory(new PropertyValueFactory<>("age"));
table = new TableView<>();
table.setItems(getP());
table.getColumns().addAll( fnameC, lnameC, ageC);
VBox vbox = new VBox();
vbox.getChildren().addAll(table);
//initialize window
Scene scene = new Scene(vbox, 300, 250);
window.setScene(scene);
window.show();
}
public ObservableList<Person> getP() {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person("Lisa", "Simpson", 10));
persons.add(new Person("Bart", "Simpson", 12));
persons.add(new Person("Marge", "Simpson", 32));
persons.add(new Person("Homer", "Simpson", 35));
persons.add(new Person("Maggie", "Simpson", 2));
return persons;
}
}// end class
Person类:
public class Person {
String fName;
String lName;
int age;
Person(String fName, String lName, int age)
{
this.fName = fName;
this.lName = lName;
this.age = age;
}
}
(我没有为Person写任何getter和setter,因为这里不需要它们。)
我已经尝试解决了2个小时(是的,真的),非常感谢您的帮助。