[Solved] Javafx .add and .remove not working in SelectionModel


I would suggest you not use a StackPane for displaying only one thing at a time. Instead, switch out what you want to display. Then the button can just look up what’s being displayed, and throw it over to the other side. Try this:

public class GroceryList extends Application {

String fruit;

ObservableList<String> dataSelected =
        FXCollections.observableArrayList();
ListView<String> listSelected = new ListView<String>();

ListView<Department> listFruit = new ListView(FruitList.getFruitList());//Create fruitListView and add its data

ListView<Department> listVegetable = new ListView(VegetableList.getVegetableList());

@Override
public void start(Stage primaryStage) {

    ChoiceBox<Department> choiceBox = new ChoiceBox<Department>(DepartmentList.getDepartmentList());

    //Inital Setup
    choiceBox.setValue(choiceBox.getItems().get(0));//Set the ChoiceBox's inital value to Fruit

    VBox vbox1 = new VBox();
    vbox1.setSpacing(10);
    vbox1.setPadding(new Insets(20, 10, 10, 10));
    Label op1 = new Label("1. choose department:");
    op1.setFont(Font.font("Tahoma", FontWeight.NORMAL, 14));
    vbox1.getChildren().addAll(op1, choiceBox, listFruit);

    VBox vbox2 = new VBox();
    vbox2.setSpacing(10);
    vbox2.setPadding(new Insets(55, 10, 10, 10));
    Label op2 = new Label("Your shopping list");
    op2.setFont(Font.font("Tahoma", FontWeight.NORMAL, 14));
    vbox2.getChildren().addAll(op2, listSelected);

    VBox vbox3 = new VBox();
    vbox3.setSpacing(10);
    vbox3.setAlignment(Pos.CENTER);
    Button btn = new Button("Add");
    btn.setOnAction(e -> {
        for(Department item : ((ListView<Department>)vbox1.getChildren().get(2)).getSelectionModel().getSelectedItems())
        listSelected.getItems().add(item.toString());
    });
    vbox3.getChildren().add(btn);

    HBox hbox = new HBox();
    hbox.setSpacing(10);
    hbox.setAlignment(Pos.TOP_CENTER);
    Text sceneTitle = new Text("My Shopping List");
    sceneTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 25));
    hbox.getChildren().addAll(sceneTitle);

    BorderPane bp = new BorderPane();
    bp.setPadding(new Insets(15, 12, 15, 12));
    bp.setTop(hbox);
    bp.setLeft(vbox1);
    bp.setRight(vbox2);
    bp.setCenter(vbox3);

    choiceBox.getSelectionModel()
            .selectedItemProperty().addListener((obs, oldValue, newValue) -> {

        if (newValue != null) { //

            Department tempDepartment = newValue;
            switch (tempDepartment.toString()) { //Switch on the choosen Department value
                case "Fruit":
                    vbox1.getChildren().set(2, listFruit);
                    break;
                case "Vegetables":
                    vbox1.getChildren().set(2, listVegetable);
                    break;
                case "Beverages":
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setHeaderText("Not Implemented Error!");
                    alert.setContentText("You have not implemented this option yet! Do it now!");
                    alert.showAndWait();

            }
        }
    });

    Scene scene = new Scene(bp, 650, 500);//Add VBox to root

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}
/**
 * @param args the command line arguments
 */

public static void main(String[] args)
{

    launch(args);
}

}

1

solved Javafx .add and .remove not working in SelectionModel