Re: JScrollPane content sizing



On May 22, 2:35 am, MRe <pgd...@xxxxxxxxx> wrote:
Hi,

I'm new to Java GUI programming, and it's been a constant struggle
to write a GUI that looks like it was on purpose. Well, JScrollPane
has been giving me more trouble than it's worth, so as a last resort,
maybe someone could just tell me how to fix it?..

What I want to do is have a JScrollPane containing a JPanel. The
JPanel should automatically size to the width of the JScrollPane's
viewport, and to the minimum height of the contents of the JPanel. The
idea being that the JPanel's controls spring to fit the width of the
JScrollPane, (never showing a horizontal scroll bar), but be compact
vertically, showing a vertical scrollbar when the JScrollPane is too
small.

Hope that makes some sense; any help greatly appreciated

Thank you,
Kind regards,
Eliott

I've never tried to do quite what you are attempting, so I don't know
how helpful this will be. If you post some example code, it may make
it easier for people to give you help.

The JOptionPane usually sizes itself to fit what it contains. As an
experiment, create a JTextArea using new JTextArea(), and add it to a
JScrollPane with new JScrollPane(the JTextArea). When you layout the
JScrollPane and show it on the screen, you won't be able to see the
scroll pane because it is 1 by 1 pixel in size. Now go back and
create the JTextArea with new JTextArea(80, 80). Now you can see the
scroll pane because the text area has a size that the viewport can use
to set its size.

I'm not sure what will happen if you remove and then add different
objects to the scroll pane. You might try using a card layout, and
having a JTextArea or something else with a well defined size as the
first object in the layout. You could then add and remove the other
members to the card layout and only show the object you want to show.
I would guess that you will have to play around with setting
preferred, or perhaps minimum and maximum sizes, to get the exact
effect you want.

Good luck,

John
.



Relevant Pages