简体   繁体   中英

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

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

setCaretPosition(0)为我工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM