listviewjavafxselectionmodel

Javafx .add and .remove not working in SelectionModel


I simply can't get this button to work - what am I doing wrong?

The button is only suppose to move selected item from right list to left list - that's all.

I have looked at other suggestions at StackOverflow, but end up with an error on the add and remove part. The solution was from a FXML project - is it called something different in JavaFX?

Also - is it possible to get the button to work with several different listviews? If yes, then how?

public class GroceryList extends Application {

private ListView<String> boxSelected;

private ListView <String> boxFruit;


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());

private void moveAction(ActionEvent action){
    String selectedItem = boxFruit.getSelectionModel().getSelectedItem();
    listFruit.remove(selectedItem);
    listSelected.add(fruit);       
}

@Override
public void start(Stage primaryStage) {

    boxFruit.setItems((ObservableList<String>) listFruit);
    boxSelected.setItems(dataSelected);

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

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

        if (newValue != null) { //

            Department tempDepartment = (Department) newValue;
            switch (tempDepartment.toString()) { //Switch on the choosen 
            Department value

                case "Fruit":
                    listFruit.setVisible(true);//When Fruit is selected 
                    in the ChoiceBox show the fruitList
                    listVegetable.setVisible(false);
                    break;
                case "Vegetables":
                    listFruit.setVisible(false);
                    listVegetable.setVisible(true);//When Vegatables is 
                    selected in the ChoiceBox show the vegetablesList
                    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();

            }
        }
    });

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

    //Since fruit is the inital value for the ChoicBox, set the 
    fruitListView to visisble and the others to not visible
    listFruit.setVisible(true);
    listVegetable.setVisible(false);
    //End Initial Setup

    StackPane stackPane = new StackPane(listFruit, listVegetable);//Add 
    both ListViews to the StackPane
    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, stackPane);        

    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");
    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);

    /*private void moveAction(ActionEvent action){
    String selectedItem = boxFruit.getSelectionModel().getSelectedItem();
    listFruit.remove(selectedItem);
    listSelected.add(fruit);       
    }*/

    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);
}
}

Solution

  • 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);
    }
    

    }