I wanted to make a button which is transparent until the user hovers over it with their mouse, so I created my own class which extends JButton. I tested it and it does make the button transparent and does detect when the user hovers over it, but it doesn't make it opaque afterwards. What do I need to change with this code?
import javax.swing.*;
import java.awt.event.*;
public class TransparentButton extends JButton {
boolean opaque = false, areaFilled = false, borderPainted = false;
public TransparentButton(Icon icon) {
super(icon);
initialise();
}
public TransparentButton(String text) {
super(text);
initialise();
}
private void initialise() {
super.setOpaque(opaque);
super.setContentAreaFilled(areaFilled);
super.setBorderPainted(borderPainted);
super.addMouseListener(new MouseListener() {
public void mouseEntered(MouseEvent e) {
opaque = true;
areaFilled = true;
borderPainted = true;
}
public void mouseExited(MouseEvent e) {
opaque = false;
areaFilled = false;
borderPainted = false;
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
}
}