代码之家  ›  专栏  ›  技术社区  ›  HRS

Javafx。添加和。删除在SelectionModel中不工作

  •  -3
  • HRS  · 技术社区  · 7 年前

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

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

    我在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);
    }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Brian Nieves    7 年前

    我建议您不要使用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);
    }
    

    }