user116324
user116324

Reputation: 68

Java JButton text and focus displaying incorrectly, overlapping

I've created a simple swing GUI with JButtons. When I start the GUI, this is what it looks like.

enter image description here

If I click on each button once, it looks like this.

enter image description here

How do I remove the phantom text that on each button, and how do I remove the dark gray focus on buttons that have been clicked? Below is my button class

package gui;

import java.awt.Color;
import java.awt.Dimension;

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

public class MenuButton extends JButton{

private static final long serialVersionUID = 1L;

     public MenuButton(String text, JPanel container){
        setPreferredSize(new Dimension((int)(container.getSize().width),50));
        setBackground(new Color(168, 228, 247, 0));
        setForeground(Color.WHITE);
        setText(text);
        setOpaque(false);
        setFocusPainted(false);
        setBorderPainted(false);
        setRolloverEnabled(false);
        setContentAreaFilled(false);
    }

    public void addTo(JPanel container){
        container.add(this);
    }
}

I created a new instance of the "MenuButton" in main and added it to my Jpanel called JMenu, but have done nothing further. So all it should do at the moment is display a JButton with the appropriate text, and without all of the hover/click/mouseover highlighting effects.

Upvotes: 1

Views: 507

Answers (1)

camickr
camickr

Reputation: 324108

setBackground(new Color(168, 228, 247, 0));
setOpaque(false);

You have conflicting parameters. You are saying the button is transparent (so you don't want the background painted) but you try to give it a background color with an alpha value.

Use one or the other, but not both.

If you do decide that you want a background with an alpha value then you will have painting problems. Check out Backgrounds With Transparency for an explanation of the problem and a couple of solutions.

Upvotes: 3

Related Questions