How can I get an event when the user types a key in the TextField of a JComboBox?

Sandip Chitale

This simply becomes a problem of getting at the textfield of a editable combobox.

if (combo.isEditable()) {
  Component c = combo.getEditor().getEditorComponent();
  if (c instanceof JTextField) {
    JTextField tf = (JTextField) c;
    // now listen for key types or
    // add Document listener to the
    // backing the textfield

How to listen on typed keys is a well understood problem. Hints are -
  • textfield.addKeyListener(...)
  • textfield.getDocument().addDocumentListsner(...); // better
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact