Javafx 如何删除可编辑组合框中输入的选择

Javafx 如何删除可编辑组合框中输入的选择,javafx,combobox,selection,deselect,Javafx,Combobox,Selection,Deselect,是的,在这个问题上有一些更早和更重要的问题。他们告诉我答案应该是setValue(null)或getSelectionModel().clearSelection()。但是做这些操作都会给我一个java.lang.IndexOutOfBoundsException 我想做的是在每次有东西写入组合框时清除选择。这是因为,当您在组合框中写入某些内容,而在组合框弹出窗口中其他内容仍处于选中状态时,它会导致问题,并且看起来很奇怪 这是一个SSCCE: import javafx.application.

是的,在这个问题上有一些更早和更重要的问题。他们告诉我答案应该是
setValue(null)
getSelectionModel().clearSelection()
。但是做这些操作都会给我一个
java.lang.IndexOutOfBoundsException

我想做的是在每次有东西写入组合框时清除选择。这是因为,当您在组合框中写入某些内容,而在组合框弹出窗口中其他内容仍处于选中状态时,它会导致问题,并且看起来很奇怪

这是一个SSCCE:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;

import javafx.stage.Stage;
import javafx.util.converter.IntegerStringConverter;

public class SSCCE extends Application {

    @Override
    public void start(Stage stage) {

        HBox root = new HBox();

        ComboBox<Integer> cb = new ComboBox<Integer>();
        cb.setEditable(true);
        cb.getItems().addAll(1, 2, 6, 7, 9);
        cb.setConverter(new IntegerStringConverter());

        cb.getEditor().textProperty()
                .addListener((obs, oldValue, newValue) -> {
                    // Using any of these will give me a IndexOutOfBoundsException
                    // Using any of these will give me a IndexOutOfBoundsException
                    //cb.setValue(null);
                    //cb.getSelectionModel().clearSelection();
                    });

        root.getChildren().addAll(cb);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.ComboBox;
导入javafx.scene.layout.HBox;
导入javafx.stage.stage;
导入javafx.util.converter.IntegerStringConverter;
公共类SSCCE扩展应用程序{
@凌驾
公众假期开始(阶段){
HBox根=新的HBox();
ComboBox cb=新ComboBox();
cb.setEditable(true);
cb.getItems().addAll(1,2,6,7,9);
setConverter(新的IntegerStringConverter());
cb.getEditor().textProperty()
.addListener((obs、旧值、新值)->{
//使用其中任何一个都会给我一个IndexOutOfBoundsException
//使用其中任何一个都会给我一个IndexOutOfBoundsException
//cb.setValue(空);
//cb.getSelectionModel().clearSelection();
});
root.getChildren().addAll(cb);
场景=新场景(根);
舞台场景;
stage.show();
}
公共静态void main(字符串[]args){
发射();
}
}
您遇到了此问题,它导致了
索引自动边界异常
。这是一种痛苦

无论如何,您的尝试都有一个逻辑问题:清除所选值将导致编辑器更新其文本,因此即使这样做有效,用户也无法键入。因此,您需要检查更改的值是否不是键入的值。这似乎解决了两个问题:

    cb.getEditor().textProperty()
            .addListener((obs, oldValue, newValue) -> {
                if (cb.getValue() != null && ! cb.getValue().toString().equals(newValue)) {
                    cb.getSelectionModel().clearSelection();
                }
            });

您可能需要更改
toString()
调用,具体取决于您使用的转换器。在这种情况下,它会起作用。

只要将代码复制到我的SSCCE中,我就会得到一个“
java.lang.IllegalArgumentException
:开始时必须使用以下方法来处理问题:
如果((cb.getValue()!=null&&newValue.trim().length()!=0)和&!cb.getValue().toString().equals(newValue)){cb.setValue(Integer.parseInt(newValue));}
执行设置值(…)选择设置为该值。这种方法还增加了弹出选项的愉悦效果,与所选文本字段中写入的内容相匹配。我的解决方案中的第二个布尔表达式是不必要的:if语句中的布尔表达式参数应如您所建议的:if语句中的代码但是,您的解决方案的nt仍然不正确,并且我在上面的注释中的解决方案(使用您的布尔参数)仍然是迄今为止最好的解决方案。