code: private static WindowFocusListener createRepaintWindowListener(final JComponent component)
    {
      if (component instanceof WindowFocusListener)
      {
        return (WindowFocusListener) component;
      }
      else
      {
        return new WindowFocusListener()
        {
          public void windowGainedFocus(WindowEvent e)
          {
            if (component instanceof WindowFocusListener)
            {
              ((WindowFocusListener) component).windowGainedFocus(e);
            }
            component.repaint();
          }
  
          public void windowLostFocus(WindowEvent e)
          {
            if (component instanceof WindowFocusListener)
            {
              ((WindowFocusListener) component).windowLostFocus(e);
            }
            component.repaint();
          }
        };
      }
    }
docs: Creates a {@link WindowFocusListener} that calls repaint on the given
  {@link JComponent} when focused gained or focus lost is called.