LabelFieldLayout
LabelFieldLayout is a wrapper around GridBagLayout that simplifies things for the common case of two columns, one of labels and one of fields.
An example:
JPanel panel = new JPanel(); LabelFieldLayout l = new LabelFieldLayout(); panel.setLayout(l); panel.add(new JLabel("Label label:"), l.labelLabel()); panel.add(new JLabel("Label field"), l.labelField()); l.nextLine(); panel.add(new JLabel("Normal label:"), l.normalLabel()); panel.add(new JTextField("Normal field"), l.normalField()); l.nextLine(); panel.add(Box.createVerticalStrut(12), l.spacing()); l.nextLine(); panel.add(new JLabel("Wide label"), l.wideLabel()); l.nextLine(); panel.add(Box.createVerticalStrut(12), l.spacing()); l.nextLine(); panel.add(new JLabel("Final wide field:"), l.wideLabel()); l.nextLine(); JList list = new JList(new Object[] { "Final wide field A", "Final wide field B", "Final wide field C" }); panel.add(new JScrollPane(list), l.finalWideField());
The final component in a LabelFieldLayout stretches vertically to cover space left at the bottom of its container. It can be either an empty component, such as Box.createGlue(), or a wide field, such as a JList in a JScrollPane:
See LabelFieldLayoutExample.java for a complete example.
LabelFieldPanel
LabelFieldPanel is a JPanel whose layout manager is an instance of LabelFieldLayout. It contains helper addLabel and addField methods to reduce the lines of code necessary to lay out components. The example code above can be reduced to the following with LabelFieldPanel:
LabelFieldPanel panel = new LabelFieldPanel(); panel.addField("Label label:", "Label field"); panel.addField("Normal label:", new JTextField("Normal field")); panel.addSpacing(12); panel.addLabel("Wide label"); panel.addSpacing(12); panel.addLabel("Final field:"); JList list = new JList(new Object[] { "Final field A", "Final field B", "Final field C" }); panel.addFinalField(new JScrollPane(list));
See LabelFieldPanelExample.java for a complete example.
ButtonPanel
ButtonPanel is a JPanel whose layout manager is an instance of ButtonLayout. It overrides add(JButton) and add(Action) methods to provide appropriate spacing between adjacent buttons and between buttons and the sides of the container.
ButtonPanel buttonPanel = new ButtonPanel(); buttonPanel.add(new JButton("Up")); buttonPanel.add(new JButton("Down")); buttonPanel.add(new AbstractAction("Left") { ... }); buttonPanel.add(new AbstractAction("Right") { ... });
See ButtonPanelExample.java for a complete example.
Acknowledgements
- Powered by Apache Maven.
- Project hosting by SourceForge.net.