vondip
vondip

Reputation: 14029

scrollable JPanel in java

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!

Upvotes: 0

Views: 4603

Answers (3)

camickr
camickr

Reputation: 324078

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.

Upvotes: 4

aperkins
aperkins

Reputation: 13114

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).

Upvotes: 1

Pierre
Pierre

Reputation: 35226

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

Upvotes: 0

Related Questions