This is a lazy holiday here in Germany, therefore combining the two answers:
final JLabel label = new JLabel("some label with a nice text"); label.setBackground(Color.YELLOW); MouseAdapter adapter = new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { label.setOpaque(true); label.repaint(); } @Override public void mouseExited(MouseEvent e) { label.setOpaque(false); label.repaint(); } }; label.addMouseListener(adapter);
The problem (in fact, I think this is a mistake) is that setting an opaque property does not cause a redraw, as it would be appropriate. JComponent fires a change event, but it looks like no one is listening:
public void setOpaque(boolean isOpaque) { boolean oldValue = getFlag(IS_OPAQUE); setFlag(IS_OPAQUE, isOpaque); setFlag(OPAQUE_SET, true); firePropertyChange("opaque", oldValue, isOpaque); }
source share