We help IT Professionals succeed at work.

Scroll Panel

nhvo73
nhvo73 asked
on
Hi,

I have a Java applet and in this program I created a main panel which stores all of the child components in there.  Then, I created a scroll panel to contain this main panel and manipulate it up and down.  However, when the main panel opens, it has too many fields in this panel.  As a result, the scroll bar in the scroll panel just keeps positioning at the bottom.  Do you know any method to set this scroll bar at the beginning position (on the top) of the main panel, and how to apply this?

I did something like this:

 JScrollPane scrollPane = new JScrollPane(mainPanel);
 //  Add the scroll pane to the center of the border  
 //layout
 contentPane.add(scrollPane, BorderLayout.CENTER);


Thanks a lot.

Comment
Watch Question

Commented:
If you want the scroll bars to be there all the time, you can do this:

JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

Commented:
nhvo73,

Use the method 'scrollRectToVisible(Rectangle rect)'

Set rectangle coordinates to top of scroll pane and then use this method.

cheers,
ken
CERTIFIED EXPERT
Commented:
Try it :

Rectangle bounds = yourScrollPane.getViewportBorderBounds();
if (bounds != null) {
 bounds.y = 0;
 yourScrollPane.scrollRectToVisible(bounds);
}

I hope it will help you.
Best Regards,
Valeri

Author

Commented:
Hi,

I tried all of these methods and it still did not work.  
Do you have any ideas why?

Thanks

Author

Commented:
Hi,

I tried all of these methods and it still did not work.  
Do you have any ideas why?

Thanks

Author

Commented:
So, only the parts which I want to link to are set to text/html?

Author

Commented:
opps, sorry I posted the comment in the wrong place.  Please disregard it.

Author

Commented:
Thanks Valeri.  I know your logic is correct, but it seemed not working in my program. However, thanks for your effort.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.