3

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.

animuson
  • 53,861
  • 28
  • 137
  • 147
kazoo
  • 137
  • 2
  • 7
  • Could you provide an example? Is it the problem that it *doesn't* scroll and keep your content in the middle? – dacwe Sep 12 '11 at 14:30
  • I will have a long document that extends far below the visible screen. When I'm in the middle of the document, I insert a newline, and the view slips down to the bottom of the document. – kazoo Sep 12 '11 at 14:42
  • @Anyscope are you meaning when the content in its JViewPort??? – mKorbel Sep 12 '11 at 15:48
  • Ah, yes. Sorry for being unclear. – kazoo Sep 12 '11 at 15:49
  • you have two JTextPane and one separetelly JTextPane for Numbering the Rows, there are must exist (some examples by two authors, and active here too) about Line Numbering, I changed your tags – mKorbel Sep 12 '11 at 16:49

2 Answers2

2

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

kleopatra
  • 51,061
  • 28
  • 99
  • 211
0

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..

Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102