static void textComponentDependButtons(final JTextComponent tc, L buttons) { for (Component c : buttons) if (c instanceof JButton) { final JButton b = cast c; tc.addCaretListener(new CaretListener { public void caretUpdate(CaretEvent e) { b.setEnabled(tc.getSelectionEnd() > tc.getSelectionStart()); } }); b.setEnabled(tc.getSelectionEnd() > tc.getSelectionStart()); } }