Java – swing: hover over the radio button label on the translucent JPanel
In my question, I have an opaque JPanel and another translucent (translucent) JPanel, which is located on the first JPanel When I add radio buttons on the top JPanel The problem is that every time I type the mouse over the label area of each radio button (every time I move the 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,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 radio button problem, because on another occasion, I observed that under the same conditions, if I add jlabel to the top JPanel and add listener to the top panel, so that the color of jlabel text will change when the mouse hovers and reset to the original color when the mouse exits, the text will be redrawn in different positions, as shown in the following figure: –
If necessary, I will also release the code I think both cases have the same problem
Solution
You may get these painting artifacts because of the transparent color used for the background Jcomponents do not support transparent colors as background colors This is a good article of @ camickr, which explains this problem in detail and provides another solution