Re: Noddy layout question!
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 27 Sep 2006 08:55:56 -0700
laura p wrote:
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);
Layouts with JScrollPanes are tricky. You need to put the scroll pane in a component that makes it the size that you want to see it or set it's preferred size and use a layout manager that respects this. The component in the scroll pane needs a layout that you can pack into the shape you need.
In this test program, change the layout manager of the JFrame to BorderLayout and you will get a good idea of what I mean. I used a GridLayout for the JPanel that lives in the scroll pane.
FlowLayout is really for putting components in one long row. It will move them to the next row if there is a horizontal constraint but you can only put constraints on the size of components in two dimensions. Hence your problem with the 500 x 10000 JPanel. I would use another layout that is really two dimensional, for example GridLayout or GridBagLayout.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JPanel {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel p = new JPanel();
p.setLayout(new GridLayout(0,10));
for (int i=0; i<4000; i++)
p.add(new JButton(Integer.toString(i)));
p.setBackground(Color.BLUE);
JScrollPane sp = new JScrollPane(p);
sp.setPreferredSize(new Dimension(400,300));
f.add(sp,c);
f.setSize(800,600);
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
--
Knute Johnson
email s/nospam/knute/
.
- References:
- Noddy layout question!
- From: laura p
- Noddy layout question!
- Prev by Date: Re: Can JOptionPane use a MaskFormatter for data input
- Next by Date: Re: Can JOptionPane use a MaskFormatter for data input
- Previous by thread: Noddy layout question!
- Next by thread: JTree display update issue with new nodes
- Index(es):