如何在JavaFX中的TableView中使单元格可编辑?
我致电setEditable()
,名为setCellFactory()
,已实施WrtitableValue
,但这并没有帮助。
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.ObservableValueBase;
import javafx.beans.value.WritableObjectValue;
import javafx.beans.value.WritableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;
/**
* Table editing does not work
*/
public class TableViewEditable2 extends Application{
public static class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
public static class FirstName extends ObservableValueBase<String> implements WritableValue<String> {
private Person person;
public FirstName(Person person) {
this.person = person;
}
public String getValue() {
return person.firstName;
}
public void setValue(String value) {
person.firstName = value;
fireValueChangedEvent();
}
}
public static class LastName extends ObservableValueBase<String> implements WritableValue<String> {
private Person person;
public LastName(Person person) {
this.person = person;
}
public String getValue() {
return person.lastName;
}
public void setValue(String value) {
person.lastName = value;
fireValueChangedEvent();
}
}
@Override
public void start(Stage primaryStage) throws Exception {
ObservableList<Person> teamMembers = FXCollections.observableArrayList();
teamMembers.add(new Person("John", "Johnson"));
teamMembers.add(new Person("Ivan", "Ivanov"));
TableView<Person> table = new TableView<Person>();
table.setItems(teamMembers);
table.setEditable(true);
TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
firstNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person, String>, ObservableValue<String>>() {
public ObservableValue<String> call(TableColumn.CellDataFeatures<Person, String> param) {
FirstName ans = new FirstName(param.getValue());
return ans;
}
});
firstNameCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
TableColumn<Person,String> lastNameCol = new TableColumn<Person,String>("Last Name");
lastNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person, String>, ObservableValue<String>>() {
public ObservableValue<String> call(TableColumn.CellDataFeatures<Person, String> param) {
return new LastName(param.getValue());
}
});
lastNameCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
table.getColumns().setAll(firstNameCol, lastNameCol);
StackPane root = new StackPane();
root.getChildren().add(table);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("TableViewEditable2");
primaryStage.setScene(scene);
primaryStage.show();
}
}