How to fix JButton.setBackground(new Color())? Alpha is not working properly

Viewed 188

Whenever I hover the mouse over a JButton with the setBackground method set to a new color with the alpha set to 10 it gets darker. The problem is when I take the mouse off of the button it still stays just as dark as if i were hovering over the button. On top of that when I hover over the button again it makes the button even darker.

I'm trying to make the button stay translucent so I can see the panel in the background.

import java.awt.*;
import javax.swing.*;
public class Test
{
    private JFrame f;
    private JPanel p;
    private JButton b;
    public Test()
    {
        f = new JFrame("Test");
        f.setSize(400, 200);
        p = new JPanel();
        p.setLayout(new GridLayout(1, 1));
        b = new JButton();
        b.setBackground(new Color(255, 0, 0, 10));
        p.add(b);
        f.add(p);
    }
    public static void main(String args[])
    {
        Test t = new Test();
        t.f.setVisible(true);
    }
}

What I want is to create a translucent button so I can see the panel in the background.

1 Answers

you should declare two color active and hover, and add MouseListener, where you change background color and repaint the frame

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FrameTest {
    private JFrame f;
    private JPanel p;
    private JButton b;

    private static final Color activeButtonBackground = new Color(255, 0, 0, 10);
    private static final Color hoverButtonBackground = new Color(255, 0, 0, 20);

    public FrameTest() {
        f = new JFrame("Test");
        f.setSize(400, 200);
        p = new JPanel();
        p.setLayout(new GridLayout(1, 1));
        b = new JButton();
        b.setBackground(activeButtonBackground);
        b.addMouseListener(getMouseListeners());
        p.add(b);
        f.add(p);
    }

    private MouseListener getMouseListeners() {
        return new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                //on button released
            }

            @Override
            public void mousePressed(MouseEvent e) {
                //on button pressed
            }

            @Override
            public void mouseExited(MouseEvent e) {
                //on move mouse off the button
                b.setBackground(activeButtonBackground);
                f.repaint();
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                //on button hover
                b.setBackground(hoverButtonBackground);
                f.repaint();
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                //on mouse clicked - pressed and released
            }
        };
    }


    public static void main(String args[]) {
        FrameTest t = new FrameTest();
        t.f.setVisible(true);
    }
}
Related