title: Swing Jbutton: showing border and background only when it is hovered
body: <p>I want to add a hovering-effect to my customized Swing.JButton similar to the icon on my Chrome Browser:</p>
  
  <p>Before hover >></p>
  
  <p><img src="https://i.stack.imgur.com/OlQ3F.png" alt="enter image description here"></p>
  
  <p>After hover >></p>
  
  <p><img src="https://i.stack.imgur.com/6NTtm.png" alt="enter image description here"></p>
  
  <p>I am able to set the button in the "before" status when it is created, but I am not able to create the "border + raised-background" when it is hovered.  When I try to re-add the border to the button, I got a moving effect as after repainting a new border is inserted.</p>
  
  <p>This is my current code:</p>
  
  <pre><code>public class MyButton extends JButton implements MouseListener {
  
  public MyButton(String iconPath, String toolTip) {
      super(new ImageIcon(TipButton.class.getResource(iconPath)));
      addMouseListener(this);
      setBorder(null);
      setBorderPainted(false);
      setFocusPainted(false);
      setOpaque(false);
      setContentAreaFilled(false);
      setToolTipText(toolTip);
  }
  
  public MyButton(String iconPath, String name, String toolTip) {
      this(observers, iconPath, toolTip);
      setText(name);
  }
  
  @Override
  public void mouseClicked(MouseEvent e) {}
  
  @Override
  public void mousePressed(MouseEvent e) {}
  
  @Override
  public void mouseReleased(MouseEvent e) {}
  
  @Override
  public void mouseEntered(MouseEvent e) {
      if (e.getSource() != this) return;
  
      setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
  }
  
  @Override
  public void mouseExited(MouseEvent e) {
      if (e.getSource() != this) return;
  
      setBorder(null);
  }
  
  }
  </code></pre>
  
  <p>I suppose the main logic should be in the methods mouseEntered/mouseExited but I don't know how to get the wanted effect. Any idea?</p>