1

Yes, there are earlier threads and guides on the issue. And they tell me that either setValue(null) or getSelectionModel().clearSelection() should be the answer. But doing any of these gives me a java.lang.IndexOutOfBoundsException.

What I want to do is to clear the selection everytime something is being written into the combo box. This is because it causes problems and looks weird when you write something in the combo box and something else remains selected in the combo box popup.

Here's an 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();
    }
}
braX
  • 11,506
  • 5
  • 20
  • 33
Jonatan Stenbacka
  • 1,824
  • 2
  • 23
  • 48

1 Answers1

1

You are running into this JavaFX ComboBox change value causes IndexOutOfBoundsException issue, which is causing the IndexOutOfBoundsException. These are kind of a pain.

There is a bit of a logical issue with your attempts anyway: clearing the selected value will cause the editor to update its text, so even if this worked it would make it impossible for the user to type. So you want to check that the changed value isn't the one typed in. This seems to fix both issues:

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

You may need to change the toString() call, depending on the exact converter you are using. In this case, it will work.

Community
  • 1
  • 1
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Just copying your code into my SSCCE gives me a "`java.lang.IllegalArgumentException`: The start must be <= the end" exception if I first chose an option from the popup and then tries to add a number to it in the textfield. – Jonatan Stenbacka Sep 16 '15 at 14:08
  • The following approach seems to handle the issue: `if ((cb.getValue() != null && newValue.trim().length() != 0) && ! cb.getValue().toString().equals(newValue)) { cb.setValue(Integer.parseInt(newValue)); }` When you do `setValue(..)` the selection is set to that value. This approach also adds the pleasent effect of the popup alternative matching what has been written in the textfield being selected. – Jonatan Stenbacka Sep 16 '15 at 14:20
  • The second boolean expression in my solutions is unnecessary: The boolean expression parameter in the if statement should be as you suggested: The code inside the if-statement of your solution is still incorrect though, and my solution in the comment above (with your boolean parameter) remains the best solution so far. – Jonatan Stenbacka Sep 17 '15 at 10:56