Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make jTextArea Transparent background

Tags:

java

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.

like image 551
Hyunjin-Kim Avatar asked May 25 '15 09:05

Hyunjin-Kim


2 Answers

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().

like image 185
Jean-Baptiste Yunès Avatar answered Sep 28 '22 00:09

Jean-Baptiste Yunès


The following worked for me.

JTextArea textArea = new JTextArea();
textArea.setOpaque(false);
textArea.setBackground(new Color(red, green, blue, alpha));

JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.getViewport().setOpaque(false);
scrollPane.setOpaque(false);
like image 42
josemr Avatar answered Sep 28 '22 02:09

josemr