Link to home
Start Free TrialLog in
Avatar of levg
levg

asked on

JScrollPane - how to enable scrolling?

Hi there!
I'd like to add JLabel to JScrollPane.
Say,
jScrollPane.setBounds(0,0,100,100);
jLabel.setBounds(0,0,150,150);
jScrollPane.add(jLabel,null);
How should I enable scrolling EXACTLY (150-100=) 50 pixels to each side?
And in common - I need exact scrolling enabled only when I get:
jLabel.getHeight() > jScrollPane.getHeight() or/and jLabel.getWidth() > jScrollPane.getWidth()
Thank you in advance.
Avatar of levg
levg

ASKER

Edited text of question.
JScrollPane uses the preferredSize of the child component to determine how to scroll.
If you use absolute positioning of the child you need to set the preferred size explicitly:
label.setPreferredSize(new Dimension(150,150));
also you would want to set the scroll policy for the scrollPane.
use these constants in the JScrollPane constructor:
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED

Avatar of levg

ASKER

Well it works. Thank you. Come and grab the points.
ASKER CERTIFIED SOLUTION
Avatar of conick
conick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial