是的,有关于这个问题的早期线程和指南。他们告诉我,要么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();
}
}