Reputation: 4209
One of my developers has attempted to extend ComboBox to autofilter based on what the user types:
public class AutoCompleteComboBox<T> extends ComboBox<T> {
private FilteredList<T> filteredItems;
private SortedList<T> sortedItems;
public AutoCompleteComboBox() {
setEditable(true);
setOnKeyReleased(e -> handleOnKeyReleasedEvent(e));
setOnMouseClicked(e -> handleOnMouseClicked(e));
}
private void handleOnMouseClicked(MouseEvent e) {
getItems().stream()
.filter(item -> item.toString().equals(getEditor().getText()))
.forEach(item -> getSelectionModel().select(item));
setCaretPositionToEnd();
}
private void handleOnKeyReleasedEvent(KeyEvent e) {
if (e.getCode() == KeyCode.UP || e.getCode() == KeyCode.DOWN) {
getItems().stream()
.filter(item -> item.toString().equals(getEditor().getText()))
.forEach(item -> getSelectionModel().select(item));
show();
setCaretPositionToEnd();
} else if (e.getCode() == KeyCode.ENTER || e.getCode() == KeyCode.TAB) {
String editorStr = getEditor().getText();
getSelectionModel().clearSelection();
getEditor().setText(editorStr);
setItems(this.sortedItems);
getItems().stream()
.filter(item -> item.toString().equals(editorStr))
.forEach(item -> getSelectionModel().select(item));
getEditor().selectEnd();
if (e.getCode() == KeyCode.ENTER) {
getEditor().deselect();
}
hide();
} else if (e.getText().length() == 1) {
getSelectionModel().clearSelection();
if (getEditor().getText().length() == 0) {
getEditor().setText(e.getText());
}
filterSelectionList();
show();
} else if (e.getCode() == KeyCode.BACK_SPACE && getEditor().getText().length() > 0) {
String editorStr = getEditor().getText();
getSelectionModel().clearSelection();
getEditor().setText(editorStr);
int beforeFilter = getItems().size();
filterSelectionList();
int afterFilter = getItems().size();
if (afterFilter > beforeFilter) {
hide();
}
show();
} else if (e.getCode() == KeyCode.BACK_SPACE && getEditor().getText().length() == 0) {
clearSelection();
hide();
show();
}
}
private void filterSelectionList() {
setFilteredItems();
setCaretPositionToEnd();
}
private void setFilteredItems() {
filteredItems.setPredicate(item ->
item.toString().toLowerCase().startsWith(getEditor().getText().toLowerCase()));
}
private void setCaretPositionToEnd() {
getEditor().selectEnd();
getEditor().deselect();
}
public void setInitItems(ObservableList<T> values) {
filteredItems = new FilteredList<>(values);
sortedItems = new SortedList<>(filteredItems);
setItems(this.sortedItems);
}
public void clearSelection() {
getSelectionModel().clearSelection();
getEditor().clear();
if (this.filteredItems != null) {
this.filteredItems.setPredicate(item -> true);
}
}
public T getSelectedItem() {
T selectedItem = null;
if (getSelectionModel().getSelectedIndex() > -1) {
selectedItem = getItems().get(getSelectionModel().getSelectedIndex());
}
return selectedItem;
}
public void select(String value) {
if (!value.isEmpty()) {
getItems().stream()
.filter(item -> value.equals(item.toString()))
.findFirst()
.ifPresent(item -> getSelectionModel().select(item));
}
}
}
The control works just fine, until I bind something to either the Selected or Value property. Once that happens, once I attempt to leave the field (or hit enter which causes the action to occur), I get the following exception. (Simply selecting the value with the mouse selects without issues) What I am binding to in this control is the simple object of CodeTableValue - which has two properties - both Strings, Code & Value. The toString returns the Value property.
If the control is just set up without any listeners, it works just fine. But as soon as I listen to one of the properties for the value it fails.
When I use the AutoCompleteComboBox
control in my class:
@FXML private AutoCompleteComboBox<CodeTableValue> myAutoCompleteBox;
myAutoCompleteBox.getSelectionModel().selectedItemProperty().addListener((obs, ov, nv) -> {
System.out.println(nv);
});
The exception that is thrown:
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.String cannot be cast to cache.CodeTableValue
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102)
at javafx.scene.control.ComboBox.lambda$new$152(ComboBox.java:249)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:150)
at com.sun.javafx.scene.control.skin.ComboBoxPopupControl.setTextFromTextFieldIntoComboBoxValue(ComboBoxPopupControl.java:405)
at com.sun.javafx.scene.control.skin.ComboBoxPopupControl.lambda$new$291(ComboBoxPopupControl.java:82)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:72)
at javafx.scene.Node$FocusedProperty.notifyListeners(Node.java:7718)
at javafx.scene.Node.setFocused(Node.java:7771)
at javafx.scene.Scene$KeyHandler.setWindowFocused(Scene.java:3932)
at javafx.scene.Scene$KeyHandler.lambda$new$11(Scene.java:3954)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:72)
at javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
at javafx.stage.Window.setFocused(Window.java:439)
at com.sun.javafx.stage.WindowPeerListener.changedFocused(WindowPeerListener.java:59)
at com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:100)
at com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:40)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassWindowEventHandler.lambda$handleWindowEvent$423(GlassWindowEventHandler.java:150)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassWindowEventHandler.handleWindowEvent(GlassWindowEventHandler.java:148)
at com.sun.glass.ui.Window.handleWindowEvent(Window.java:1266)
at com.sun.glass.ui.Window.notifyFocus(Window.java:1245)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
If the editableFlag is false, then this works. It just happens when we allow the user to type into the combo box to filter the content. Without the binding, we get no exceptions and the values are correctly set.
I have been digging deeper into this exception and it is being thrown here:
com.sun.javafx.binding.ExpressionHelper.Generic.fireValueChangedEvent()
if (curChangeSize > 0) {
final T oldValue = currentValue;
currentValue = observable.getValue();
final boolean changed = (currentValue == null)? (oldValue != null) : !currentValue.equals(oldValue);
if (changed) {
for (int i = 0; i < curChangeSize; i++) {
try {
curChangeList[i].changed(observable, oldValue, currentValue);
} catch (Exception e) {
Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
}
}
}
}
It comes from here only when the valueProperty or selectedItemProperty is bound. Otherwise we don't get this issue.
javafx.beans.property.ObjectPropertyBase.set(T) @Override
public void set(T newValue) {
if (isBound()) {
throw new java.lang.RuntimeException((getBean() != null && getName() != null ?
getBean().getClass().getSimpleName() + "." + getName() + " : ": "") + "A bound value cannot be set.");
}
if (value != newValue) {
value = newValue;
markInvalid();
}
}
I have attempted to add a StringConverter to the editor to see if that would correct the issue, but again, it continues to throw this exception. Perhaps we are attempting to handle this all wrong?
Basically we are looking to filter the combobox selection items as the user types into the field. If there is different way we should handle this please let me know, but at the moment, I think that this could be a bug in the JDK? If the field is not bound then we have no issues, but once bound, this exception occurs when the field either loses focus, or the enter key is pressed.
Upvotes: 1
Views: 1000
Reputation: 45486
I've created a quick sample using your control:
@Override
public void start(Stage primaryStage) {
AutoCompleteComboBox<CodeTableValue> myAutoCompleteBox =
new AutoCompleteComboBox<>();
myAutoCompleteBox.setInitItems(FXCollections.observableArrayList(new CodeTableValue("One", "1"),
new CodeTableValue("Two", "2"), new CodeTableValue("Three", "4")));
StackPane root = new StackPane(myAutoCompleteBox);
myAutoCompleteBox.getSelectionModel().selectedItemProperty().addListener((obs, ov, nv) -> {
System.out.println(nv);
});
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
Based on a simple model class:
public class CodeTableValue {
private String code;
private String value;
public CodeTableValue(String code, String value) {
this.code = code;
this.value = value;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "code=" + code + ", value=" + value;
}
}
I can reproduce your exception once I finish typing and click enter to commit the value and leave the control:
java.lang.ClassCastException: java.lang.String cannot be cast to CodeTableValue
The exception comes from the fact that the TextField
used from the ComboBox in edit mode will return always a String
, but you are forcing the custom ComboBox to use a CodeTableValue
class.
The solution is just providing a way to convert between both String and CodeTableValue
, using a StringConverter
.
So I've modified your control:
public AutoCompleteComboBox(StringConverter<T> converter) {
setEditable(true);
setOnKeyReleased(e -> handleOnKeyReleasedEvent(e));
setOnMouseClicked(e -> handleOnMouseClicked(e));
super.setConverter(converter);
}
and now in the sample:
AutoCompleteComboBox<CodeTableValue> myAutoCompleteBox =
new AutoCompleteComboBox<>(new StringConverter<CodeTableValue>() {
@Override
public String toString(CodeTableValue object) {
if (object != null) {
return object.getValue();
}
return null;
}
@Override
public CodeTableValue fromString(String string) {
return new CodeTableValue(string, string);
}
});
This works now, without the ClassCastException
.
Obviously you'll have to provide a way to type a string (either for code
or for value
) and retrieve the other out of it.
Upvotes: 1