带有自定义对象的可编辑列表视图

时间:2018-04-28 16:45:04

标签: java listview javafx callback

所以我有一个列表视图填充了类Tree的对象,我希望能够编辑和添加类树的新项目到列表视图,如果可能的话,当选择一个项目时,我按下删除键它将从列表视图中删除树对象。 listview中的第一项是覆盖它的项目,它将新项目添加到listview并在listview顶部生成一个新的可覆盖树,这是一个带字符串的例子,我希望它与树对象

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对象但不能设法让它工作,即使在尝试了几个小时之后。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

您可能不得不以某种方式处理这些对象的设置ID,但我希望这是您正在寻找的

开始方法:

   public void start(Stage primaryStage) {

    ListView<Tree> simpleList = new ListView<>(FXCollections.observableArrayList(new Tree(0, "add new tree here"), new Tree(1, "Tree one"), new Tree(2, "Tree two"), new Tree(1, "Tree three"), new Tree(1, "Tree four"), new Tree(1, "Tree five")));
    simpleList.setEditable(true);

    simpleList.setCellFactory(listView -> {
        TextFieldListCell<Tree> cell = new TextFieldListCell<>();
        cell.setConverter(new StringConverter<Tree>() {
            @Override
            public String toString(Tree tree) {
                return tree.getName();
            }

            @Override
            public Tree fromString(String string) {
                Tree tree = cell.getItem();
                tree.setName(string);
                return tree;
            }
        });
        return cell;
    });

    simpleList.setOnEditCommit(t -> {
        simpleList.getItems().set(t.getIndex(), t.getNewValue());
        if (t.getIndex() == 0) {
            simpleList.getItems().add(0, new Tree(0, "add new tree here"));
        }
    });

    // init delete item handler
    simpleList.setOnKeyReleased(event -> {
        if (event.getCode().equals(KeyCode.DELETE)) {
            Tree selectedItem = simpleList.getSelectionModel().getSelectedItem();
            simpleList.getItems().remove(selectedItem);
            System.out.println(selectedItem + " deleted from list");
        }
    });

    simpleList.setOnEditCancel(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;
private String name;

public Tree(int id, String name) {
    this.id = id;
    this.name = name;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String toString() {
    return this.getName();
}

}