// Call this AFTER other action listeners have been registered. static void addHistoryToTextField(final JTextField input) { final Var historyIdx = new Var(0); final new L 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)); }); }