void (JComponent owner, JComponent referent) { referent.addComponentListener(new ComponentAdapter() { @Override public void componentShown(ComponentEvent e) { toggleVisibility(e); } @Override public void componentHidden(ComponentEvent e) { toggleVisibility(e); } private void toggleVisibility(ComponentEvent e) { Component component = e.getComponent(); if (component != null) { owner.setVisible(component.isVisible()); } } }); }