问题
My app shows long lists of containers and when the user edits the content one of them, the whole list must be refreshed since the content in the other containers may change (they may also change size).
Currently I do this simply by getContentPane().removeAll()
, followed by recalculating all the containers, and then revalidate()
to show the updated form. However, when I do this the list scrolls back to the top.
How can I keep the same scroll position in the list, eg that the new instance of the just edited container stays in the same place on the screen as before? I know scrollComponentToVisible(Component)
but as far as I can see it doesn't allow me to place a container in exactly the same spot on the screen as before the refresh.
回答1:
Since you rebuilt the position might change so calling setScrollY()
directly probably won't provide the same position I don't think that's doable since you need functionality that's closer to scrollComponentToVisible
.
来源:https://stackoverflow.com/questions/39675988/rebuild-list-of-containers-without-scrolling-the-list