Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JScrollPane with transparent background and content

In my app, I show a popup dialog to show a large list of cards. I display them as images in many JLabel components in a JPanel subclass. I then put that object in a JScrollPane to allow for horizontal scrolling through the cards.

I want the unused space to be transparent with a dark background to show that what's behind it is disabled. I used setBackground(new Color(50, 50, 50, 200)) to achieve the look I want, but the content behind it does not redraw, so I get artifacting.

Here's what it looks like:

Artifacting when scrolling

How would I go about fixing this? How do I get the content behind it to redraw when I scroll?

Thanks in advance.

like image 708
MaxGhost Avatar asked Mar 15 '13 00:03

MaxGhost


People also ask

How do I make JScrollPane transparent?

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.

Is JScrollPane a container?

A JScrollPane is a container that can hold one component. Said another way, a JScrollPane wraps another component. By default, if the wrapped component is larger than the JScrollPane itself, the JScrollPane supplies scrollbars.

How do I add JScrollPane to my panel?

getContentPane(). add(scrollPanel); This code will work in general to add JScrollPane to JPanel. Adjust bounds of frame, panel and scrollpane according to your requirements but ensure that the bounds of JScrollPane are within the bounds of the frame otherwise the scrollpane will not be visible.


2 Answers

Taking the window out of the equation for the momement.

The JScrollPane contains a JViewport which then contains you content. So you need to set your content pane to transparent, the viewport to transparent and then the scroll pane to transparent.

You can achieve this by using setOpaque(false) on each of these containers.

This will ensure that the repaint manager will now paint through the background.

The next problem is, Swing doesn't actually support "semi-transparent" components (that is, either it's opaque or transparent).

You can implement this by overriding the paintComponent method of the main component (the one on the viewport is probably sufficient)

like image 116
MadProgrammer Avatar answered Oct 13 '22 00:10

MadProgrammer


Try the following...might give you some relief during scrolling. You likely also have a problem when the main frame is maximized or restored. You will need a listener for those events and a similar fix.

    jScrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
        @Override
        public void adjustmentValueChanged(final AdjustmentEvent e) {
            sevenWondersframe.repaint();
        }
    });
    jScrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
        @Override
        public void adjustmentValueChanged(final AdjustmentEvent e) {
            sevenWondersframe.repaint();
        }
    });
like image 28
Java42 Avatar answered Oct 12 '22 23:10

Java42