static void onFocusLeave(JComponent c, final Runnable action) { c.addFocusListener(new FocusAdapter() { public void focusLost(FocusEvent evt) { try { action.run(); } catch (Throwable e) { e.printStackTrace(); } } }); }