Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

79
LINES

< > BotCompany Repo | #1007529 // AutoComboBox - uses scoredSearch to find items

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (5065L) is out of date.

// TODO: improve filtering

sclass AutoComboBox extends JComboBox<S> {
  String keyWord[];
  new Vector myVector;
  bool acceptOnTab; // don't work so good
  
  *() {
    setModel(new DefaultComboBoxModel(myVector));
    setSelectedIndex(-1);
    setEditable(true);
    JTextField text = (JTextField) this.getEditor().getEditorComponent();
    text.setFocusable(true);
    text.setText("");
    text.addKeyListener(new ComboListener(this, myVector));
    if (acceptOnTab) text.setFocusTraversalKeysEnabled(false);
    setMyVector();
  }
  
  /**
   * set the item list of the AutoComboBox
   * @param keyWord an String array
   */
  void setKeyWord(String[] keyWord) {
    this.keyWord = keyWord;
    setMyVector();
  }
  
  void setKeyWord(Collection<S> keyWord) {
    setKeyWord(toStringArray(keyWord));
  }
  
  private void setMyVector() {
    copyArrayToVector(keyWord, myVector);
  }
  
  class ComboListener extends KeyAdapter {
    JComboBox cb;
    Vector vector;
    
    *(JComboBox *cb, Vector *vector) {}
    
    public void /*keyReleased*/keyPressed(KeyEvent key) {
      if (key.getKeyCode() == KeyEvent.VK_ENTER) ret;
      
      if (key.getKeyCode() == KeyEvent.VK_ESCAPE) { cb.hidePopup(); ret; }
      
      if (acceptOnTab && key.getKeyCode() == KeyEvent.VK_TAB /*&& key.getModifiers() == 0*/) {
        _print("Have tab event (modifiers=" + key.getModifiers() + ")");
        if ((key.getModifiers() & ActionEvent.SHIFT_MASK) == 0 && cb.isPopupVisible()) {
          cb.setSelectedIndex(0); // accept item
          cb.hidePopup();
        } else // standard tab behavior
          swing_standardTabBehavior(key);
          
        ret;
      }
      
      JTextField tf = cast cb.getEditor().getEditorComponent();
      
      if (tf.getCaretPosition() != l(tf.getText())) ret;
      
      String text = ((JTextField) key.getSource()).getText();
  
      Vector list = getFilteredList(text);
      if (nempty(list)) {
        cb.setModel(new DefaultComboBoxModel(list));
        cb.setSelectedIndex(-1);
        tf.setText(text); // necessary?
        cb.showPopup();
      } else
        cb.hidePopup();
    }
  
    public Vector getFilteredList(String text) {
      ret emptyAfterTrim(text) ? vector : new Vector(scoredSearch(text, vector));
    }
  }
}

Author comment

by Malith, http://stackoverflow.com/questions/13681977/jcombobox-autocomplete, modified by Stefan

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1007529
Snippet name: AutoComboBox - uses scoredSearch to find items
Eternal ID of this version: #1007529/35
Text MD5: e4420a052a35bed7abdf011a63f8f56e
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-24 15:27:12
Source code size: 2362 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 1019 / 3519
Version history: 34 change(s)
Referenced in: #1012958 - AutoComboBox2 - with flexible entry maker
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)