Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem setting list of values of SingleSelectionField after it has been created #22

Open
aalmiray opened this issue Jul 6, 2018 · 1 comment

Comments

@aalmiray
Copy link
Contributor

aalmiray commented Jul 6, 2018

The attached code showcases a problem I recently encountered. It appears it's not possible to defer setting the list of values of a SingleSelectionField and have it match automatically if there's already a selection. In my use case, the selected value is known ahead of time (loaded from a DataSource) and the list of possible matches is calculated after the Form has been created.

In the attached sample code this would be the list3/value3 combination.

import com.dlsc.formsfx.model.structure.Field;
import com.dlsc.formsfx.model.structure.Form;
import com.dlsc.formsfx.model.structure.Group;
import com.dlsc.formsfx.model.structure.SingleSelectionField;
import com.dlsc.formsfx.view.renderer.FormRenderer;
import javafx.application.Application;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Sample extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        ObservableList<String> list1 = FXCollections.observableArrayList();
        ObservableList<String> list2 = FXCollections.observableArrayList();
        ObservableList<String> list3 = FXCollections.observableArrayList();

        ObjectProperty<String> value1 = new SimpleObjectProperty<>("2");
        ObjectProperty<String> value2 = new SimpleObjectProperty<>("2");
        ObjectProperty<String> value3 = new SimpleObjectProperty<>("2");
        ObjectProperty<String> value4 = new SimpleObjectProperty<>("2");

        ListProperty<String> prop3 = new SimpleListProperty<>(list3);
        ListProperty<String> prop4 = new SimpleListProperty<>(list3);

        SingleSelectionField field1 = Field.ofSingleSelectionType(list1);
        SingleSelectionField field2 = Field.ofSingleSelectionType(list2, 1);
        SingleSelectionField field3 = Field.ofSingleSelectionType(prop3, value3);
        SingleSelectionField field4 = Field.ofSingleSelectionType(prop4, value4);

        field1.selectionProperty().bindBidirectional(value1);
        field2.selectionProperty().bindBidirectional(value2);
        field3.selectionProperty().bindBidirectional(value3);
        field4.selectionProperty().bindBidirectional(value4);

        value1.addListener((v, o, n) -> System.out.println("VALUE1 " + n));
        value2.addListener((v, o, n) -> System.out.println("VALUE2 " + n));
        value3.addListener((v, o, n) -> System.out.println("VALUE3 " + n));
        value4.addListener((v, o, n) -> System.out.println("VALUE4 " + n));

        Form form = Form.of(
            Group.of(
                field1.label("List 1"),
                field2.label("List 2"),
                field3.label("List 3"),
                field4.label("List 4")
            )
        );

        Button button = new Button("Update");
        button.setOnAction(e -> {
            list1.clear();
            list2.clear();
            list3.clear();

            list1.addAll("1", "2", "3");
            list2.addAll("1", "2", "3");
            list3.addAll("1", "2", "3");
            prop4.setValue(FXCollections.observableArrayList("1", "2", "3"));
        });

        stage.setScene(new Scene(new VBox(new FormRenderer(form), button)));
        stage.sizeToScene();
        stage.show();
    }
}

Also, the list of items for list1 and list2 are never updated. It appears the problem is inside the factory method as it creates a copy of the input list (assumed to be a plain List<>) by wrapping it with a FXCollections.observableArrayList() instead of inspecting if the runtime type is compatible with ObservableList and use it as is.

@aalmiray
Copy link
Contributor Author

aalmiray commented Dec 8, 2018

@martinfrancois @Genron this is the issue we discussed during JavaFX Days Zurich 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant