Java - Transparent JScrollPane

test picture test · Aug 19, 2010 · Viewed 19.8k times · Source

I have a JTextArea and it's riding ontop of a JScrollPane. Anyways, I know I can use the getViewPort() method to set the opaque of of the view port... but I cannot seem to find any sign of how to do that... anywhere. :S

Here is what I have so far:

 if (e.getKeyCode() == KeyEvent.VK_F)
{
    if (sp.isVisible())
    {
        sp.setVisible(false);
    }
    else
    {
        sp.setVisible(true);
    }
}

Answer

Serplat picture Serplat · Aug 19, 2010

You need to use setOpaque(false) to make it transparent. Call that both on the JScrollPane, and on it's ViewPort.

sp.setOpaque(false);
sp.getViewport().setOpaque(false);

You'll also have to call setOpaque(false) on the JTextArea, if you want that transparent as well.