Can I avoid JViewport revalidation every time setViewPosition() is called?

59 Views Asked by At

I was debugging a problem with a custom scrolling view using a subclass of JViewport. I was printing some information from the layoutContainer() method in my custom LayoutManager, and saw that it was being called continuously, without any resizing to trigger it. I looked at the source and it appeared as though JViewport.setViewPosition() revalidates on each call. I thought that can't be. So I went back to another project using a standard JScrollPane, and added this code:

    myScrollPane.setLayout(new ScrollPaneLayout() {
        @Override
        public void layoutContainer(Container parent) {
            System.out.println("Layout ScrollPane");
            super.layoutContainer(parent);
        }
    });

which proved that the standard version suffers from this same problem. Is there a robust and accepted way around this? I'm using several instances of my JViewport subclass to display a waveform and other things scrolling in real time using JViewport.setViewPosition() and laying out the container (exactly the same each time) every 10 milliseconds is a big inefficiency. I can subclass JViewport and override this, but understanding why it was deemed necessary to revalidate simply for a change in the view position would help me to avoid making an new problem. Currently I'm puzzled as to why this is necessary.

So the corollary question is "why is JViewport.setViewPosition()" causing a revalidation?".

0

There are 0 best solutions below