Noddy layout question!



Hi!

Apologies for the noddy question, I've been away from using Swing for
over a year, and I just can't work out how to do this!

I am using a card layout, which changes based on a selection elsewhere
in the GUI. The card components are composed of varying numbers of
JLabels (between 1 and 5000). What I want to achieve is a flowlayout
for the labels (so that each label takes its preferred size) and if
there are two many labels to fit in the view, for it to scroll
vertically.

The closest I have come is using the code below. It sets the width
correctly, and my buttons look the way I want them too, however, the
height of the scrolling panel is far too big for the components that
only contain one or two buttons. If I set the preferred size height to
less, it looks better for the small ones, but there is no way for me to
scroll down to see any that aren't displayed in the current view point.

Any help would be great - I don't know why I can't get my head around
this one!!!
- Laura

final JPanel filePanel = new JPanel(new CardLayout());
JScrollPane labelPanelScroll = new JScrollPane(labelPanel);
JScrollPane filePanelScroll = new JScrollPane(filePanel);

List<Label> labels = LabelManager.instance().getLabels();
for (Label label : labels) {
JButton b = new JButton(label.getName());

JPanel lPanel = new JPanel(new BorderLayout());

JPanel bPanel = new JPanel(new FlowLayout());
bPanel.setPreferredSize(new Dimension(500, 10000));
List<Labelable> tmp = LabelManager.instance().get(label);
for (Labelable labelable : tmp) {
bPanel.add(new JButton(labelable.getName()));
}

lPanel.add(bPanel, BorderLayout.NORTH);
filePanel.add(bPanel, label.getName());

b.setActionCommand(label.getName());
labelPanel.add(b);
}

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
labelPanelScroll, filePanelScroll);

.