Reputation: 2503
I have two bugs when I use a treeview
, here's what happen :
BUG 1 : Not validate changes
How to create the bug :
Let's suppose I want to edit the cell "Id" in my treview.
I enter something else :
But I change my mind, I don't want to edit this item, so instead of clicking on enter and validate,I click left on something else, everything seems ok, I'm back to picture one, but if I click to edit the "Id" cell again, i'm directly back at picture 2, and "Id-Edit" is show to me, it means the cancel of the edit didn't work well ...
BUG 2 : The change of value
It happens rarely but happens, when I click on a celle to edit it, this cell take the text of another cell, here's an example :
The "Id" take the text of another cell when I edit it, but if I cancel the edit, eveything is back to normal again, like in picture one.
Here's the code that I use for my treeview :
public class DatabaseStructureView extends TreeView<Object> {
TreeItem<Object> root;
public DatabaseStructureView() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("DatabaseStructureView.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
this.setEditable(true);
this.setCellFactory((TreeView<Object> param) -> new TextFieldTreeCellImpl());
root = new TreeItem<>();
this.setRoot(root);
setShowRoot(false);
root.setExpanded(true);
}
private final class TextFieldTreeCellImpl extends TreeCell<Object> {
private TextField textField;
@Override
public void startEdit() {
super.startEdit();
if (textField == null) {
createTextField();
}
setText(null);
setGraphic(textField);
textField.selectAll();
}
@Override
public void cancelEdit() {
super.cancelEdit();
setText(getString());
setGraphic(getTreeItem().getGraphic());
}
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
}
setText(null);
setGraphic(textField);
} else {
setText(getString());
setGraphic(getTreeItem().getGraphic());
}
}
}
private void createTextField() {
textField = new TextField(getString());
textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
commitEdit(getDatabase(textField.getText()));
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
});
}
private Database getDatabase(String text) {
return new Database(text);
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
}
/**
*
* @param db
*/
public void setDatabase(Database db) {
TreeItem<Object> dbRoot = new TreeItem<>(db);
dbRoot.setExpanded(true);
db.getVariables().stream().forEach((var) -> {
dbRoot.getChildren().add(new TreeItem<>(var));
});
if (!root.getChildren().contains(dbRoot)) {
root.getChildren().add(dbRoot);
}
}
/**
*
* @param dbs
*/
public void setDatabases(List<Database> dbs) {
dbs.stream().forEach((db) -> {
setDatabase(db);
});
}
}
As you may see, my tree view is not fill with String
but with an object that I create, and is Database
, that's why I use Object
type. I think my problem comes from the update function, but don't know why.
Thanks for reading :)
Upvotes: 0
Views: 104
Reputation: 209358
In the startEdit()
method, you need to set the text of the text field to the correct value:
@Override
public void startEdit() {
super.startEdit();
if (textField == null) {
createTextField();
}
textField.setText(getString());
setText(null);
setGraphic(textField);
textField.selectAll();
}
Upvotes: 2