forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComboBox.java
More file actions
135 lines (116 loc) · 3.33 KB
/
Copy pathComboBox.java
File metadata and controls
135 lines (116 loc) · 3.33 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
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
package javaforce.ui;
/** Combo Box - combines TextField / Button and a ListBox (in a ScrollBox)
*
* @author pquiring
*/
import javaforce.*;
public class ComboBox extends Container {
private TextField text;
private Button button;
private ListBox list;
private ScrollBox scroll;
private ChangeListener change;
private int rows = 3; //max visible rows in ListBox
public ComboBox(String txt) {
text = new TextField(txt);
super.add(text);
button = new Button(Icons.getArrowDown());
button.setFocusable(false);
super.add(button);
list = new ListBox();
list.setLayer(1);
scroll = new ScrollBox(list, Direction.VERTICAL);
scroll.setLayer(1);
scroll.setVisible(false);
super.add(scroll);
button.setActionListner(new ActionListener() {
public void actionPerformed(Component cmp) {
list.setSelectedIndex(-1);
scroll.setVisible(!scroll.isVisible());
scroll.resetOffset();
}
});
list.setChangeListener(new ChangeListener() {
public void changed(Component cmp) {
String txt = list.getSelectedItem();
if (txt != null) {
text.setText(txt);
text.setCursorPosition(0, 0);
if (change != null) {
change.changed(ComboBox.this);
}
}
scroll.setVisible(false);
}
});
}
public Dimension getMinSize() {
return new Dimension(getWidth(), text.getMinHeight());
}
public void layout(LayoutMetrics metrics) {
int org_width = metrics.size.width;
int org_height = metrics.size.height;
int org_x = metrics.pos.x;
int org_y = metrics.pos.y;
setPosition(metrics.pos);
metrics.size.height = getHeight() + rows * list.getItemHeight();
setSize(metrics.size);
metrics.size.height = getMinHeight();
metrics.size.width -= 16;
text.layout(metrics);
metrics.pos.x += metrics.size.width;
metrics.size.width = 16;
button.layout(metrics);
metrics.pos.x = org_x;
metrics.pos.y += metrics.size.height;
metrics.size.width = org_width;
metrics.size.height = rows * list.getItemHeight();
scroll.layout(metrics);
metrics.pos.y = org_y;
metrics.size.height = org_height;
}
/** ComboBox is not a real Container, do not add other Components. */
public void add(Component child) {}
public void addItem(String item) {
list.addItem(item);
}
public void removeItem(String item) {
list.removeItem(item);
}
public String getText() {
return text.getText();
}
public void setText(String txt) {
text.setText(txt);
}
public boolean isEditable() {
return text.isEditable();
}
public void setEditable(boolean state) {
text.setEditable(state);
}
/** Sets number of items visible in drop down list. */
public void setItemsVisible(int items) {
rows = items;
}
public void setForeColor(Color clr) {
super.setForeColor(clr);
if (text == null) return;
text.setForeColor(clr);
list.setForeColor(clr);
button.setForeColor(clr);
}
public void setBackColor(Color clr) {
super.setBackColor(clr);
if (text == null) return;
text.setBackColor(clr);
list.setBackColor(clr);
button.setBackColor(clr);
}
public void setChangeListener(ChangeListener listener) {
change = listener;
}
public String toString() {
return "ComboBox:" + getText();
}
}