1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.ui.swing.components;
import java.awt.Component;
import java.util.List;
import java.util.Vector;
import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.border.EmptyBorder;
/**
* CheckButtonPanel is a simple extension of ButtonPanel. Differences are that
* it uses JCheckBoxes and the default alignment is vertical. The panel defaults
* to having no buttons selected.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 904 $
*/
public class CheckButtonPanel extends ButtonPanel {
/**
* Constructs a CheckButtonPanel. Three buttons are created so the panel is
* filled when used in a GUI-builder environment.
*/
public CheckButtonPanel() {
super();
}
/**
* Constructs a ButtonPanel using specified buttons.
*
* @param buttonList An array containing the strings to be used in labeling the
* buttons.
*/
public CheckButtonPanel(String[] buttonList) {
super(buttonList);
}
/**
* Overridden to set vertical-center alignment and zero vgap.
*/
protected void initLayout() {
super.initLayout();
buttonPanelLayout.setAlignment(BetterFlowLayout.CENTER_VERTICAL);
buttonPanelLayout.setVgap(0);
}
/**
* Overridden to return a JRadioButton.
*
* @param aLabel The label for the component that will be created.
* @return The newly created component.
*/
protected Component createComponentWithLabel(String aLabel) {
String buttonLabel = aLabel;
JCheckBox newButton = new JCheckBox();
newButton.setName(aLabel);
newButton.setText(buttonLabel);
newButton.setActionCommand(aLabel);
newButton.addActionListener(this);
// reduce insets per java l&f guidelines (was 4 on each side)
newButton.setBorder(new EmptyBorder(1, 4, 1, 4));
return newButton;
}
/**
* Sets the value of the button whose name matches the given text value.
*
* @param aName A String matching the name of one of the buttons. If null,
* empty, or not matching, nothing happens.
* @param aValue A value to set the button.
*/
public void setValue(String aName, boolean aValue) {
if (aName != null) {
Component c = null;
int count = buttonContainer.getComponentCount();
for (int i = 0; i < count; i++) {
c = buttonContainer.getComponent(i);
if (c instanceof AbstractButton) {
if (c.getName().equals(aName)) {
((AbstractButton) c).setSelected(aValue);
c.repaint();
return;
}
}
}
}
// null, empty, or not matching - exit.
System.out.println("CheckButtonPanel.setValue: not found: " + aName);
}
/**
* Sets the state of the specified buttons to the specified value.
*
* @param aLabelArray An Array of Strings listing the buttons to be set.
* @param aValue The value to which the specified buttons will be set.
*/
public void setValues(String[] aLabelArray, boolean aValue) {
if (aLabelArray != null) {
for (int i = 0; i < aLabelArray.length; i++) {
setValue(aLabelArray[i], aValue);
}
}
}
/**
* Convenience method to set all checkboxes on the panel.
*
* @param aValue The value to which all checkboxes on the panel will be set.
*/
public void setAllValues(boolean aValue) {
setValues(getLabels(), aValue);
}
/**
* Convenience method to check all boxes on the panel.
*/
public void checkAll() {
setAllValues(true);
}
/**
* Convenience method to clear all boxes on the panel.
*/
public void clearAll() {
setAllValues(false);
}
/**
* A convenience method to set only those buttons on the entire panel that
* should be checked. Buttons not in the list are unchecked.
*
* @param aLabelArray An Array of Strings listing the buttons to be set.
*/
public void setCheckedValues(String[] aLabelArray) {
setAllValues(false);
setValues(aLabelArray, true);
}
/**
* Gets the labels of all checkboxes that are checked.
*
* @return A List of Strings containing the labels of the boxes that are
* checked.
*/
public List getCheckedValueList() {
Vector v = new Vector();
Component c = null;
int count = buttonContainer.getComponentCount();
for (int i = 0; i < count; i++) {
c = buttonContainer.getComponent(i);
if (c instanceof AbstractButton) {
if (((AbstractButton) c).isSelected()) {
v.addElement(c.getName());
}
}
}
return v;
}
/**
* Gets the labels of all checkboxes that are checked.
*
* @return A String Array containing the labels of the boxes that are checked.
*/
public String[] getCheckedValues() {
List v = getCheckedValueList();
String[] result = new String[v.size()];
for (int i = 0; i < v.size(); i++) {
result[i] = (String) v.get(i);
}
return result;
}
/**
* Gets the value of the specified button.
*
* @param aName A String matching the name of one of the buttons.
* @return True if the button is checked, False if it is not checked. NOTE: If
* the button is not found in the list, False is returned.
*/
public boolean getValue(String aName) {
Component c = null;
int count = buttonContainer.getComponentCount();
for (int i = 0; i < count; i++) {
c = buttonContainer.getComponent(i);
if ((c instanceof AbstractButton) && (((AbstractButton) c).isSelected())) {
if (((AbstractButton) c).getText().equals(aName)) {
return ((AbstractButton) c).isSelected();
}
}
}
return false;
}
// for testing
public static void main(String[] argv) {
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
}
javax.swing.JFrame dialog = new javax.swing.JFrame();
java.awt.BorderLayout bl = new java.awt.BorderLayout(20, 20);
CheckButtonPanel panel = new CheckButtonPanel(new String[] { "One", "Two", "Three" });
dialog.getContentPane().setLayout(bl);
dialog.getContentPane().add(panel, java.awt.BorderLayout.CENTER);
dialog.setLocation(50, 50);
// dialog.setSize( 450, 150 );
panel.setAlignment(BetterFlowLayout.CENTER_VERTICAL);
panel.getButton("One").setEnabled(false);
panel.setValues(new String[] { "One" }, true);
panel.setValue("Three", true);
// panel.setCheckedValues( new String[] { "Two" } );
String[] values = panel.getCheckedValues();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
dialog.pack();
dialog.setVisible(true);
}
}
|