【发布时间】:2018-04-28 16:45:03
【问题描述】:
So I have a listview filled with Objects of the class Tree, I want to be able to edit and add new items of the class tree to the listview and if possible when an item is selected and I press the delete key it will从列表视图中删除树对象。列表视图中的第一个项目是在被覆盖时将新项目添加到列表视图并在列表视图顶部生成一个新的可覆盖树的项目,这是一个带有字符串的示例,我希望它带有树对象
public void start(Stage primaryStage) {
simpleList = new ListView<>(FXCollections.observableArrayList("add new Tree here","Item1", "Item2", "Item3", "Item4"));
simpleList.setEditable(true);
simpleList.setCellFactory(TextFieldListCell.forListView());
simpleList.setOnEditCommit(new EventHandler<ListView.EditEvent<String>>() {
@Override
public void handle(ListView.EditEvent<String> t) {
simpleList.getItems().set(t.getIndex(), t.getNewValue());
if (t.getIndex() == 0){
simpleList.getItems().add(0,"add new tree here");
}
}
});
simpleList.setOnEditCancel(new EventHandler<ListView.EditEvent<String>>() {
@Override
public void handle(ListView.EditEvent<String> t) {
System.out.println("setOnEditCancel");
}
});
BorderPane root = new BorderPane();
root.setCenter(simpleList);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public class Tree {
private int id;
public int getId() {
return id;
}
private String name;
public String getName() {
return name;
}
public Tree(int id, String name){
this.id = id;
this.name = name;
}
public String toString() {
return this.getName();
}
}
我知道如何让它与字符串一起工作,但不知道如何让它与 custum 对象一起工作,已经搜索并发现我必须使用 Callback 对象但无法让它工作,即使尝试了几个小时。
提前致谢!
【问题讨论】:
标签: java listview javafx callback