0

Sometimes (not always) when scrolling the bar of my JScrollPane, some of the components (usually the text ones like JLabels) don't repaint properly and they end up only being partially rendered.

I don't know why this is. I've tried invoking paint() inside of an AdjustmentListener, but that doesn't seem to help.

Any ideas?

EDIT: Initialization of the components

    panel = new JPanel();
    ImageIcon img = new ImageIcon("editor.png");
    setIconImage(img.getImage());
    initComponents();

    final JScrollPane pane = new JScrollPane(panel);
    this.setContentPane(pane);
    //pane.setLayout(new ScrollPaneLayout());
    //pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    Dimension dim = panel.getSize();
    dim.height = dim.height - 100;
    pane.setSize(dim);
    this.setSize(dim);
    AdjustmentListener hListener = new AdjustmentListener() {
        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            repaint();
            for(Component c : panel.getComponents())
                c.repaint();
            for(Component c : pane.getComponents())
                c.repaint();
            panel.repaint();
            panel.revalidate();
            pane.repaint();
            pane.revalidate();
        }
    };
    pane.getVerticalScrollBar().addAdjustmentListener(hListener);
    panel.setVisible(true);
    pane.setVisible(true);
Blackvein
  • 558
  • 4
  • 10
  • 23

1 Answers1

8

Violations of any of these basic principles can cause rendering artifact.

Addendum: Incorporating these dicta, this related example scrolls thousands of flashing JLabel instances without artifact.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045