i am trying to create a ScrollPane which contains a JPanel with a transparent background (or nearly transparent in RGBA: 0, 0, 0, 150). The problem is when ever I scroll down/up the JViewPort repaints. But it does not repaint the parent, that happens a few milliseconds later. So for a few milliseconds I see all the buttons around the scrollview inside the scrollview. Is there a fix for that? Or is it possible to override the repaint method for the Viewport to repaint the whole screen?
My Code:
scrollView = new JScrollPane(content);
scrollView.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollView.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollView.getVerticalScrollBar().setUI(new ScrollBar());
scrollView.getVerticalScrollBar().setUnitIncrement(1);
scrollView.setBorder(null);
scrollView.setViewportBorder(null);
scrollView.setBounds(20, 20, W - 40, H - 40);
scrollView.setPreferredSize(new Dimension(W - 40, H - 40));
add(scrollView, 0);
parentScreen.repaint();
Swing doesn't paint transparent components properly so this could be this issue. Swing expects a component to be opaque or non-opaque (in which case the parent component is painted first to remove painting artifacts).
Check out Backgrounds With Transparency for more information and possible solutions.