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

40
LINES

< > BotCompany Repo | #1004145 // addHistoryToTextField

JavaX fragment (include)

// Call this AFTER other action listeners have been registered.
static void addHistoryToTextField(final JTextField input) {
  final Var<Int> historyIdx = new Var(0);
  final new L<S> history;
  input.addKeyListener(new KeyAdapter {
    public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_UP) {
        //print("up");
        listSet(history, historyIdx.get(), input.getText(), "");
        historyIdx.set(max(0, historyIdx.get()-1));
        S text = get(history, historyIdx.get());
        if (text != null) {
          input.setText(text);
          input.selectAll();
        }
      }
      
      if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        //print("down");
        S text = input.getText();
        if (historyIdx.get() < l(history) ||
          neq(last(history), text))
          listSet(history, historyIdx.get(), text, "");
        historyIdx.set(min(l(history), historyIdx.get()+1));
        text = get(history, historyIdx.get());
        input.setText(unnull(text));
        input.selectAll();
      }
    }
  });
  
  input.addActionListener(actionListener {
    S text = input.getText();
    //print("Text: " + text);
    while (history.contains("")) history.remove("");
    history.remove(text);
    history.add(text);
    historyIdx.set(l(history));
  });
}

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: 546 / 551
Referenced in: [show references]