Thursday, February 22, 2018

Setting all JPanel, JCheckbox, JRadioButton in your project non-opaque / transparent through UIManager

To set the transparency of an individual JComponent, we use the JComponent.setOpaque(boolean opaque) to achieve it.

By default, a JPanel is opaque. To set all the JPanel objects in your whole project to be non-opaque, you can achieve it through modifying the UIManager once instead of setting each JPanel object to be non-opaque by calling setOpaque(false).

The odd thing is that the code UIManager.put("Panel.opaque", false) does not always work. In fact, it has never worked for me.

The way it works for me is by setting the background color of panels through the UIManager. There are two Color constructors that take four parameters.

Color(int r, int g, int b, int alpha)
Color(float r, float g, float b, float alpha)

The last parameter alpha specifies the transparency of the color.

For the int argument, alpha is a value between 0 and 255; 0 is completely transparent and 255 completely non-transparent.

For the float argument, alpha is a value between 0.0 and 1.0; 0.0 is complete transparent and 1.0 completely non-transparent.

To set all your JPanel objects transparent (non-opaque), use the following code before constructing any of the JPanel objects:

UIManager.put("Panel.background", new Color(110, 110, 110, 0);
or
UIManager.put("Panel.background", new Color(0.5, 0.5, 0.5, 0.0);

To set all your JPanel objects non-transparent(opaque):
UIManager.put("Panel.background", new Color(110, 110, 110, 255);
or
UIManager.put("Panel.background", new Color(0.5, 0.5, 0.5, 1.0);

Of course, you can set the transparency to any value in between.

Similarly, you can set the transparency of your JCheckbox and JRadioButton objects by setting the values for the CheckBox.background and RadioButton.foreground keys in the UIManager.

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment