English 中文(简体)
• 如何在Java SE与AutoCompleteSupport(glazedlists)一道补充JComboBox的主要听众
原标题:How to add a key listener to JComboBox with AutoCompleteSupport (glazedlists) in Java SE

How do I add a KeyListener to JComboBox with "AutoCompleteSupport" (glazedlists) in Java SE. I am working on a small program which has a JComboBox that works with AutoCompleteSupport. I want to execute a method if I press Enter key in the JComboBox. How can I do this with AutoCompleteSupport?

最佳回答

http://publicobject.com/glazedlists/glazedlists-1.8.0/api/ca/odell/glazedlists/swing/AutoCompleteSupport.html AutoCompleteSupport (Glazed Lists),其中提到:

<>strong>JComboBox ActionEvents

在上述情况下,JComboBox公司发射一枚行动Event:

  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;
}

看像你那样,希望有<条码>行动标准/代码>,而不是<条码>。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签