Java - Transparent JScrollPane
Asked Answered
P

3

8

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);
    }
}
Peroxidize answered 19/8, 2010 at 0:5 Comment(8)
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.Aspirant
Where is your SSCCE (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.Professed
There is no SCCEE because I do not know how to do it.Peroxidize
Maybe I can set the background of a JScrollPane to a .gif image which is 50% transparent?Peroxidize
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!Professed
@Dan: This short example by @Professed may be a useful starting point: stackoverflow.com/questions/2846497Handshake
I TOLD YOU CAMICKR< I DO NOT HAVE A SSCCE BECAUSE I DO NOT KNOW HOW TO DO IT. GEEZ!Peroxidize
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?Professed
H
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();
            }
        });
    }
}
Handshake answered 19/8, 2010 at 1:21 Comment(2)
Actually, I was hoping I could use the Image on a BACKGROUND of a JScrollPane... since I can't really set the opacity ... but thanksPeroxidize
@Dan: Indeed, opacity is either on or off. I added an example above that may suggest a way forward.Handshake
H
32

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.

Hassanhassell answered 19/8, 2010 at 0:19 Comment(11)
Can I not just ... what's the word.. set the transparency to a certain number?Peroxidize
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 weirdPeroxidize
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.Hassanhassell
Line 572: sp.getViewPort().setOpaque(false);Peroxidize
Sorry, that was a typo. It should be getViewport() not getViewPort()Hassanhassell
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 throughPeroxidize
What do you mean by see through? Did you want it so that the entire thing disappeared, or just the background?Hassanhassell
Well i want the entire thing to be 50% transparent.Peroxidize
Maybe I can set the background of a JScrollPane to a .gif image which is 50% transparent?Peroxidize
I don't think that will work. I don't know of a way to do what you are requesting.Hassanhassell
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 backgroundDesdee
H
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();
            }
        });
    }
}
Handshake answered 19/8, 2010 at 1:21 Comment(2)
Actually, I was hoping I could use the Image on a BACKGROUND of a JScrollPane... since I can't really set the opacity ... but thanksPeroxidize
@Dan: Indeed, opacity is either on or off. I added an example above that may suggest a way forward.Handshake
U
3

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); 
Uda answered 19/8, 2010 at 0:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.