hyunjin-Kim hyunjin-Kim - 7 months ago 14
Java Question

How to make jTextArea Transparent background

I want to make a transparent background jTextArea.
I try to setBackground(new color(0,0,0,0));
jTextField is Working, jTextArea didn't working.

like this code.

// Not working.. Just remains gray.
jScrollPane1.setOpaque(false);
jScrollPane1.setBackground(new Color(0,0,0,0));
jTextArea1.setOpaque(false);
jTextArea1.setBackground(new Color(0,0,0,0));

// Working.. As it wants to be transparent.
jTextField1.setOpaque(false);
jTextField1.setBackground(new Color(0,0,0,0));


enter image description here

How can I jTextArea transparent background?

Thanks & Regards.

Answer

A JScrollPane is a composed component, it controls/contains a JViewport which is the component that does the drawings. See API:

A common operation to want to do is to set the background color that will be used if the main viewport view is smaller than the viewport, or is not opaque. This can be accomplished by setting the background color of the viewport, via scrollPane.getViewport().setBackground(). The reason for setting the color of the viewport and not the scrollpane is that by default JViewport is opaque which, among other things, means it will completely fill in its background using its background color. Therefore when JScrollPane draws its background the viewport will usually draw over it.

So you should change the opaque and color properties of the JViewportas well. You can access it with jScrollPane1.getViewport().