void (JTree tree, Disposable parentDisposable, Consumer<? super AbstractTestProxy> setSelection) { final ClickListener userSelectionListener = new ClickListener() { @Override public boolean onClick(@NotNull MouseEvent e, int clickCount) { setSelection.accept(getUserSelection(tree)); return true; } }; userSelectionListener.installOn(tree); final KeyAdapter keyAdapter = new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { final int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_PAGE_DOWN || keyCode == KeyEvent.VK_PAGE_UP) { setSelection.accept(getUserSelection(tree)); } } }; tree.addKeyListener(keyAdapter); Disposer.register(parentDisposable, new Disposable() { @Override public void dispose() { userSelectionListener.uninstall(tree); tree.removeKeyListener(keyAdapter); } }); }