AgostinoX
AgostinoX

Reputation: 7683

javafx-2 ComboBox, undocumented feature: ComboBox maintains a value setted via setValue(T object) even if it is not in the items collection

javafx-2 ComboBox seems to have an (yet) undocumented feature that suggest a whole new combo box concept behind the scenes.

Simply put, the value property is not constrainted to be one of the elements of the items collection.

While the user can only choose one of the items, the setValue method allows you to set a value even if it does not belong to the collection (provided that it is of the correct type, of course, but this is enforced by generics).

In the same way, if the valueProperty is bound to some other property, valueProperty will follow the value of the other property whether it is in the items property or not.

This may be useful in several circumstances, such as when a user chooses an item that is subsequently removed from the items collection.

Since this feature is not documented (or i couldn't been able to find documentation), i wonder if I can rely on it, or it has to be consider some sort of "side-effect" and in so it might be removed in future releases of the framework.

SSCE (just fix imports):

public class T08 extends Application {
@Override public void start(Stage primaryStage) throws Exception {
    ComboBox<String> c = new ComboBox(FXCollections.observableArrayList("item-a", "item-b"));
    primaryStage.setScene(new Scene(c));
    c.setValue("outsider-item");
    primaryStage.show();
} 
}

The output is as following. As you can see, the combo displays the "outsider-item" value, but then it is not proposed in the list.
The getValue() method is coherent with the value displayed by the component. enter image description here

Upvotes: 0

Views: 580

Answers (1)

Alexander Kirov
Alexander Kirov

Reputation: 3654

This behavior is known,

I've filed an issue http://javafx-jira.kenai.com/browse/RT-27543 to describe this behavior in javadoc.

It is a fixed and tested behavior, and not expected to change.

Upvotes: 1

Related Questions