JTextField and transparency

I have a JTextField and I am trying to set it up so it is somewhat transparent.

mTextField.setBackground(new Color(255,255,255,50));

But what happens when i do that is this: the textfield as a text value gets the text that i have set up for one of the neighbouring components.

I am not sure why this happens.

Has anyone experienced this problem before? Is there a fix to it?

I tried setting up the text using setText to an empty string but it happens agian. I dont have this problem when the transparency is Not set.

Cheers

Dan

[578 byte] By [DanDanAUSa] at [2007-10-3 10:51:30]
# 1
> I have a JTextField and I am trying to set it up so it is somewhat transparent.textField.setOpaque(false);
Michael_Dunna at 2007-7-15 6:16:50 > top of Java-index,Desktop,Core GUI APIs...
# 2

> I am not sure why this happens.

An opaque component is responsible for painting the entire area of the component, which basically means that the RepaintManager doesnt' have to worry about the garbage that may exist where the component is about to be repainted.

Since you background is somewhat transparent, you can see the garbage left behind. (Don't ask me what its left behind from, thats in the details of the Swing repaint manager which I know nothing about).

Michaels suggestion makes the component transparent which means Swing will paint the background of the components parent. However when you do this, the background of your component isn't painted.

So heres a solution that paints the background of the parent and the child:

JTextField textField = new JTextField("Some Text")

{

protected void paintComponent(Graphics g)

{

g.setColor( getBackground() );

g.fillRect(0, 0, getSize().width, getSize().height);

super.paintComponent( g );

}

};

textField.setBackground(new Color(0, 255, 255, 50));

textField.setOpaque( false );

camickra at 2007-7-15 6:16:50 > top of Java-index,Desktop,Core GUI APIs...