Listview Javafx.add和.remove在SelectionModel中不起作用

Listview Javafx.add和.remove在SelectionModel中不起作用,listview,javafx,selectionmodel,Listview,Javafx,Selectionmodel,我就是不能让这个按钮工作-我做错了什么 该按钮仅用于将选定项从右列表移动到左列表,仅此而已 我在StackOverflow上查看了其他建议,但最后在添加和删除部分出现了一个错误。这个解决方案来自一个FXML项目——它在JavaFX中被称为不同的东西吗 还有-是否可以让按钮与多个不同的ListView一起工作?如果是的话,怎么办 public class GroceryList extends Application { private ListView<String> boxSel

我就是不能让这个按钮工作-我做错了什么

该按钮仅用于将选定项从右列表移动到左列表,仅此而已

我在StackOverflow上查看了其他建议,但最后在添加和删除部分出现了一个错误。这个解决方案来自一个FXML项目——它在JavaFX中被称为不同的东西吗

还有-是否可以让按钮与多个不同的ListView一起工作?如果是的话,怎么办

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

我建议您不要使用StackPane一次只显示一件东西。取而代之的是,切换出您想要显示的内容。然后按钮就可以查看正在显示的内容,并将其扔到另一边。试试这个:

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

}

我们应该对那些经验不如我们的人谦逊,这样他们就不会泄气去学习。只是说不,我还不知道我在做什么,但我正在努力学习。一些教程已经完成,谷歌被搜索,博克被阅读,但我还是不明白,这就是为什么我在寻求帮助。实际上,我认为这就是堆栈溢出的原因,但也许有人对更友好的论坛有什么建议?谢谢穆罕默德。或者是对那本像样的教程的一个建议?啊哈,这本书看起来好多了,而且更有意义-谢谢!