我有一个javafx TableView
,
public class CellEditingExample extends Application {
private final TableView table = new TableView();
private final ObservableList<Person> data =
FXCollections.observableArrayList( new Person("A"));
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
TableColumn firstNameCol = createSimpleFirstNameColumn();
table.setItems(data);
table.getColumns().addAll(firstNameCol);
table.setEditable(true);
((Group) scene.getRoot()).getChildren().addAll(table);
stage.setScene(scene);
stage.show();
}
private TableColumn createSimpleFirstNameColumn() {
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
firstNameCol.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<Person, String> t) {
t.getRowValue().setFirstName(t.getNewValue());
System.out.println("Table Size is : " + table.getItems().size());
}
});
return firstNameCol;
}
}
和Person.java
是,
public class Person {
private final SimpleStringProperty firstName;
public Person(String firstName) {
this.firstName = new SimpleStringProperty(firstName);
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public SimpleStringProperty firstNameProperty() {
return firstName;
}
}
如果我们运行该应用程序, 我们得到一个表格,其中一行有&#34; A&#34;作为值,该行可编辑。
我想编辑下一行,即使没有值(也不影响表的项目大小),
我试过了,
FXCollections.observableArrayList( new Person("A"),new Person("")); // This increases getItems().Size();
FXCollections.observableArrayList( new Person("A"),null); // NullPointerException is thrown
有可能吗?