給定一個Person類:的JavaFX 2.0 TableViewBuilder,如何使用
public class Person {
private StringProperty firstName;
private StringProperty lastName;
public Person(String firstName, String lastName){
setFirstName(firstName);
setLastName(lastName);
}
//SETTERS
public final void setFirstName(String value) { firstNameProperty().set(value); }
public final void setLastName(String value) { lastNameProperty().set(value); }
//GETTERS
public String getFirstName() { return firstNameProperty().get(); }
public String getLastName() { return lastNameProperty().get(); }
//PROPERTY GETTERS
public StringProperty firstNameProperty() {
if (firstName == null) firstName = new SimpleStringProperty(this, "firstName");
return firstName;
}
public StringProperty lastNameProperty() {
if (lastName == null) lastName = new SimpleStringProperty(this, "lastName");
return lastName;
}
}
我重新上的TableView JavaFX的API例如:
public class TestTableViewBuilder extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jacob", "Smith"),
new Person("Isabella", "Johnson"),
new Person("Ethan", "Williams"),
new Person("Emma", "Jones"),
new Person("Michael", "Brown")
);
TableView<Person> table = new TableView<Person>();
table.setItems(data);
TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
TableColumn<Person,String> lastNameCol = new TableColumn<Person,String>("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
table.getColumns().setAll(firstNameCol, lastNameCol);
Group root = new Group();
root.getChildren().add(table);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
我一直沒有成功使用TableViewBuilder來重新創建相同的表格。任何人有一個想法如何使用JavaFX 2.0 TableViewBuilder創建一個現有的ObservableList TableView?
謝謝,所述第二片的代碼是解決方案。我從來沒有從API中找到它。 – Burabari 2012-02-17 15:05:25