Transparent JPanel not working correctly - java

I want my JPanel to be semi transparent. This panel is animated and has the motion of a dropdown list. I have used the Color(r,g,b,a) constructor to achieve the transparency, however the transparency only takes effect as the panel returns to its original position. For example, as it is moving downwards it is not transparent at all, however when moves up, it spontaneously becomes transparent.
How do I fix this problem?
detailPanel.setBackground(new Color(50,50,50,220));
detailPanel.setLayout(null);
detailPanel.setBounds(0,posY,1200,750);

Check out Backgrounds With Transparency for the cause of the problem (you can't use a transparent Color on an opaque background) and a couple of solutions to fix the problem:
make the component non-opaque and paint the background yourself
use the AlphaContainer class so it can do the painting of the background for you

Related

JButton, JLabel, JTextArea is flapping while the background color alpha value is set to less then 255 (200)

I am trying to set the background of a JTextArea transparent with alpha value 200 and added to a LayerPan, which support Z index, the behind of the JTextArea is JPanel which is using for playing video, in the front is a JTextArea which is using to show some text, everything works well, but the JTextArea background is flapping. For better understanding the question please look at the image below:
Case 1: Normal
Case 2: Abnormal
In the second case, you will see that the background is not correct, it is something that I don't want. What can be the problem?
I am trying to set the background of a JTextArea transparent with alpha value 200
I've never tried playing with video and transparent background before but in general Swing does not properly support transparent backgrounds. The transparency breaks the painting rules since the component is neither fully opaque or transparent.
So basically you need to make sure the background of the parent component is painted first. Then you need to paint the background of your component to get the transparency.
Check out Background With Transparency for more information and a simple class that does this painting for you.

Jlabel Extra Clickable Space

I create buttons using jlabels, so I can make a image into sort of a button. The only problem is, is that jlabels are square, therfore if i click somewhere within the square where the picture is not contained, it still runs the jlabel.MouseClickEvent. Is there any fix for this, or another component that i could use?
Ex. If i click this on the corner where the circle is not showing, but the square is still there, then the event fires.
Any fixes/different components to use? Thanks!
If you are just using simple Shapes for the images then you might be able to use the Shape Component found in Playing With Shapes.
The ShapeComponent will only respond to mouse events within the bounds of the Shape.
Otherwise the solution is to override the contains(...) method of your JLabel to check if the mouse point is in the bounds of your image, or in your case if the pixel at that location is not transparent.

How do I set the background to be transparent in a HTML Component in LWUIT?

When I use a HTMLComponent in LWUIT 1.5, it seems that the background is defaulted to white.
How do I make it transparent, or at least a background color that fills the entire screen?
I have tried the following:
.getStyle.setBgTransparency(0);
.getUnselectedStyle().setBgTansparency(0);
.getSelectedStyle().setBgTransparency(0);
.getStyle.setBgColor(0xff0000);
.getUnselectedStyle().setBgColor(0xff0000);
.getSelectedStyle().setBgColor(0xff0000);
All of the above are ignored.
I have also tried putting a color in the body:
.setBodyText("<div style='background: #ff0000'>Some text</div>");
.setBodyText("<body style='background: #ff0000'>Some text</body>");
.setBodyText("<body bgcolor='#ff0000'>Some text</body");
But that only puts the background color behind the text and does not fill the entire screen in height.
I have also tried putting the component in CENTER of a BorderLayout to make it fill the entire screen. Still no go.
Best would be if I can just make the background in the HTMLComponent transparent, so that I can constroll the background from the container component. Is this possible?
I managed to put a full screen background color by putting the HTMLComponent CENTER in a BorderLayout and then use this:
.setHTML("<html><body bgcolor='#eeee00'>This is some text</body></html>", "UTF-8", "", true);

How do I make a Swing JComponent a bigger mouse target?

I have a JPanel that contains a bunch of Swing JComponents, including some JSeparators that may be only one or two pixels wide. I want to let my users drag the items around, but it can be rather difficult to hit a one or two pixel wide line. Is there a way that I can give those JSeparators a wider "target" region for mouse clicks? The only thing I've been able to think of is to have my mouse handler listen for clicks on the JPanel, and if it gets any, run through the list of JSeparators, looking to see if any of them are within a couple of pixels of the mouse click.
Should that work? Is there a better way?
Add a fat EmptyBorder to the component.
If it already has a border, you can set a compound border using the current border then the empty border, or simpler, add the empty border (and listener) to a panel that contains the component. The latter will work better for components such as JButton, which have borders that change according to state and focus.

JLabel over transparent painted Rectangle?

I set the background of a JWindow completely transparent. Then I painted a rounded Rectangle (RGB: 0,0,0,100) in it's paint-Method and added a JLabel to the JWindows ContentPane. But when I try to repaint the JWindow to update the JLabel, it doesn't remove the old Rectangle and the old value of the JLabel. So the result is that my custom tooltip box (what it should be) gets less transparent and you cannot read the JLabels contents, because it overlays the old contens.
Is there any way to solve this problem?
BTW, if I don't repaint, it doesn't get less transparent, but the new contents of my JLabel overlays the old Contents, like it is, when I repaint.
First you should override paintComponent instead of paint and call super.paintComponent(g). You should leave JWindow opaque, because the component on the rearmost layer will clear the old contents. If all the layers are transparent you will end up with screen garbage.
See painting with Swing. Perhaps you really wanted to create translucent windows?

Categories

Resources