gpt4 book ai didi

java - 有没有人成功地为列表单元格自动完成组合框?

转载 作者:行者123 更新时间:2023-11-29 08:47:24 27 4
gpt4 key购买 nike

作为独立元素,this解决方案工作正常。

但在自定义 ListCell 中(从 ComboBoxListCell 来源镜像),它有时会给出:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.control.skin.ComboBoxPopupControl.positionAndShowPopup(ComboBoxPopupControl.java:100)
at com.sun.javafx.scene.control.skin.ComboBoxPopupControl.show(ComboBoxPopupControl.java:74)
at com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.handleControlPropertyChanged(ComboBoxBaseSkin.java:115)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.handleControlPropertyChanged(ComboBoxListViewSkin.java:245)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$2.call(BehaviorSkinBase.java:189)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$2.call(BehaviorSkinBase.java:187)
at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:176)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.ReadOnlyBooleanWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:178)
at javafx.beans.property.ReadOnlyBooleanWrapper$ReadOnlyPropertyImpl.access$100(ReadOnlyBooleanWrapper.java:148)
at javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:144)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.scene.control.ComboBoxBase.setShowing(ComboBoxBase.java:202)
at javafx.scene.control.ComboBoxBase.show(ComboBoxBase.java:407)

似乎 ComboBox 的 show() 方法可以在 JavaFX 代码中产生 NPE。

自定义 ComboBoxListCell 仅在 startEdit() 方面不同:

@Override public void startEdit() {
if (! isEditable() || ! getListView().isEditable()) {
return;
}

if (comboBox == null) {
comboBox = createComboBox(this, items, converterProperty());

//The only special string
handler = new AutoCompleteComboBoxListener<>(comboBox);

comboBox.editableProperty().bind(comboBoxEditableProperty());
}

comboBox.getSelectionModel().select(getItem());

super.startEdit();

if (isEditing()) {
setText(null);
setGraphic(comboBox);
}
}

给出NPE的AutoCompleteComboBoxListener的代码:

@Override
public void handle(KeyEvent event) {

if(event.getCode() == KeyCode.UP) {
caretPos = -1;
moveCaret(comboBox.getEditor().getText().length());
return;
} else if(event.getCode() == KeyCode.DOWN) {
if(!comboBox.isShowing()) {
comboBox.show();
}
caretPos = -1;
moveCaret(comboBox.getEditor().getText().length());
return;
} else if(event.getCode() == KeyCode.BACK_SPACE) {
moveCaretToPos = true;
caretPos = comboBox.getEditor().getCaretPosition();
} else if(event.getCode() == KeyCode.DELETE) {
moveCaretToPos = true;
caretPos = comboBox.getEditor().getCaretPosition();
}

if (event.getCode() == KeyCode.RIGHT || event.getCode() == KeyCode.LEFT
|| event.isControlDown() || event.getCode() == KeyCode.HOME
|| event.getCode() == KeyCode.END || event.getCode() == KeyCode.TAB) {
return;
}

ObservableList list = FXCollections.observableArrayList();
for (int i=0; i<data.size(); i++) {
if(data.get(i).toString().toLowerCase().startsWith(
AutoCompleteComboBoxListener.this.comboBox
.getEditor().getText().toLowerCase())) {
list.add(data.get(i));
}
}
String t = comboBox.getEditor().getText();

comboBox.setItems(list);
comboBox.getEditor().setText(t);
if(!moveCaretToPos) {
caretPos = -1;
}
moveCaret(t.length());
if(!list.isEmpty()) {

//This gives NPE inside JavaFX API
comboBox.show();
}
}

最佳答案

好的,问题是 setItems 将当前选择跳过到 -1,然后 ListCell 将该事实视为需要提交新值(空)(如果当前选择!= -1)。在该提交之后,comboBox 没有任何可显示的内容,因为它处于非编辑状态。

所以这是一个设计问题,而不是错误。如果不特别小心,ComboBoxListCell 代码无法即时更改项目。

关于java - 有没有人成功地为列表单元格自动完成组合框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24422202/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com