【问题标题】:editable listview with custom object带有自定义对象的可编辑列表视图
【发布时间】: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


    【解决方案1】:

    您可能必须以某种方式处理这些对象的设置 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();
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 2016-12-13
      相关资源
      最近更新 更多