Scrollable Panel Problem

/*

* ScrollableFlowPanel.java

*

* Created on November 24, 2006, 2:25 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

/import java.awt.*;

import javax.swing.*;

publicclass ScrollableFlowPanelextends JPanelimplements Scrollable{

publicvoid setBounds(int x,int y,int width,int height ){

super.setBounds( x, y, getParent().getWidth(), height );

}

public Dimension getPreferredSize(){

returnnew Dimension( getWidth(), getPreferredHeight() );

}

public Dimension getPreferredScrollableViewportSize(){

return super.getPreferredSize();

}

publicint getScrollableUnitIncrement( Rectangle visibleRect,int orientation,int direction ){

int hundredth = ( orientation == SwingConstants.VERTICAL

? getParent().getHeight() : getParent().getWidth() ) / 100;

return ( hundredth == 0 ? 1 : hundredth );

}

publicint getScrollableBlockIncrement( Rectangle visibleRect,int orientation,int direction ){

return orientation == SwingConstants.VERTICAL ? getParent().getHeight() : getParent().getWidth();

}

publicboolean getScrollableTracksViewportWidth(){

returntrue;

}

publicboolean getScrollableTracksViewportHeight(){

returnfalse;

}

privateint getPreferredHeight(){

int rv = 0;

for (int k = 0, count = getComponentCount(); k < count; k++ ){

Component comp = getComponent( k );

Rectangle r = comp.getBounds();

int height = r.y + r.height;

if ( height > rv )

rv = height;

}

rv += ( (FlowLayout) getLayout() ).getVgap();

//System.out.println(rv);

return rv;

}

}

I got this scrollable flow panel code from somewhere else which enables me to actually add items inside the Panel and enable me to do the vertical scrolling. The code works fine and it's great.

But I encounter 1 problem. It uses flow panel and flow panel is always sort of like running all over according to the size and number of items on the display. I wish to fixed the items to become like row by column and I wanted to change this code to be compatible with SpringLayout. But not exactly sure how to change it? =(

[4390 byte] By [pandoramica] at [2007-11-26 16:39:20]
# 1
This class extends JPanel which uses FlowLayout by default so its not a matter of just changing a few words. I recommend you search the tutorials for "layout managers" and read up on BoxLayout and the Grid based layouts which are more "exact" about where objects are placed than
michael.paynea at 2007-7-8 23:06:12 > top of Java-index,Java Essentials,New To Java...
# 2
Why do you need a Scrollable JPanel? There's JScrollPanes for that.
CaptainMorgan08a at 2007-7-8 23:06:12 > top of Java-index,Java Essentials,New To Java...
# 3
ScrollPane doesn't wrap when the item reach to the boundary.
pandoramica at 2007-7-8 23:06:12 > top of Java-index,Java Essentials,New To Java...
# 4

Scrollpane doesn't wrap by itself, but the JPanel or whatever you put IN the Scrollpane should wrap according to its layout manager. Make sure you are setting the size of the JPanel if you want things to wrap at a certain point, since Scrollpane will happily display things that are too big for the screen.

michael.paynea at 2007-7-8 23:06:12 > top of Java-index,Java Essentials,New To Java...