0

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) {}
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
sticks
  • 57
  • 9

4 Answers4

1
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.addMouseListener(new MouseListener() {
            public void mouseEntered(MouseEvent e) {
                opaque = true;
                areaFilled = true;
                borderPainted = true;
                reset();

            }

            public void mouseExited(MouseEvent e) {
                opaque = false;
                areaFilled = false;
                borderPainted = false;
                reset();
            }

            public void mouseClicked(MouseEvent e) {}
            public void mousePressed(MouseEvent e) {}
            public void mouseReleased(MouseEvent e) {}
        });
    }

    private void reset() {
        super.setOpaque(opaque);
        super.setContentAreaFilled(areaFilled);
        super.setBorderPainted(borderPainted);
    }
}
Aram Arabyan
  • 2,339
  • 1
  • 17
  • 30
1

boolean opaque = false, areaFilled = false, borderPainted = false; Fields opaque,areaFilled and borderPainted are local variables for your calss. changing it will not affect supper class!

There is lot of logic behind of setOpaque, setContentAreaFilled and setBorderPainted methods you should use it

Aram Arabyan
  • 2,339
  • 1
  • 17
  • 30
1
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Setting the variables is not enough. You must call the appropriate methods on the button as well:

        public void mouseEntered(MouseEvent e) {
                super.setOpaque(true);
                super.setContentAreaFilled(true);
                super.setBorderPainted(true);

        }

        public void mouseExited(MouseEvent e) {
                super.setOpaque(false);
                super.setContentAreaFilled(false);
                super.setBorderPainted(false);

        }

P.S: I did't test it. But this seems logically obvious enoguh

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94