2

I am facing a weird problem. I have an editable ComboBox with some items. After running my code if I type something into that ComboBox and call getValue() function then it gives me null value.

Here is my code (thenewboston): package application;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    Stage window;
    Scene scene;
    Button button;
    ComboBox<String> comboBox;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle("ComboBox Demo");
        button = new Button("Submit");

        comboBox = new ComboBox<>();
        comboBox.getItems().addAll(
                "Good Will Hunting",
                "St. Vincent",
                "Blackhat"
        );

        comboBox.setPromptText("What is your favorite movie?");
        comboBox.setEditable(true);
        button.setOnAction(e -> printMovie());

        //ComboBoxes also generate actions if you need to get value instantly
        comboBox.setOnAction( e -> System.out.println("User selected " + comboBox.getValue()) );

        VBox layout = new VBox(10);
        layout.setPadding(new Insets(20, 20, 20, 20));
        layout.getChildren().addAll(comboBox, button);

        scene = new Scene(layout, 300, 250);
        window.setScene(scene);
        window.show();
    }
    private void printMovie(){
        System.out.println(comboBox.getValue());
    }
}

I am using Windows 8.1, Eclipse Mars (4.5) and Java 1.8.0_66

Shihab
  • 2,641
  • 3
  • 21
  • 29
  • assuming you get the null when clicking the submit button, I closed the question as [http://stackoverflow.com/q/32620739/203657](duplicate) - please edit if my assumption is incorrect – kleopatra Jan 29 '16 at 11:13

1 Answers1

9

According to the documentation for getValue():

The value of this ComboBox is defined as the selected item if the input is not editable, or if it is editable, the most recent user action: either the value input by the user, or the last selected item.

The "value input by the user" will not be returned by getValue() until the user has accepted their input text with the action key which is normally the Enter key on your keyboard.

As such, getValue() will return the following in these scenarios and behaves exactly as expected with this detail in mind:

  • Text, "text", is typed but not accepted with the action key: null

  • Text, "text", is typed into the combobox and is accepted with the action key: text

  • Drop-down combobox item, "Good Will Hunting", is selected: Good Will Hunting

Once the user has accepted the text typed with the action key, the correct value will be returned instead of null. If you would like to get the actual text typed into the combobox though, you may consider retrieving the combobox editor's current value instead:

comboBox.getEditor().getText();
David Yee
  • 3,515
  • 25
  • 45
  • Thank you fro your answer. Now it is working perfectly. One thing I wanna know is, is there any problem if every time I use 'getEditor().getText()' insted of 'getValue()' ? @david-yee – Shihab Jan 26 '16 at 21:27
  • There shouldn't be any problems with relying on the editor's text value except for the subtle difference in behaviour which affects the way your users will interact with your application. – David Yee Jan 26 '16 at 21:35
  • the "user action" mentioned in the doc must include transfering focus out off the combo (f.i. by clicking on the submit button), not doing so is a bug, fixed as of jdk9, and backported to 8u72 – kleopatra Jan 29 '16 at 11:09