/Users/lyon/j4p/src/gui/dialogs/LabelledItemPanel.java
|
1 package gui.dialogs;
2
3 import javax.swing.*;
4 import java.awt.*;
5
6 /**
7 * This class provides a panel for laying out labelled elements neatly with
8 * all the labels and elements aligned down the screen.
9 *
10 * @author David Fraser
11 * @author Michael Harris
12 */
13 public class LabelledItemPanel extends JPanel {
14 /**
15 * The row to add the next labelled item to
16 */
17 private int myNextItemRow = 0;
18
19 /**
20 * This method is the default constructor.
21 */
22 public LabelledItemPanel() {
23 init();
24 }
25
26 /**
27 * This method initialises the panel and layout manager.
28 */
29 private void init() {
30 setLayout(new GridBagLayout());
31
32 // Create a blank label to use as a vertical fill so that the
33 // label/item pairs are aligned to the top of the panel and are not
34 // grouped in the centre if the parent component is taller than
35 // the preferred size of the panel.
36
37 GridBagConstraints constraints = new GridBagConstraints();
38 constraints.gridx = 0;
39 constraints.gridy = 99;
40 constraints.insets = new Insets(10, 0, 0, 0);
41 constraints.weighty = 1.0;
42 constraints.fill = GridBagConstraints.VERTICAL;
43
44 JLabel verticalFillLabel = new JLabel();
45
46 add(verticalFillLabel, constraints);
47 }
48
49 /**
50 * This method adds a labelled jComponent to the panel. The jComponent is added to
51 * the row below the last jComponent added.
52 *
53 * @param labelText The label text for the jComponent.
54 * @param jComponent The jComponent to be added.
55 */
56
57
58 public void addItem(String labelText,
59 JComponent jComponent) {
60 // Create the label and its constraints
61
62 JLabel label = new JLabel(labelText);
63
64 GridBagConstraints labelConstraints = new GridBagConstraints();
65
66 labelConstraints.gridx = 0;
67 labelConstraints.gridy = myNextItemRow;
68 labelConstraints.insets =
69 new Insets(10, 10, 0, 0);
70 labelConstraints.anchor =
71 GridBagConstraints.NORTHEAST;
72 labelConstraints.fill =
73 GridBagConstraints.NONE;
74
75 add(label, labelConstraints);
76
77 // Add the component with its constraints
78
79 GridBagConstraints itemConstraints = new GridBagConstraints();
80
81 itemConstraints.gridx = 1;
82 itemConstraints.gridy = myNextItemRow;
83 itemConstraints.insets =
84 new Insets(10, 10, 0, 10);
85 itemConstraints.weightx = 1.0;
86 itemConstraints.anchor =
87 GridBagConstraints.WEST;
88 itemConstraints.fill =
89 GridBagConstraints.HORIZONTAL;
90
91 add(jComponent, itemConstraints);
92
93 myNextItemRow++;
94 }
95 public void addItem(String labelText,
96 JComponent jComponent, int rows) {
97 // Create the label and its constraints
98
99 JLabel label = new JLabel(labelText);
100
101 GridBagConstraints labelConstraints = new GridBagConstraints();
102
103 labelConstraints.gridx = 0;
104 labelConstraints.gridy = myNextItemRow;
105 labelConstraints.insets =
106 new Insets(10, 10, 0, 0);
107 labelConstraints.anchor =
108 GridBagConstraints.NORTHEAST;
109 labelConstraints.fill =
110 GridBagConstraints.NONE;
111
112 add(label, labelConstraints);
113
114 // Add the component with its constraints
115
116 GridBagConstraints itemConstraints = new GridBagConstraints();
117
118 itemConstraints.gridx = 1;
119 itemConstraints.gridy = myNextItemRow;
120 itemConstraints.gridheight = rows;
121 itemConstraints.gridwidth = rows;
122 itemConstraints.insets =
123 new Insets(10, 10, 0, 10);
124 itemConstraints.weightx = 1.0;
125 itemConstraints.anchor =
126 GridBagConstraints.WEST;
127 itemConstraints.fill =
128 GridBagConstraints.BOTH;
129
130 add(jComponent, itemConstraints);
131
132 myNextItemRow=rows + myNextItemRow;
133 }
134 }