JavaFX中的可编辑表单元格在尝试再次编辑时显示旧值



编辑:
-----------------------------------------------------------
我添加了类InterfaceEntry,它封装Entry并赋予它JavaFX属性:

public class InterfaceEntry {
private StringProperty service = new SimpleStringProperty();
private StringProperty username = new SimpleStringProperty();
public InterfaceEntry() {}
public InterfaceEntry(Entry entry) {
setService(entry.getService());
setUsername(entry.getUsername());
}
public Entry toEntry() {
Entry entry = new Entry();
entry.setService(getService());
entry.setUsername(getUsername());
return entry;
}
public String getService() {
return service.get();
}
public StringProperty serviceProperty() {
return service;
}
public void setService(String service) {
this.service.set(service);
}
public String getUsername() {
return username.get();
}
public StringProperty usernameProperty() {
return username;
}
public void setUsername(String username) {
this.username.set(username);
}
}

-----------------------------------------------------------


初始问题:
--------------------------------------------
我在FXML:中创建了一个这样的表

<TableView fx:id="tableView" editable="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="50.0">
<columns>
<TableColumn reorderable="false" onEditCommit="#onEditTable" text="Service">
<cellValueFactory>
<PropertyValueFactory property="service"/>
</cellValueFactory>
<cellFactory>
<TextFieldTableCell fx:factory="forTableColumn"/>
</cellFactory>
</TableColumn>
<TableColumn reorderable="false" onEditCommit="#onEditTable" text="Username">
<cellValueFactory>
<PropertyValueFactory property="username"/>
</cellValueFactory>
<cellFactory>
<TextFieldTableCell fx:factory="forTableColumn"/>
</cellFactory>
</TableColumn>
</columns>
<TableView>

我的控制器看起来像这样:

@FXML
private TableView<InterfaceEntry> tableView;
private InterfaceEntry interfaceEntry;
public void initialize() {
Entry entry = new Entry();

entry.setService("Service");
entry.setUsername("Username");
interfaceEntry = new InterfaceEntry(entry);
tableView.getItems().add(interfaceEntry);
}
public void onEditTable() {
System.out.println("Username: " + interfaceEntry.getUsername());
System.out.println("Service: " + interfaceEntry.getService());
}

这是Entry类:

public class Entry {
private String service;
private String username;
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

例如,如果我编辑Username单元格,一切似乎都很正常。但是,如果我再次尝试编辑它,旧的用户名将显示在TextField中,并且Entryusername属性仍然是旧的。

因此,如果我将Username单元格更改为newUsername,将Service单元格更改为newService,这就是输出:

Username: Username
Service: Service
Username: Username
Service: Service

这就是预期输出:

Username: newUsername
Service: Service
Username: newUsername
Service: newService

如何解决这个问题?

问题是默认事件处理程序被覆盖。我通过在运行我的逻辑之前执行它来修复它:

public void initialize {
tableView.getColumns().forEach(column -> {
EventHandler handler = column.getOnEditCommit();
handler.setOnEditCommit(event -> {
handler.handle(event);
onEditTable();
});
});
}

我还必须从FXML文档中的TableColumn元素中删除#onActionEvent="onEditTable"

最新更新