Make JLabel transparent again

I have a JLabel that changes its background color when the mouse enters it. The problem is that I want JLabel to become transparent after mouse out.

Is there an instruction I can use to accomplish this?

+6
source share
2 answers

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() { /** * @inherited <p> */ @Override public void mouseEntered(MouseEvent e) { label.setOpaque(true); label.repaint(); } /** * @inherited <p> */ @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); } 
+17
source

JLabel is transparent and opaque by default, if you want to change the background on the mouse output, then you need to:

+2
source

Source: https://habr.com/ru/post/898508/


All Articles