uday gowda
uday gowda

Reputation: 619

Always start vertical ScrollBar from the top

nspQuestionArea = new NScrollPane(); 
nspQuestionArea.setVerticalScrollBarPolicy
(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);  

nspQuestionArea.setVerticalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);  

nspQuestionArea.setViewportView(getNpQuestionArea());    
//where getNpQuestionArea() return a panel    
nspQuestionArea.getVerticalScrollBar().setValue(0);             
nspQuestionArea.getVerticalScrollBar().setValue(0);    
nspQuestionArea.getVerticalScrollBar().setUnitIncrement(5);    
nspQuestionArea.setOpaque(false);

if i open the panel,the vertical scrollbar starts from the middle of the panel,i need to start the scrollbar always from the top.

is there any method to do like that?

thanks in advance

Upvotes: 0

Views: 969

Answers (2)

Stephen Paul
Stephen Paul

Reputation: 39055

setCaretPosition(0) worked for me

Upvotes: 0

wattostudios
wattostudios

Reputation: 8774

I'm not really sure what an NScrollPane is, or what the component returned from getNpQuestionArea() is, but it looks like they're probably just an extension of JComponents. In that case, you can probably call the following...

getNpQuestionArea().scrollRectToVisible(new Rectangle(0,0,0,0));

Also, assuming that NScrollPane is an extension of JScrollPane, if you pass in the getNpQuestionArea() into the constructor of the NScrollPane, I believe it should automatically be at the top. So, something like this...

nspQuestionArea = new NScrollPane(getNpQuestionArea());

If you parse the JComponent into the NScrollPane constructor, I believe it positions the scrollbar at the top-left of the JComponent. However, if you add the JComponent later by calling setViewportView(), it positions it centrally by default. It has something to do with the way that the JComponents are layed-out when generating the display - if you add it in the constructor, it lays out the NScrollPane to the correct size and location for the JComponent that you pass to it. However, if you create a default NScrollPane and only give it the JComponent later, it just creates a NScrollPane at a default size, rather than fitting it appropriately to the JComponent.

Give both of these a try and see how you go.

Upvotes: 1

Related Questions