7

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);
    }
}
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
test
  • 17,706
  • 64
  • 171
  • 244
  • Are you trying to set the opacity of your scroll pane (i.e. making it more/less see-through), or are you trying to just make it visible/invisible? If the latter, then I don't think you need to use the viewport to do that. – Nate W. Aug 19 '10 at 00:19
  • Where is your SSCCE (http://sscce.org) that shows what you have so far and shows the problems you are having? You've been asked for a SSCCE in the past. People are wasting too much time trying to understand your question since you can't explain your requirement very well. – camickr Aug 19 '10 at 00:45
  • There is no SCCEE because I do not know how to do it. – test Aug 19 '10 at 01:00
  • Maybe I can set the background of a JScrollPane to a .gif image which is 50% transparent? – test Aug 19 '10 at 01:03
  • 2
    You made this comment about the "text area being see through" which people don't understand so you have some code that is sort of working and if people can see what your are talking about, then maybe we can provide another solution. But if you are not willing to make the effort to post the SSCCE, then I'm sure not going to waste time guessing what you are talking about. I'm outta here! – camickr Aug 19 '10 at 01:12
  • 2
    @Dan: This short example by @camickr may be a useful starting point: http://stackoverflow.com/questions/2846497 – trashgod Aug 19 '10 at 01:45
  • I TOLD YOU CAMICKR< I DO NOT HAVE A SSCCE BECAUSE I DO NOT KNOW HOW TO DO IT. GEEZ! – test Aug 19 '10 at 01:49
  • 2
    You have some code that doesn't work the way you expect it to work. That is a SSCCE by definition. Show us your problem since you aren't able to describe the problem. Do you even bother to read suggestions given to you. Did you read the link to find out what a SSCCE is about? – camickr Aug 19 '10 at 03:20

3 Answers3

30

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.

Serplat
  • 4,367
  • 2
  • 18
  • 20
  • Can I not just ... what's the word.. set the transparency to a certain number? – test Aug 19 '10 at 00:25
  • Getting errros... here is what I have: sp.setOpaque(false); sp.getViewPort().setOpaque(false); c.setOpaque(false); and here is my error: C:\wamp\www\mystikrpg\tileGen.java:572: cannot find symbol symbol : method getViewPort() location: class javax.swing.JScrollPane sp.getViewPort().setOpaque(false); ^ 1 error Tool completed with exit code 1 weird – test Aug 19 '10 at 00:29
  • Oh, you want to set the opacity. I don't think that's possible with JScrollPanes. As far as I know, you either have to be completely transparent or compeltely opaque. – Serplat Aug 19 '10 at 00:29
  • Line 572: sp.getViewPort().setOpaque(false); – test Aug 19 '10 at 00:30
  • Sorry, that was a typo. It should be getViewport() not getViewPort() – Serplat Aug 19 '10 at 00:31
  • OK so when I do that... the background of my JExtarea becomes even though i still see text..... i'm guessing it worked? but its not see through – test Aug 19 '10 at 00:33
  • What do you mean by see through? Did you want it so that the entire thing disappeared, or just the background? – Serplat Aug 19 '10 at 00:35
  • Well i want the entire thing to be 50% transparent. – test Aug 19 '10 at 01:00
  • Maybe I can set the background of a JScrollPane to a .gif image which is 50% transparent? – test Aug 19 '10 at 01:03
  • I don't think that will work. I don't know of a way to do what you are requesting. – Serplat Aug 19 '10 at 13:00
  • I used this one BUT sadly it only displays correct as long as I dont move the scrollpane is within a Panel with half transparent background as soon as I scroll there is a strange duplication artifact of the text and the beneath laying background – Georodin Sep 05 '20 at 11:26
9

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.

ScrollPanePaint

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();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Actually, I was hoping I could use the Image on a BACKGROUND of a JScrollPane... since I can't really set the opacity ... but thanks – test Aug 19 '10 at 02:40
  • 1
    @Dan: Indeed, _opacity_ is either on or off. I added an example above that may suggest a way forward. – trashgod Aug 19 '10 at 02:56
2

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); 
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
sathya
  • 25
  • 2