Swing: hovering mouse over radio button label on translucent JPanel
Asked Answered
U

3

6

In my problem I have an opaque JPanel and another JPanel that is translucent(semi-transparent) which sits on the first JPanel. When I have added radio buttons onto the top JPanel. The problem is every time I enter mouse over the area of the label of each radio button(and every time I move mouse away from the label), it gets darker and darker.

package trial;

import java.awt.Color;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Test {

public static void main(String arg[]){
    JFrame rootframe = new JFrame("Test panel");
    rootframe.setSize(800, 550);
    rootframe.setExtendedState(JFrame.MAXIMIZED_BOTH);

    JPanel basePanel = new JPanel(); //fills rootFrame
    basePanel.setOpaque(true);
    basePanel.setBackground(Color.yellow );     

    JPanel panelContainingRadioButtons = new JPanel();//wraps radio buttons
    panelContainingRadioButtons.setOpaque(true);
    panelContainingRadioButtons.setBackground(new Color(0,0,0,100) );

    ButtonGroup buttonGroup1 = new ButtonGroup();

    JRadioButton jRadioButton1 = new JRadioButton();
    jRadioButton1.setText("Text A...............................");
    jRadioButton1.setOpaque(false);
    jRadioButton1.setForeground( Color.white);
    buttonGroup1.add(jRadioButton1);

    JRadioButton jRadioButton2 = new JRadioButton();
    jRadioButton2.setOpaque(false);
    jRadioButton2.setForeground( Color.white);
    buttonGroup1.add(jRadioButton2);
    jRadioButton2.setText("Text B.......................");

    JRadioButton jRadioButton3 = new JRadioButton();
    jRadioButton3.setOpaque(false);
    jRadioButton3.setForeground( Color.white);
    buttonGroup1.add(jRadioButton3);
    jRadioButton3.setText("Text C................................");

    panelContainingRadioButtons.add(jRadioButton1);
    panelContainingRadioButtons.add(jRadioButton2);
    panelContainingRadioButtons.add(jRadioButton3);

    basePanel.add(panelContainingRadioButtons);

    rootframe.add(basePanel);
    rootframe.setVisible(true);

}
}

I believe this is not a problem with radio buttons because, in another occasion I have observed that under same conditions, if I added a JLabel to the top JPanel, and add listeners to the top Panel so that the color of the text of jLabel will change when the mouse is hovered over, and reset to orginal color when the mouse exits, the text gets redrawn in different places as in the image below:-

https://static.mcmap.net/file/mcmap/ZG-Ab5ovK1LYLw2ea1MAZV0hXRUQa1In/6yn3cw48n/Untitled.png

If necessary I will post that code too. I think it is the same problem that is there in both cases.

Utham answered 17/7, 2012 at 3:21 Comment(0)
C
8

You get these painting artifacts probably because of transparent color that is used for a background. JComponents do not support transparent colors as background colors. Here is a good article by @camickr that explains in details the issue and also provides an alternative solution.

Cartwright answered 17/7, 2012 at 3:50 Comment(0)
H
4

Your result is not unexpected, as the default Graphics2D composite is AlphaComposite.SRC_OVER. If you want a different result, you'll need to use a different mode; AlphaComposite.SRC, for example, is not additive. Related examples may be found here, here and here.

Haemostasis answered 17/7, 2012 at 3:50 Comment(0)
P
1

Instead of using red, green, blue, and alpha Eg: setBackground(new Color(236, 233, 216, 220)); use setBackground(new Color(236,233,216)); which is red, green, blue. It will work perfectly.

Polymorphous answered 26/2, 2013 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.