java - 如何在 Java SE 中使用 AutoCompleteSupport (glazedlists) 向 JComboBox 添加关键监听器

标签 java jcombobox

如何在 Java SE 中使用“AutoCompleteSupport”(glazedlists)将 KeyListener 添加到 JComboBox。 我正在开发一个小程序,它有一个与 AutoCompleteSupport 配合使用的 JComboBox。如果我在 JComboBox 中按 Enter 键,我想执行一个方法。我如何使用 AutoCompleteSupport 执行此操作?

最佳答案

查看 AutoCompleteSupport (Glazed Lists)其中提到:

JComboBox ActionEvents

A single ActionEvent is fired from the JComboBox in these situations:

  1. the user hits the enter key
  2. the selected item within the popup is changed (which can happen due to a mouse click, a change in the autocompletion term, or using the arrow keys)
  3. the JComboBox loses focus and contains a value that does not appear in the ComboBoxModel

另请查看来源中的这些摘录:

public void keyPressed(KeyEvent e) {
    if (!isTableCellEditor)
        doNotTogglePopup = false;

    // this KeyHandler performs ALL processing of the ENTER key otherwise multiple
    // ActionEvents are fired to ActionListeners by the default JComboBox processing.
    // To control processing of the enter key, we set a flag to avoid changing the
    // editor's Document in any way, and also unregister the ActionListeners temporarily.
    if (e.getKeyChar() == KeyEvent.VK_ENTER) {
        doNotChangeDocument = true;
        this.actionListeners = unregisterAllActionListeners(comboBox);
    }

    // make sure this backspace key does not modify our comboBoxEditorComponent's Document
    if (isTrigger(e))
        doNotChangeDocument = true;
}

和:

public void keyReleased(KeyEvent e) {
    // resume the ability to modify our comboBoxEditorComponent's Document
    if (isTrigger(e))
        doNotChangeDocument = false;

    // keyPressed(e) has disabled the JComboBox's normal processing of the enter key
    // so now it is time to perform our own processing. We reattach all ActionListeners
    // and simulate exactly ONE ActionEvent in the JComboBox and then reenable Document changes.
    if (e.getKeyChar() == KeyEvent.VK_ENTER) {
        updateFilter();

        // reregister all ActionListeners and then notify them due to the ENTER key

        // Note: We *must* check for a null ActionListener[]. The reason
        // is that it is possible to receive a keyReleased() callback
        // *without* a corresponding keyPressed() callback! It occurs
        // when focus is transferred away from the ComboBoxEditor and
        // then the ENTER key transfers focus back to the ComboBoxEditor.
        if (actionListeners != null) {
            registerAllActionListeners(comboBox, actionListeners);
            comboBox.actionPerformed(new ActionEvent(e.getSource(), e.getID(), null));
        }

        // null out our own reference to the ActionListeners
        actionListeners = null;

        // reenable Document changes once more
        doNotChangeDocument = false;
    }

    if (!isTableCellEditor)
        doNotTogglePopup = true;
}

看起来您想要一个 ActionListener 而不是 KeyListener

关于java - 如何在 Java SE 中使用 AutoCompleteSupport (glazedlists) 向 JComboBox 添加关键监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8183459/

相关文章:

java - 帮助在 JComboBox 中的 ItemChange 时自动更改值

java - 如何使一个 jtextarea 响应另一个 jtextarea 的更改

java - 将 JComboBox 添加到 JTable 单元格。所选项目不保留

java - 如何在 Java 中执行等效的 cURL/mimetype 代码?

java - 以编程方式下载通过 PHP 页面推送的文件

java - 尝试在 Android 中退出。堆栈未正确清除

java - JComboBox 列出年龄

java - 这三种算法的复杂度是多少?

java - 在 preparedStatement 中使用可变数量的参数

java - 如何在 Java 中将浮点枚举与 Jcombobox 一起使用