This is my first attept at using a JScrollPane. My goal is to have a
component of some sort (I'm currently using JPanel) that I can set to a
certain size and draw on. I add the component to a JScrollPane since
the size of the Panel will more the likely be larger then the JFrame.
The problem I have right now is that the JPanel that the scroll pane is
holding onto resizes itself with everything else and I never get the
scroll bars.
Code:
TestPanel panel = new TestPanel(); //just a JPanel with some
logic for pait method
scroller = new JScrollPane();
scroller.setViewportView(panel);
scroller.setPreferredSize(new Dimension(300,200));
test_frame = new JFrame();
test_frame.setLayout(new BorderLayout());
test_frame.add(scroller,BorderLayout.CENTER);
test_frame.setSize(150,150);
test_frame.setVisible(true);
Any and all help appreciated.
Thanks,
Adam
klynn47@comcast.net - 07 Jan 2005 03:26 GMT
I would try creating the JScrollPane using the panel,
scroller = new JScrollPane(panel),
and then calling the methods setPreferredSize and revalidate on the
panel.
Andrew Thompson - 07 Jan 2005 08:18 GMT
> TestPanel panel = new TestPanel(); //just a JPanel with some
The problem most likely originates from this line, or rather,
the TestPanel that you refer to but don't include[1].
klynn is on the right track, I think, from the little you have told
us, but the setting of sizes is most logically put within the TestPanel
itself.
[1] <http://www.physci.org/codes/sscce.jsp>

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
ckumar - 12 Jan 2005 10:40 GMT
Hai,
I never used scrollpane for a JPanel.But I used for JLabel, JTable. I
normally add the component to the JScrollPane.
Try this following code.
TestPanel panel = new TestPanel();
int vs =ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int hs =ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
scrollpane=new JScrollPane(panel,vs,hs);
test_frame = new JFrame();
test_frame.setLayout(new BorderLayout());
test_frame.add(scrollpane,BorderLayout.CENTER);
test_frame.setSize(150,150);
test_frame.setVisible(true);
test_frame.pack();
ackumar