I have created a class which extends a JButton. While the button is held down, the background of the button becomes light blue.
I've seen related posts regarding buttons with transparency, and have been solved by using setContentAreaFilled(false). But when I try that, the entire button background disappears.
I want the JButton itself to be a certain color while not being clicked, and I only want the button to change color while the mouse is being held down over it. What I've tried so far is the following:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MyButton extends JButton {
private final Color defaultColor = Color.RED;
private final Color onClickColor = Color.BLUE;
private String text;
private String tier;
public MyButton(String text, String tier, JFrame f) {
this.text = text;
this.tier = tier;
//setContentAreaFilled(false); <-- When this line is enabled, the button does not show at all and the background is fully transparent.
setBackground(defaultColor);
setForeground(Color.WHITE);
setBorderPainted(false);
setFocusPainted(false);
setText(text);
//mouse listener to change color of button while clicked
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
setBackground(onClickColor);
}
});
//change button size based on tier parameter
if (tier.equals("Primary")) {
setSize(f.getWidth()-14, 40);
}
else if (tier.equals("Secondary")) {
setSize((text.length() * 10) + 10, 44);
}
}
public static void main(String[] args) {
//create frame
JFrame f = new JFrame("frame");
f.setSize(400, 200);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//create panel
JPanel p = new JPanel();
p.setLayout(null);
//create button
MyButton b = new MyButton("TESTTEST", "Primary", f);
//build frame, panel, button, set visible
p.add(b, BorderLayout.CENTER);
f.add(p, BorderLayout.CENTER);
f.setVisible(true);
}
}
What I expected:
When I run this code, I was expecting that the button should be red as default, then while the mouse is held down on the button, it would turn blue, but once released it would turn red again.
What Actually Happens:
However, what happens is while the mouse is held down on the button it turns the default highlighted "light blue" color, and only turns Color.BLUE once the button is released. When I enable the line that sets the content area filled to false and run, the button is completely transparent.
Adding the following
@Override
public void mouseReleased(MouseEvent e){
super.mouseReleased(e);
setBackground(defaultColor);
setFocusPainted(false);
}
within the MouseListener does make the button color turn back to red on release of the mouse, but the color while the button is clicked is still the default light blue instead of the actual Color.BLUE I thought it should be set to.
I'm sure that the answer is staring me in the face and should be fairly obvious, but I can't for the life of me recognize what I'm doing wrong.
The color is changed successfully, just it not shown. If you still held down the mouse but leave the button, you can see the changed color (which turns to red after release it). You have to disable the action (not the button):