You're right. I must have been thinking about something else
(ScrollPane?). Anyway, the problem here is that, to the best of my
knowledge, neither AWT nor Swing has a layout manager that will readjust
it's height given a fixed width or vice versa.
When you resize a JScrollPane, you resize its viewport, but the underlying
JPanel component isn't affected. So a solution to this problem would
involve intercepting the viewport change and passing its new width on to
the JPanel. Unfortunately, as I said, you cannot simply set the width on a
container with FlowLayout and have it recompute the height. So what you
end up doing is a hack: first, set the JPanel to the proper width (computed
from the viewport's width) and let it recompute locations of it's children;
2) find the location of the last child component and compute JPanel's
proper height from that; 3) set the new dimension (I'm including sample code).
I think this is the simplest way of achieving what you want to do, but if
you find a better one, I'd like to know.
Dmitry
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test2 extends JPanel
{
static JPanel pane;
public static void main( String[] args )
{
JFrame f = new JFrame("Test");
JScrollPane sp;
f.setContentPane( sp = new JScrollPane( pane = new test2(),
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
) );
sp.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent ev )
{
JComponent c = (JComponent)ev.getSource();
Insets i = c.getInsets();
pane.setPreferredSize( new Dimension( c.getWidth()-i.left-i.right,
c.getHeight()) );
pane.revalidate();
}
} );
pane.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent ev )
{
JComponent c = (JComponent)ev.getSource();
Dimension currentPrefSize = c.getPreferredSize();
Component lastComp = c.getComponent( c.getComponentCount()-1 );
int newHeight = lastComp.getLocation().y +
lastComp.getHeight() + c.getInsets().bottom;
currentPrefSize.height = newHeight;
c.setPreferredSize( currentPrefSize );
c.revalidate();
}
} );
f.setBounds( 100,100,300,300 );
f.show();
}
public test2()
{
for( int i=0; i<20; i++ )
add( new JButton(""+i) );
}
}
At 02:12 PM 3/4/2002, Reinstein, Lenny wrote:
>I did, but that does not help. It does not scroll vertically.
>
>-----Original Message-----
>From: Dmitry Beransky [mailto:[EMAIL PROTECTED]]
>Sent: Monday, March 04, 2002 5:01 PM
>To: '[EMAIL PROTECTED]'
>Subject: Re: Interesting Problem with Scrolling.
>
>
>You need to disable horizontal scrolling. See
><http://java.sun.com/j2se/1.3/docs/api/javax/swing/JScrollPane.html#JScrollP
>ane(java.awt.Component,%20int,%20int)>
>
>
>Dmitry
>
>
>At 01:50 PM 3/4/2002, Reinstein, Lenny wrote:
> >When I just use the panel by itself with the set size, as soon as there's
> >no more place for components in one rows, they automaticaly wrap aroung
> >However, when I place the same panel into a JScrollPane, they don't wrap
> >anymore.
>
>_______________________________________________
>Advanced-swing mailing list
>[EMAIL PROTECTED]
>http://eos.dk/mailman/listinfo/advanced-swing
>_______________________________________________
>Advanced-swing mailing list
>[EMAIL PROTECTED]
>http://eos.dk/mailman/listinfo/advanced-swing
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing