JavaFX,TableView,编辑后动态更新项目
JavaFX, TableView, dynamic update Item after edit
我正在尝试修改 TableView 的 JavaFX 示例代码
使用动态方法编辑后更新项目。
整个教程和示例代码可以在这里找到:
https://docs.oracle.com/javafx/2/ui_controls/table-view.htm
项目的更新被编程为处理程序:
firstNameCol.setOnEditCommit(
new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
((Person) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setFirstName(t.getNewValue());
}
} );
我想使用一些动态的东西而不是 setFirstName() 。
除了反射还有其他可能吗?
我已经通过以下方式获得了 属性 的名称:
String propertyName = ((PropertyValueFactory)t.getTableColumn().getCellValueFactory()).getProperty();
我可以使用 propertyName 设置 firstName 的值吗?
我知道,这可以通过反射来完成,但我想使用属性的功能。
谢谢,
安妮
说我的对象是用场景生成器构建的。但它将函数代码定义 bean class 中的字段 属性 ,这提供了使用它们的可能性。
//@FXML
@FXML
Label nom;
@FXML
Label prenom;
@FXML
private TableColumn<UtilisateurToDisplay, String> firstNameColumn;
@FXML
private TableColumn<UtilisateurToDisplay, String> lastNameColumn;
@FXML
private TableView<UtilisateurToDisplay> UserTable;
private final ObservableList<UtilisateurToDisplay> data_usr = FXCollections.observableArrayList();
public void initialize(URL location, ResourceBundle resources) {
// Initialize table with the two columns.
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
// Listen for selection changes and show the person details when changed.
UserTable.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showUserDetails(newValue));
}
private void showUserDetails(UtilisateurToDisplay person) {
if (person != null) {
// Fill the labels with info from the person object.
nom.setText(person.getNom_usr());
prenom.setText(person.getPrenom_usr());
} else {
// Person is null, remove all the text.
nom.setText("");
prenom.setText("");
}
}
}
您可以查看此地址以获取更多详细信息:
http://code.makery.ch/library/javafx-8-tutorial/part3/
你可以做到
TableColumn<Person, String> col = t.getTableColumn();
int row = t.getTablePosition().getRow();
ObservableValue<String> ov = col.getCellObservableValue(row);
if (ov instanceof WritableValue) {
((WritableValue<String>)ov).setValue(t.getNewValue());
}
如果需要,您应该能够用类型变量替换特定类型 Person
和 String
。
请注意,这与 TableColumn
上定义的默认编辑提交处理程序基本相同。
作为替代方案,您可以考虑定义一个实用程序方法,在给定标题和 属性 工厂的情况下创建 TableColumn
s。我经常使用这样的便捷方法(稍微调整以包含您的用例):
private static <S,T> TableColumn<S,T> createColumn(String title, Function<S, Property<T>> property) {
TableColumn<S,T> col = new TableColumn<>(title);
col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
col.setOnEditCommit(edit -> {
S rowValue = edit.getRowValue();
property.apply(rowValue).setValue(edit.getNewValue());
});
return col ;
}
然后你可以做
TableView<Person> table = new TableView<>();
TableColumn<Person, String> firstNameColumn = createColumn("First Name", Person::firstNameProperty);
和 table 列已经设置了 onEditCommit
。
与 PropertyValueFactory
相比,我更喜欢这种通用风格,它不是类型安全的,并且由于名称中的拼写错误等原因容易出错。自 [=33 发布以来,它确实应该被视为遗留代码=] 8,这允许使用更惯用的方法来设置单元格值工厂,如上所示。
我正在尝试修改 TableView 的 JavaFX 示例代码
使用动态方法编辑后更新项目。
整个教程和示例代码可以在这里找到:
https://docs.oracle.com/javafx/2/ui_controls/table-view.htm
项目的更新被编程为处理程序:
firstNameCol.setOnEditCommit(
new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
((Person) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setFirstName(t.getNewValue());
}
} );
我想使用一些动态的东西而不是 setFirstName() 。
除了反射还有其他可能吗?
我已经通过以下方式获得了 属性 的名称:
String propertyName = ((PropertyValueFactory)t.getTableColumn().getCellValueFactory()).getProperty();
我可以使用 propertyName 设置 firstName 的值吗?
我知道,这可以通过反射来完成,但我想使用属性的功能。
谢谢, 安妮
说我的对象是用场景生成器构建的。但它将函数代码定义 bean class 中的字段 属性 ,这提供了使用它们的可能性。
//@FXML
@FXML
Label nom;
@FXML
Label prenom;
@FXML
private TableColumn<UtilisateurToDisplay, String> firstNameColumn;
@FXML
private TableColumn<UtilisateurToDisplay, String> lastNameColumn;
@FXML
private TableView<UtilisateurToDisplay> UserTable;
private final ObservableList<UtilisateurToDisplay> data_usr = FXCollections.observableArrayList();
public void initialize(URL location, ResourceBundle resources) {
// Initialize table with the two columns.
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
// Listen for selection changes and show the person details when changed.
UserTable.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showUserDetails(newValue));
}
private void showUserDetails(UtilisateurToDisplay person) {
if (person != null) {
// Fill the labels with info from the person object.
nom.setText(person.getNom_usr());
prenom.setText(person.getPrenom_usr());
} else {
// Person is null, remove all the text.
nom.setText("");
prenom.setText("");
}
}
}
您可以查看此地址以获取更多详细信息: http://code.makery.ch/library/javafx-8-tutorial/part3/
你可以做到
TableColumn<Person, String> col = t.getTableColumn();
int row = t.getTablePosition().getRow();
ObservableValue<String> ov = col.getCellObservableValue(row);
if (ov instanceof WritableValue) {
((WritableValue<String>)ov).setValue(t.getNewValue());
}
如果需要,您应该能够用类型变量替换特定类型 Person
和 String
。
请注意,这与 TableColumn
上定义的默认编辑提交处理程序基本相同。
作为替代方案,您可以考虑定义一个实用程序方法,在给定标题和 属性 工厂的情况下创建 TableColumn
s。我经常使用这样的便捷方法(稍微调整以包含您的用例):
private static <S,T> TableColumn<S,T> createColumn(String title, Function<S, Property<T>> property) {
TableColumn<S,T> col = new TableColumn<>(title);
col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
col.setOnEditCommit(edit -> {
S rowValue = edit.getRowValue();
property.apply(rowValue).setValue(edit.getNewValue());
});
return col ;
}
然后你可以做
TableView<Person> table = new TableView<>();
TableColumn<Person, String> firstNameColumn = createColumn("First Name", Person::firstNameProperty);
和 table 列已经设置了 onEditCommit
。
与 PropertyValueFactory
相比,我更喜欢这种通用风格,它不是类型安全的,并且由于名称中的拼写错误等原因容易出错。自 [=33 发布以来,它确实应该被视为遗留代码=] 8,这允许使用更惯用的方法来设置单元格值工厂,如上所示。