-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemCboxAutoCompleted.java
More file actions
61 lines (46 loc) · 1.28 KB
/
Copy pathItemCboxAutoCompleted.java
File metadata and controls
61 lines (46 loc) · 1.28 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
package Utils;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.util.List;
public class ItemCboxAutoCompleted<T> {
private boolean isUpdating = false;
public boolean isUpdating() {
return isUpdating;
}
public void ativarText(JComboBox<T> combobox, ComboBoxFilter<T> filtro) {
combobox.setEditable(true);
JTextField editar = (JTextField) combobox.getEditor().getEditorComponent();
editar.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
filtrar();
}
public void removeUpdate(DocumentEvent e) {
filtrar();
}
public void changedUpdate(DocumentEvent e) {
filtrar();
}
private void filtrar() {
if (isUpdating) {
return;
}
SwingUtilities.invokeLater(() -> {
String txt = editar.getText();
if (txt.length() >= 2) {
isUpdating = true;
List<T> results = filtro.filtrar(txt);
combobox.removeAllItems();
for (T itm : results) {
combobox.addItem(itm);
}
combobox.setPopupVisible(true);
editar.setText(txt);
editar.setCaretPosition(txt.length());
isUpdating = false;
}
});
}
});
}
}