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

85
LINES

< > BotCompany Repo | #1012962 // AutoComboBox3 - with background thread

JavaX fragment (include)

sbool AutoComboBox3_handleTab = true;

sclass AutoComboBox3 extends JComboBox<S> {
  F1<S, Collection<S>> entryMaker; // called (serially) in non-AWT thread
  ReliableSingleThread rst = new(r { update2() });
  S textBehindModel;
  
  *() { 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 (AutoComboBox3_handleTab && key.getKeyCode() == KeyEvent.VK_TAB /*&& key.getModifiers() == 0*/) {
          //_print("Have tab event (modifiers=" + key.getModifiers() + ")");
          if (key.getModifiers() == 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;
    
        update();
      }
    });
      
    if (AutoComboBox3_handleTab)
      text.setFocusTraversalKeysEnabled(false);
  }
  
  Collection<S> getSuggestions(S text) {
    ret callF(entryMaker, text);
  }
  
  JTextField textField() {
    ret (JTextField) getEditor().getEditorComponent();
  }
  
  void update {
    if (neq(getText(textField()), textBehindModel))
      rst.trigger();
  }
  
  bool setTextBehindModel(fS text) {
    ret swing(func -> Bool {
      if (eq(text, textBehindModel)) false;
      textBehindModel = text;
      true;
    });
  }
  
  void update2 {
    fS text = getText(textField());
    if (!setTextBehindModel(text)) ret;
    try {
      final Vector list = new Vector(getSuggestions(text));
      swing {
        if (nempty(list)) {
          S currentText = getText(textField());
          setModel(new DefaultComboBoxModel(list));
          setTextBehindModel(text);
          setSelectedIndex(-1);
          textField().setText(currentText);
          showPopup();
        } else
          hidePopup();
      }
    } catch print e {
      setTextBehindModel(null);
    }
  }
}

Author comment

Began life as a copy of #1012958

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

Comments [hide]

ID Author/Program Comment Date
1361 stefan The bug is in Linux only 2017-12-19 01:44:52
1360 stefan There is a weird bug that disables Alt+Tab when list is dropped down 2017-12-19 01:31:12

add comment

Snippet ID: #1012962
Snippet name: AutoComboBox3 - with background thread
Eternal ID of this version: #1012962/18
Text MD5: 7ff482e8a92c0997a1811f9aa8913107
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-27 01:21:45
Source code size: 2468 bytes / 85 lines
Pitched / IR pitched: No / No
Views / Downloads: 721 / 1318
Version history: 17 change(s)
Referenced in: [show references]