Search code examples
javaswingjscrollpanebufferedimage

JScrollPane: repaint offscreen buffered image


I have a JScrollPane component:

    scroller = new JScrollPane();
    scroller.getViewport().add(bigPanel);
    scroller.getViewport().setDoubleBuffered(true);

The bigPanel is painted into a bufferedImage which is displayed at startup and everything looks ok. I can scroll up and down without any problem. However when I enlarge bigPanel it updates the bufferedImage but saves only the part which is visible in JScrollPane (with correct dimensions, just the content outside of screen is blank).

I've tried to update buffered image on scroll bar change which is terribly expensive and causes that the image is rendered with white stripes in between...

   scroller.getVerticalScrollBar().addAdjustmentListener(this);

So, is there a way how to paint the component the way it is done at the beginning or it's better to write my own scroller?

I'll try to work on minimal working code, but it's a bit difficult to isolate this. The panels consists of filled rectangles.


Solution

  • One approach is to scale the old BufferedImage using drawImage() while getValueIsAdjusting() remains true. Revise the BufferedImage only after adjustment ceases. If the update is time-consuming, incremental updates may be worth the effort.