-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathEnumCombo.java
More file actions
50 lines (46 loc) · 1.16 KB
/
Copy pathEnumCombo.java
File metadata and controls
50 lines (46 loc) · 1.16 KB
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
package dateFormat;
import java.util.*;
import javax.swing.*;
/**
A combo box that lets users choose from among static field
values whose names are given in the constructor.
@version 1.14 2012-01-26
@author Cay Horstmann
*/
public class EnumCombo extends JComboBox<String>
{
private Map<String, Integer> table = new TreeMap<>();
/**
Constructs an EnumCombo.
@param cl a class
@param labels an array of static field names of cl
*/
public EnumCombo(Class<?> cl, String... labels)
{
for (String label : labels)
{
String name = label.toUpperCase().replace(' ', '_');
int value = 0;
try
{
java.lang.reflect.Field f = cl.getField(name);
value = f.getInt(cl);
}
catch (Exception e)
{
label = "(" + label + ")";
}
table.put(label, value);
addItem(label);
}
setSelectedItem(labels[0]);
}
/**
Returns the value of the field that the user selected.
@return the static field value
*/
public int getValue()
{
return table.get(getSelectedItem());
}
}