I have a JTextArea and it's riding on top of a JScrollPane. Anyways, I know I can use the getViewPort() method to set the opaque property of the viewport, but I cannot seem to find any sign of how to do that anywhere.
Here is what I have so far:
if (e.getKeyCode() == KeyEvent.VK_F)
{
    if (sp.isVisible())
    {
        sp.setVisible(false);
    }
    else
    {
        sp.setVisible(true);
    }
}
                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.
Your colloquy with @Serplat suggests that you may be confounding opacity and transparency.
Opacity is a boolean property of Swing components used to optimize drawing:
true: The component agrees to paint all of the bits contained within its rectangular bounds.false: The component makes no guarantees about painting all the bits within its rectangular bounds.Transparency is a means of compositing digital images, as seen in this example.
Considering the distinction may help to clarify your question or focus your search for more information.
Addendum: Based on @camickr's example, the example below shows a blue square that "sticks" to the viewport, while the gray checkerboard may be scrolled over it.

import java.awt.*;
import javax.swing.*;
/** @see https://stackoverflow.com/questions/2846497 */
public class ScrollPanePaint extends JFrame {
    private static final int TILE = 64;
    public ScrollPanePaint() {
        JViewport viewport = new MyViewport();
        viewport.setView(new MyPanel());
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewport(viewport);
        this.add(scrollPane);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
    private static class MyViewport extends JViewport {
        public MyViewport() {
            this.setOpaque(false);
            this.setPreferredSize(new Dimension(6 * TILE, 6 * TILE));
        }
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.blue);
            g.fillRect(TILE, TILE, 3 * TILE, 3 * TILE);
        }
    }
    private static class MyPanel extends JPanel {
        public MyPanel() {
            this.setOpaque(false);
            this.setPreferredSize(new Dimension(9 * TILE, 9 * TILE));
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.lightGray);
            int w = this.getWidth() / TILE + 1;
            int h = this.getHeight() / TILE + 1;
            for (int row = 0; row < h; row++) {
                for (int col = 0; col < w; col++) {
                    if ((row + col) % 2 == 0) {
                        g.fillRect(col * TILE, row * TILE, TILE, TILE);
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ScrollPanePaint();
            }
        });
    }
}
                        Code for JScrollpane Transparent Background.
  JScrollPane scrollPane = new JScrollPane();
   JViewport viewport = new JViewport();
 //Component that need to be added in Scroll pane//
   viewport.setView(new JPanel());
   viewport.setOpaque(false);
   scrollPane.setViewport(viewport);
   scrollPane.getViewport().setOpaque(false);
   scrollPane.setOpaque(false);
 // Add Scrollpane to Jframe or JPanel//
   add( scrollPane,BorderLayout.CENTER); 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With