我怎样才能避免使用这个机器人来触发我的 JComboBox?

How can I avoid using this robot to trigger my JComboBox?

我正在使用 JComboBox 为每个所选项目加载 table。 组合框是 editable,我在上面使用了 autoCompleteDecorator。 我遇到的问题是我的所有代码在搜索 组合框而不是等待 VK_ENTER。所以,我不得不使用一个变量 在按下回车键时存储字符串 "enter"。一切正常 好的(挤在一起但工作)除了我必须敲击输入 键两次以执行提交所选项目..所以而不是最终用户 必须敲两次键,我添加了一个机器人。

static JComboBox<String> drivers = new JComboBox<String>();
drivers.setEditable(true);
AutoCompleteDecorator.decorate(drivers);

drivers.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
    public void keyReleased(KeyEvent kEvent) {
        if (kEvent.getKeyChar() == KeyEvent.VK_ENTER) {
            lastKey="enter";
            try{
                Robot myBot = new Robot();
                myBot.keyPress(KeyEvent.VK_ENTER);
            }catch (AWTException e){
                JOptionPane.showMessageDialog(frame, "Something has gone terribly wrong.","myBot Failure",JOptionPane.WARNING_MESSAGE);
                e.printStackTrace();
            }
        }
    }
});

这是我的 KeyListener,这是我的 ActionListener:

drivers.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent eAction){
        if(lastKey=="enter" || !((eAction.getModifiers() & InputEvent.BUTTON1_MASK) == 0)){
            lastKey="";
            //Query the DB, load the table accordingly
            //And do a bunch of other stuff….
        }
    }
}

有没有办法让我完全不需要 KeyListener? 我想我想要完成的只是在 Enter 键上提交搜索 按 或单击鼠标左键。

不要使用 KeyListener 来监视对编辑器的更改,而是将 ActionListener 附加到编辑器或组合框,这将以独立于平台的方式执行相同的操作