views:

62

answers:

2

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);
    }
}
+2  A: 

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
Can I not just ... what's the word.. set the transparency to a certain number?
Dan
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 symbolsymbol : method getViewPort()location: class javax.swing.JScrollPane sp.getViewPort().setOpaque(false); ^1 errorTool completed with exit code 1weird
Dan
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
Line 572: sp.getViewPort().setOpaque(false);
Dan
Sorry, that was a typo. It should be getViewport() not getViewPort()
Serplat
oh, good catch!
Dan
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
Dan
What do you mean by see through? Did you want it so that the entire thing disappeared, or just the background?
Serplat
Well i want the entire thing to be 50% transparent.
Dan
Maybe I can set the background of a JScrollPane to a .gif image which is 50% transparent?
Dan
I don't think that will work. I don't know of a way to do what you are requesting.
Serplat
+2  A: 

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 http://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();
            }
        });
    }
}
trashgod
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
Dan
@Dan: Indeed, _opacity_ is either on or off. I added an example above that may suggest a way forward.
trashgod