JavaFX 8如何结合可编辑的ListView和取消选择失去的焦点

问题描述

我有一个ListView,它是可编辑的。这按预期工作。我的问题现在,我想在失去焦点时取消选择选定的项目。我使用以下代码取消了对焦点丢失的选择:

this.protocolListView.focusedproperty().addListener( ( obs,ov,nv ) ->{
    if ( !nv){
        this.protocolListView.getSelectionModel().clearSelection();
    }
});

这也适用。但是,添加此选项后,我将无法编辑ListView中的项目。我第二次单击同一项目就取消了选择,这似乎是无法编辑该项目的原因。

我需要一种方法来识别我是否仍在单击同一项目。

这是我的课程:

package util;

import com.jfoenix.controls.JFXListView;
import javafx.event.EventHandler;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;


public class Protocol
{
    private JFXListView<String> protocolListView;
    private CountdownTimer      timer;
    
    public Protocol( JFXListView<String> protocolListView,CountdownTimer timer )
    {
        this.timer = timer;
        this.protocolListView = protocolListView;
        this.protocolListView.setEditable(true);
        this.protocolListView.setCellFactory(TextFieldListCell.forListView());

        // I want to deselect the selected ListViewItem when clicking something else
        this.protocolListView.focusedproperty().addListener( ( obs,nv ) ->
        {
            if ( !nv)
            {
                if(protocolListView.getSelectionModel().getSelectedItem().equals(obs.toString()))
                {
                    this.protocolListView.getSelectionModel().clearSelection();
                }
            }
        } );
        // I want to keep the old value
        this.protocolListView.setonEditCommit(new EventHandler<JFXListView.EditEvent<String>>() {
            @Override
            public void handle(ListView.EditEvent<String> t) {
                protocolListView.getItems().set(t.getIndex(),t.getNewValue());
            }

        });
        this.protocolListView.addEventHandler(KeyEvent.KEY_pressed,new EventHandler<KeyEvent>() {
                    @Override
                    public void handle(KeyEvent event) {
                        if (event.getCode().equals(KeyCode.DELETE))
                        {
                            protocolListView.getItems().remove(protocolListView.getSelectionModel().getSelectedItem());
                        }
                    }
                });
    }
    
    public void addProtocolItem( String Text )
    {
        String timer = this.timer.getTimerproperty().getValue();
        String protocolString = timer + "   " + Text;
        protocolListView.getItems().add( protocolString );
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)