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)

1  
sclass AutoComboBox2 extends JComboBox<Object> {
2  
  F1<S, Collection<S>> entryMaker;
3  
  
4  
  *() { this(null); }
5  
  
6  
  *(F1<S, Collection<S>> *entryMaker) {
7  
    setSelectedIndex(-1);
8  
    setEditable(true);
9  
    JTextField text = cast this.getEditor().getEditorComponent();
10  
    text.setFocusable(true);
11  
12  
    text.addKeyListener(new KeyAdapter {
13  
      public void /*keyPressed*/keyReleased(KeyEvent key) {
14  
        if (key.getKeyCode() == KeyEvent.VK_ENTER) ret;
15  
        
16  
        if (key.getKeyCode() == KeyEvent.VK_ESCAPE) { hidePopup(); ret; }
17  
        
18  
        if (key.getKeyCode() == KeyEvent.VK_TAB /*&& key.getModifiers() == 0*/) {
19  
          _print("Have tab event (modifiers=" + key.getModifiers() + ")");
20  
          if ((key.getModifiers() & ActionEvent.SHIFT_MASK) == 0 && isPopupVisible()) {
21  
            setSelectedIndex(0); // accept item
22  
            hidePopup();
23  
          } else // standard tab behavior
24  
            swing_standardTabBehavior(key);
25  
            
26  
          ret;
27  
        }
28  
        
29  
        JTextField tf = cast getEditor().getEditorComponent();
30  
        if (tf.getCaretPosition() != l(tf.getText())) ret;
31  
        String text = tf.getText();
32  
    
33  
        Vector list = new Vector(getSuggestions(text));
34  
        if (nempty(list)) {
35  
          setModel(new DefaultComboBoxModel(list));
36  
          setSelectedIndex(-1);
37  
          tf.setText(text); // necessary?
38  
          showPopup();
39  
        } else
40  
          hidePopup();
41  
      }
42  
    });
43  
      
44  
    text.setFocusTraversalKeysEnabled(false);
45  
  }
46  
  
47  
  Collection<S> getSuggestions(S text) {
48  
    ret callF(entryMaker, text);
49  
  }
50  
}

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: 363 / 946
Version history: 7 change(s)
Referenced in: [show references]