はい、この問題に関する以前のスレッドとガイドがあります。そして、彼らは私に、またはどちら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();
}
}