Question

I want to prevent the scrollpane from scrolling when the content in its view expands. Any pointers? I've tried Googling variations on the title, as well as reading all the docs for JScrollPane, and I'm still a bit baffled.

Edit: Within the scroll pane, I have a textpane, and beside it I have another text pane that shows the line numbers of the pane beside it. I fill up this number pane every time a new line is added, so this is probably what is causing the view to advance.

Was it helpful?

Solution

to-scroll or not-to-scroll is configurable on DefaultCaret

    DefaultCaret caret = (DefaultCaret) textPane.getCaret();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

See also Rob's nice blog entry about text scrolling

OTHER TIPS

Usually JScrollPane does not do that, the getViewport().getViewPosition() remains fixed when you enlarge the view port with code like

JScrollPane scrollPane = ...;
scrollPane.getViewport().getView().setPreferredSize(...);
scrollPane.getViewport().revalidate();

Maybe your component changes in such a way that you perceive it as "scrolling". In that case you have to adjust the view position with scrollPane.getViewport().setViewPosition(...) after enlarging the view..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top