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

50
LINES

< > BotCompany Repo | #1012958 // AutoComboBox2 - with flexible entry maker

JavaX fragment (include)

sclass AutoComboBox2 extends JComboBox<Object> {
  F1<S, Collection<S>> entryMaker;
  
  *() { this(null); }
  
  *(F1<S, Collection<S>> *entryMaker) {
    setSelectedIndex(-1);
    setEditable(true);
    JTextField text = cast this.getEditor().getEditorComponent();
    text.setFocusable(true);

    text.addKeyListener(new KeyAdapter {
      public void /*keyPressed*/keyReleased(KeyEvent key) {
        if (key.getKeyCode() == KeyEvent.VK_ENTER) ret;
        
        if (key.getKeyCode() == KeyEvent.VK_ESCAPE) { hidePopup(); ret; }
        
        if (key.getKeyCode() == KeyEvent.VK_TAB /*&& key.getModifiers() == 0*/) {
          _print("Have tab event (modifiers=" + key.getModifiers() + ")");
          if ((key.getModifiers() & ActionEvent.SHIFT_MASK) == 0 && isPopupVisible()) {
            setSelectedIndex(0); // accept item
            hidePopup();
          } else // standard tab behavior
            swing_standardTabBehavior(key);
            
          ret;
        }
        
        JTextField tf = cast getEditor().getEditorComponent();
        if (tf.getCaretPosition() != l(tf.getText())) ret;
        String text = tf.getText();
    
        Vector list = new Vector(getSuggestions(text));
        if (nempty(list)) {
          setModel(new DefaultComboBoxModel(list));
          setSelectedIndex(-1);
          tf.setText(text); // necessary?
          showPopup();
        } else
          hidePopup();
      }
    });
      
    text.setFocusTraversalKeysEnabled(false);
  }
  
  Collection<S> getSuggestions(S text) {
    ret callF(entryMaker, text);
  }
}

Author comment

Began life as a copy of #1007529

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1012958
Snippet name: AutoComboBox2 - with flexible entry maker
Eternal ID of this version: #1012958/8
Text MD5: 84e69f874882bde08014c5592b6a2edd
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-18 22:11:56
Source code size: 1635 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 445 / 1028
Version history: 7 change(s)
Referenced in: #1012962 - AutoComboBox3 - with background thread
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)