简体   繁体   中英

editable listview with custom object

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 delete the tree object from the listview. the first item in the listview is the item that when overwriten it add the new item to the listview and spawns a new overwritable tree on the top of the listview, an exemple of this with strings and I want it with tree objects

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();
    }
}

I know how to let it work with strings but don't know how I can make it work with custum objects, already searched and found I had to use a Callback object but can't manage to let it work, even after trying for serveral hours. Thanks in advance!

You'll probably have to handle setting id of those objects somehow, but I hope this is what you were looking for

Start method:

   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();
}

Tree class:

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();
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM