Swing: Can't set colour of jtextfield inside a panel with image - java

I have a JPanel which has a background image. To this panel, I add a JTextField, and set the background color to that that text field.
I expected the background to get the colour, but it is being overriden by the background image of panel.
Is this expected behavior? If not, how to get around it?

Check that your textfield is opaque.
Swing behaviour depends on many properties and layouts. You can try to debug your swing components with this tool and make some experiments with properties(background, opaque, ...).

Related

Java JLabel background color cant be changed using Netbeans

I created JFrame and jLabel using NetBeans design view. I changed jLabel background color by changing background color property from Netbeans design view properties panel. But jLabel background color didn't change. Is there any other property that I should change before changing the background color of a jLabel?
You'd need to set opaque to true
yourLabel.setOpaque(true);
(Or tick the opaque box in the label's properties in design view in NetBeans)

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.

Transparent JPanel not working correctly

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

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);

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