如何在ListView中获取最后一个ListCell?

时间:2018-04-01 14:09:13

标签: java listview javafx

我已经开始在JavaFX中创建一个程序,其中我有ListView,增加了编辑,添加新项目和删除等功能。我已经成功完成了大部分工作,但仍有一些工作要做。到目前为止,我已经成功添加了删除和编辑功能,但我在添加功能方面遇到了一个小问题。我仍然可以向ListView添加新项目,但我无法编辑新添加的项目,因为我需要ListCell新项目表示开始编辑它。所以我的问题是如何在ListView中获取新项目的ListCell。

为此目的,这是我的代码:

    private TextField textField;
    ListCell<String> cell = this;
    int i = 0;
    boolean manually_selected = false;


    public LanguageListCell(ListView<String> languages) 
    {

        ContextMenu contextMenu = new ContextMenu();


        cell.setEditable(true);



        MenuItem editItem = new MenuItem();
        editItem.textProperty().bind(Bindings.format("Edit \"%s\"", cell.itemProperty()));

        editItem.setOnAction(event -> {

            // The LanguageListCell class i want to put here...
            cell.startEdit();
        });

        MenuItem addItem = new MenuItem("Add language");

        addItem.setOnAction(new EventHandler<ActionEvent> () {
            @Override
            public void handle(ActionEvent ev)
            {


                i++;

                String lang = "New Language " + i;



                languages.getItems().add(lang);



                if(i == 10)
                {

                    addItem.setDisable(true);
                }
                languages.getSelectionModel().clearSelection();
                languages.getSelectionModel().select(languages.getItems().size() - 1);
                cell.setItem(languages.getSelectionModel().getSelectedItem());

                manually_selected = true;



            }



        });


        MenuItem deleteItem = new MenuItem();
        deleteItem.textProperty().bind(Bindings.format("Delete \"%s\"", cell.itemProperty()));
        deleteItem.setOnAction(new EventHandler<ActionEvent> () {
            @Override
            public void handle(ActionEvent ev)
            {
                if(languages.getSelectionModel().getSelectedItems().size() - 1 > 0)
                {

                    if(i > 0)
                    {
                        i = (languages.getItems().size() - languages.getSelectionModel().getSelectedItems().size()) - 1;
                    }
                    /*for(String lang: languages.getSelectionModel().getSelectedItems())
                    {
                        languages.getItems().remove(lang);

                    }*/
                    ArrayList<String> delete_data = new ArrayList<String>(languages.getSelectionModel().getSelectedItems());
                    languages.getItems().removeAll(delete_data);

                }
                languages.getItems().remove(cell.getItem());
                if(i > 0) i = 0;
            }

        });

        contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent e) {
                if(languages.getSelectionModel().getSelectedItems().size() - 1 > 0)
                {
                    editItem.setDisable(true);
                    addItem.setDisable(true);
                }
            }
         });

        contextMenu.getItems().addAll(addItem, editItem, deleteItem);

        cell.textProperty().bind(cell.itemProperty());

        cell.emptyProperty().addListener((obs, wasEmpty, isNowEmpty) -> {
            if (isNowEmpty) {
                cell.setContextMenu(null);
            } else {

                cell.setContextMenu(contextMenu);

            }
        });



    }

0 个答案:

没有答案