Need Vertical Flow LayoutManager Advice

J

John F.

I need to fill a JPanel container with an array of smaller, square
JPanels. They need to flow vertically down a column until they reach
the bottom of the JPanel container then start again in a new column
immediately to the right. I'll not know how many smaller JPanels I
will use until I query a database (they are contacts goint into a
contact list grid.)

For instance, if there were 22 smaller panels:
1 6 11 16 21
2 7 12 17 22
3 8 13 18
4 9 14 19
5 10 15 20

Preferably, if the container were to be resized where the height was
less, then a row would be subtracted off and a new column created:

1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19
4 8 12 16 20

I'd really appreciate some advice before I reinvent the wheel.
 
T

Thomas Hawtin

John said:
I need to fill a JPanel container with an array of smaller, square
JPanels. They need to flow vertically down a column until they reach
the bottom of the JPanel container then start again in a new column
immediately to the right. I'll not know how many smaller JPanels I
will use until I query a database (they are contacts goint into a
contact list grid.)

Perhaps the simplest hack is to use GridLayout with a ComponentListener
that sets the number of rows and columns on componentResized.

Simple layout managers aren't that hard to write. There may be some open
source solution somewhere, but that might be more hassle than it's worth.

JList with setLayoutOrientation(JList.VERTICAL_WRAP) may be enough to
meet your needs.

Tom Hawtin
 
A

Andrey Kuznetsov

I need to fill a JPanel container with an array of smaller, square
JPanels. They need to flow vertically down a column until they reach
the bottom of the JPanel container then start again in a new column
immediately to the right. I'll not know how many smaller JPanels I
will use until I query a database (they are contacts goint into a
contact list grid.)

For instance, if there were 22 smaller panels:
1 6 11 16 21
2 7 12 17 22
3 8 13 18
4 9 14 19
5 10 15 20

Preferably, if the container were to be resized where the height was
less, then a row would be subtracted off and a new column created:

1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19
4 8 12 16 20

I'd really appreciate some advice before I reinvent the wheel.

see http://jgui.imagero.com/rainLayout.html
Hint - use RainLayout.LAYOUT_POLICY_COMPUTE.
 
R

Roedy Green

I need to fill a JPanel container with an array of smaller, square
JPanels.

instead of an array of JPanels, two other approaches are a JTable and
a overriding paintComponent and using drawString to do the work.

I think you will find drawString at least an order of magnitude
faster, and probably even simpler than the other two approaches.

The big problem with it is there is no built in select/copy/paste.

see http://mindprod.com/jgloss/canvas.html
 
A

Andrey Kuznetsov

instead of an array of JPanels, two other approaches are a JTable

BTW I wrote once LinearTableModel which makes exactly what you need -
it looks at component width and changes column count.
If you need I can send you source.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top