palexdev / MaterialFX

A library of material components for JavaFX
GNU Lesser General Public License v3.0
1.22k stars 122 forks source link

combobox setting selected value #84

Closed warmuuh closed 2 years ago

warmuuh commented 3 years ago

Using combobox, something is off with setting the selectedValue, example code:

public class Test extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        var root = FxmlBuilder.vbox();
        Scene mainScene = new Scene(root);

        mainScene.getStylesheets().clear();
        mainScene.getStylesheets().add("/mfx/light.css");

        root.add(new Label("MFXComboBox"));
        var nmfx = new MFXComboBox<>();
        nmfx.getItems().add("a");
        nmfx.getItems().add("b");
        nmfx.getItems().add("c");
        nmfx.setSelectedValue("b");
//        Platform.runLater(() -> nmfx.setSelectedValue("b"));
        root.add(nmfx);

        primaryStage.setScene(mainScene);
        primaryStage.show();
    }
}

this results in an empty combo-box being rendered (as in: no value selected). using Platform.runLater(() -> nmfx.setSelectedValue("b")); instead, the value is now there but the value is not highlighted as selected in the list-view: image

once manually selecting a value, the value and selected item in list are in sync

one more difference to other comboboxes (MFXLegacyComboBox, ComboBox or JFXComboBox) is that dropdown doesnt close once window looses focus, but that might be not an issue at all

palexdev commented 3 years ago

My bad, I should delete that method and make the property read only. You are doing it the wrong way. To select an item you must use the selection model. So: nmfx.getSelectionModel().selectItem("b");

warmuuh commented 3 years ago

ok, that works... just mentioning: also nmfx.getSelectionModel().setSelectedItem(...) doesnt work, could maybe a bit streamlined.

one follow-up question would be what property to use for property-binding? for all other combo-boxes, it was the value-property.

normally, i use something like comboBox.valueProperty().bindBidirectional(otherProperty)

palexdev commented 3 years ago

Almost done :)

Work

palexdev commented 3 years ago

Fixes available on staging branch, I think I'll make some more improvement though

palexdev commented 2 years ago

MFXComboBoxes have been reworked in latest release Many improvements to selection, and values set have been made