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

40
LINES

< > BotCompany Repo | #1004145 // addHistoryToTextField

JavaX fragment (include)

1  
// Call this AFTER other action listeners have been registered.
2  
static void addHistoryToTextField(final JTextField input) {
3  
  final Var<Int> historyIdx = new Var(0);
4  
  final new L<S> history;
5  
  input.addKeyListener(new KeyAdapter {
6  
    public void keyPressed(KeyEvent e) {
7  
      if (e.getKeyCode() == KeyEvent.VK_UP) {
8  
        //print("up");
9  
        listSet(history, historyIdx.get(), input.getText(), "");
10  
        historyIdx.set(max(0, historyIdx.get()-1));
11  
        S text = get(history, historyIdx.get());
12  
        if (text != null) {
13  
          input.setText(text);
14  
          input.selectAll();
15  
        }
16  
      }
17  
      
18  
      if (e.getKeyCode() == KeyEvent.VK_DOWN) {
19  
        //print("down");
20  
        S text = input.getText();
21  
        if (historyIdx.get() < l(history) ||
22  
          neq(last(history), text))
23  
          listSet(history, historyIdx.get(), text, "");
24  
        historyIdx.set(min(l(history), historyIdx.get()+1));
25  
        text = get(history, historyIdx.get());
26  
        input.setText(unnull(text));
27  
        input.selectAll();
28  
      }
29  
    }
30  
  });
31  
  
32  
  input.addActionListener(actionListener {
33  
    S text = input.getText();
34  
    //print("Text: " + text);
35  
    while (history.contains("")) history.remove("");
36  
    history.remove(text);
37  
    history.add(text);
38  
    historyIdx.set(l(history));
39  
  });
40  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004145
Snippet name: addHistoryToTextField
Eternal ID of this version: #1004145/1
Text MD5: edd19b97fdb3fe2a1af577b77081fadf
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-08 14:11:51
Source code size: 1343 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 548 / 554
Referenced in: [show references]