Detecting JComboBox editing

主宰稳场 提交于 2019-12-10 12:45:52

问题


I have a JComboBox, once every second I want to retreive a set of strings from a database and set those strings to the contents of the JComboBox, and one of them as the currently selected value. But I also want the user to be able to edit the JComboBox and add a value to the database and set it as the current value.

I want to the be able to detect when characters are entered into the JComboBox, so I can reset a count down which prevents updating the JComboBox as long as it's not zero. My first instinct was to use a KeyListener but the Java tutorial on combo boxes says this,

Although JComboBox inherits methods to register listeners for low-level events — focus, key, and mouse events, for example — we recommend that you don't listen for low-level events on a combo box.

And they go on to say that the events fired may change depending on the look and feel.


回答1:


This is a little dicey, but it should work to listen to the Document updates on the Editor component (A JTextField).

    JComboBox cb = new JComboBox();
    Component editor = cb.getEditor().getEditorComponent();
    if (editor instanceof JTextField) {
        ((JTextField) editor).getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent documentEvent) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void removeUpdate(DocumentEvent documentEvent) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void changedUpdate(DocumentEvent documentEvent) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });                                      
    }

Those *Update(DocumentEvent documentEvent) methods should get called for every character typed/deleted from the JComboBox.




回答2:


I would like to add that the changedUpdate method will not fire a notification for plain text documents. If you are using a plain text text component, you must use insertUpdate and/or removeUpdate.

I recently had to use a document listener as a way of disabling/enabling a button if the user was editing the combo box. I did something like this and worked very well:

public class MyDocumentListener implements DocumentListener
{

  @Override
  public void insertUpdate(DocumentEvent e)
  {
     setChanged();
     notifyObservers(true);
  }

  @Override
  public void removeUpdate(DocumentEvent e)
  {
     setChanged();
     notifyObservers(false);
  }

  @Override
  public void changedUpdate(DocumentEvent e)
  {
     // Not used when document is plain text
  }
}

Then, I added this listener to the combo box like this:

((JTextComponent) combobox.getEditor().getEditorComponent())
    .getDocument().addDocumentListener(new MyDocumentListener());

This works because the document associated with the combo box is plain text. When I used changedUpdate it did not.



来源:https://stackoverflow.com/questions/8949466/detecting-jcombobox-editing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!