ADDITIONAL SYSTEM INFORMATION :
Windows 11 / JavaSE-21
A DESCRIPTION OF THE PROBLEM :
The selectedItemProperty documentation states:
"The selected item is either null, to represent that there is no selection, or an Object that is retrieved from the underlying data model of the control the selection model is associated with."
Now if the ComboBox is editable and a value is entered in the editor box that is not in the list, both the valueProperty and the selectedItemProperty contain the entered value and selectionIndex=-1.
If you open the drop-down list there is no selection anymore.
The documentation of the ComboBox states:
3. Clearing the selection in the selection model does not null the value property - it remains the same as before.
4. It is valid for the selection model to have a selection set to a given index even if there is no items in the list (or less items in the list than the given index). Once the items list is further populated, such that the list contains enough items to have an item in the given index, both the selection model SelectionModel.selectedItemProperty() and value property will be updated to have this value. This is inconsistent with other controls that use a selection model, but done intentionally for ComboBox.
In my understand this is either a bug or contradicting documentation.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the following program with installed JavaFX 22 or later.
Enter "X" into the editor box.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
selectedItem() should be null according to the documentation of this property.
ACTUAL -
selectedItem() contains the entered value just like the valueProperty.
---------- BEGIN SOURCE ----------
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import java.util.Locale;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Locale.setDefault(Locale.GERMAN);
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
FlowPane flowPane = new FlowPane();
ComboBox<String> combo = new ComboBox<>();
combo.setEditable(true);
combo.getItems().add("Line 1");
combo.getItems().add("Line 2");
combo.getItems().add("Line 3");
combo.valueProperty().subscribe(newValue->{
System.out.println("valueProperty=" +
(newValue==null ? "null" : newValue) +
", selectedItem=" +
(combo.getSelectionModel().getSelectedItem()==null ? "null" :
combo.getSelectionModel().getSelectedItem()) +
", selectedIndex=" + combo.getSelectionModel().getSelectedIndex()
);
if (combo.getSelectionModel().getSelectedIndex()<0)
combo.getSelectionModel().clearSelection();
});
combo.valueProperty().set("X");
flowPane.getChildren().add(combo);
root.getChildren().add(flowPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
To check if an entered value is in the list you need to use selectedIndex.
Windows 11 / JavaSE-21
A DESCRIPTION OF THE PROBLEM :
The selectedItemProperty documentation states:
"The selected item is either null, to represent that there is no selection, or an Object that is retrieved from the underlying data model of the control the selection model is associated with."
Now if the ComboBox is editable and a value is entered in the editor box that is not in the list, both the valueProperty and the selectedItemProperty contain the entered value and selectionIndex=-1.
If you open the drop-down list there is no selection anymore.
The documentation of the ComboBox states:
3. Clearing the selection in the selection model does not null the value property - it remains the same as before.
4. It is valid for the selection model to have a selection set to a given index even if there is no items in the list (or less items in the list than the given index). Once the items list is further populated, such that the list contains enough items to have an item in the given index, both the selection model SelectionModel.selectedItemProperty() and value property will be updated to have this value. This is inconsistent with other controls that use a selection model, but done intentionally for ComboBox.
In my understand this is either a bug or contradicting documentation.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the following program with installed JavaFX 22 or later.
Enter "X" into the editor box.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
selectedItem() should be null according to the documentation of this property.
ACTUAL -
selectedItem() contains the entered value just like the valueProperty.
---------- BEGIN SOURCE ----------
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import java.util.Locale;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Locale.setDefault(Locale.GERMAN);
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
FlowPane flowPane = new FlowPane();
ComboBox<String> combo = new ComboBox<>();
combo.setEditable(true);
combo.getItems().add("Line 1");
combo.getItems().add("Line 2");
combo.getItems().add("Line 3");
combo.valueProperty().subscribe(newValue->{
System.out.println("valueProperty=" +
(newValue==null ? "null" : newValue) +
", selectedItem=" +
(combo.getSelectionModel().getSelectedItem()==null ? "null" :
combo.getSelectionModel().getSelectedItem()) +
", selectedIndex=" + combo.getSelectionModel().getSelectedIndex()
);
if (combo.getSelectionModel().getSelectedIndex()<0)
combo.getSelectionModel().clearSelection();
});
combo.valueProperty().set("X");
flowPane.getChildren().add(combo);
root.getChildren().add(flowPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
To check if an entered value is in the list you need to use selectedIndex.
- duplicates
-
JDK-8088012 SelectionModel: mismatches between api doc and implementations
- Open