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

问题描述 投票:0回答:1

所以我有一个列表视图填充了类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对象但是无法设法让它工作,即使在尝试了几个小时。提前致谢!

java listview javafx callback
1个回答
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();
}

}

© www.soinside.com 2019 - 2024. All rights reserved.