This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoFillComboBox.java
108 lines (95 loc) · 3.85 KB
/
AutoFillComboBox.java
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
package com.biblioteca2.fxcontrols;
import com.sun.javafx.scene.control.skin.ComboBoxListViewSkin;
import java.util.Optional;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
/**
* Extension of JavaFX combobox with autofill property, based on JulianG approach with some enhacements.
* @author Gustavo Fragoso
*/
public class AutoFillComboBox extends ComboBox {
private ObservableList<String> items;
private final TextField editor;
public AutoFillComboBox() {
editor = getEditor();
}
public AutoFillComboBox(ObservableList<String> items){
super(items);
editor = getEditor();
setAutoFill(true);
setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
hide();
}
});
setOnKeyReleased(createHandler());
}
public final void setAutoFill(boolean b){
if(b){
setEditable(true);
items = getItems();
}else{
setEditable(false);
}
}
private EventHandler createHandler(){
EventHandler e = new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
// Avoid excessive functions call
KeyCode code = event.getCode();
String text = editor.getText();
int length = text.length();
if(code == KeyCode.UP) {
editor.positionCaret(length);
return;
} else if(code == KeyCode.DOWN) {
if(!isShowing()) {
show();
}
editor.positionCaret(length);
return;
} else if(code == KeyCode.BACK_SPACE) {
editor.positionCaret(length-1);
getSelectionModel().clearSelection();
} else if(code == KeyCode.DELETE) {
editor.positionCaret(length-1);
getSelectionModel().clearSelection();
}
if (code == KeyCode.RIGHT || code == KeyCode.LEFT
|| event.isControlDown() || code == KeyCode.HOME
|| code == KeyCode.END || code == KeyCode.TAB) {
return;
}
String lower = text.toLowerCase();
// Search
Optional<String> string = items.stream().filter(p -> p.toLowerCase().startsWith(lower)).findFirst();
// If find someone
if(string.isPresent()){
int pos = items.indexOf(string.get());
// Avoiding bug: Caret at first letter doesn't move right.
if(code == KeyCode.ENTER){
ListView lv = ((ComboBoxListViewSkin) getSkin()).getListView();
lv.scrollTo(pos);
lv.getSelectionModel().clearAndSelect(pos);
editor.positionCaret(string.get().length());
}else{
show();
ListView lv = ((ComboBoxListViewSkin) getSkin()).getListView();
lv.scrollTo(pos);
lv.getSelectionModel().clearAndSelect(pos);
editor.setText(text);
editor.positionCaret(lower.length());
}
}
}
};
return e;
}
}