本文介绍了如何使用事件处理程序onEditCommit和onEditCancel对JavaFX的2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个的ListView 编辑,但,当我添加事件处理程序 onEditCommit onEditCancel 在code,我不能更改的ListView文本。下面我code(执行,但在修改不工作):

 公共类ItensTipoStringController实现Initializable {
   @FXML
   私人的ListView lstItens;
   ArrayList的<串GT; itens =新的ArrayList<>();
   ObservableList itensObservaveis = FXCollections.observableArrayList(itens);   @覆盖
   公共无效初始化(URL网址,资源包RB){
      itens.add(伊万德罗);
      itens.add(米格尔);      lstItens.setEditable(真);
      lstItens.setCellFactory(TextFieldListCell.forListView());
      。lstItens.getItems()的addAll(itens);      lstItens.setOnEditCommit(新的EventHandler(){
        @覆盖
        公共无效手柄(事件的事件){
            的System.out.println(onEditCommit);
        }
    });    lstItens.setOnEditCancel(新的EventHandler(){
        @覆盖
        公共无效手柄(事件的事件){
            的System.out.println(onEditCancel);
        }
    });    lstItens.setOnEditStart(新的EventHandler(){
        @覆盖
        公共无效手柄(事件的事件){
            的System.out.println(onEditStart);
        }
    });
   }

}


解决方案

You mean that the value of list item didn’t change on the OnEditCommit ?

Of course, you are just printing System.out.println("onEditCommit");

You have to update your item, add this line to your handle method body.

lstItens.getItems().set(event.getIndex(), event.getNewValue());

This gist provides a complete working example, you can find more informations about JavaFX ListView here and here.

这篇关于如何使用事件处理程序onEditCommit和onEditCancel对JavaFX的2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 00:51