tags:

views:

416

answers:

3

Hi all,

I'm building this small java applet in which I need a JPanel which will support a scroll bar. I've tried different solutions, including scrollpanes and so forth, they worked fine when I used them in java applications and in IFrames, but from some reason when I moved them to java applets and inside other panels instead of JFrames they scrollbar just did not appear.

What I am trying to do is build a small app, with an expending Graphic2d (it changes through time), once it reaches a certain width I want a scroll panel to appear and allow moving throughout it.

Thank you!

A: 

try to use setPreferredSize(Dimension ) and /or setMinimumSize(Dimension ) on your JPanel and/or your JScrollPane

Pierre
+1  A: 

You will need to use a JScrollPane and place the internal component (probably an extended JPanel based on what you are describing) inside it. You will then need to make the JScrollPane fill it's parent - the JFrame, JDialog, applet window, etc. This will then make it the size it needs to be, and then the scroll bars should appear on demand (you can turn them on always also through a method on JScrollPane, but usually you want on-demand scroll bars).

aperkins
how do I get it to : "fill it's parent - the JFrame, JDialog, applet window, etc", add it to the content container?
vondip
ok I think I understand why no scroll bar appears. When I put a bunch of buttons instead of a graphics2d object inside I get the scroll bar working. It's something to do with this graphics2d object
vondip
Filling it's parent is dependent on the layout you are using. If you are using gridbag, then you use weightx and weighty = 1.0 : on other layout managers it will be different
aperkins
I haven't done this in a while, but you can set the size of the graphics2d object (iirc), or at the very least the component it is on, which would then force a repaint. So you would change the minimum size of the component that you are painting, and then in the paintComponent method you would have an appropriately sized graphics2d object
aperkins
+2  A: 

Scrollbars appear automatically when the "preferred size" of the component added to the viewport of the scrollpane is greater than the size of the scrollpane.

Since you are doing custom painting on the panel, you are responsible for setting the preferred size of the panel as it changes.

camickr