Search code examples
javajavafxmenuitem

How to remove MenuItems from MenuButtons with Javafx


I am trying to make a dynamically sized MenuButton.

I am designing a library program. Books go on Shelves. Over the course of the program, the number of shelves could increase or decrease. I want to make a menu Button that can reflect the shelves in the library - the set of MenuItems should increase if the number of shelves increase and decrease if the number of shelves decrease. This is my current code. However, it doesn't remove any MenuItems. Also, it duplicates all the MenuItems already included.

previous code omitted…
//the button “shelfBtn.getItems” is a MenuButton defined elsewhere

Button btn = new Button(“Refresh”);
            btn.setTranslateX(-20);
            btn.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent event) {                                             
                        for(String shelf : shelfs){
                            MenuItem newShelf = new MenuItem(shelf);
                            newShelf.setOnAction(new EventHandler<ActionEvent() {

                                @Override
                                public void handle(ActionEvent event) {
                                    // ignore this
                                    shelfField.setText(shelf);
                                }
                            });
                            shelfBtn.getItems().add(newShelf);
                        }
                    }
            });
remaining code omitted…

I have also tried using iteration to limit extra menuItems from being created - to no avail.

Additionally:

1) Is there a way to just delete a menuItem?

2) Is there a way to clear a MenuButton?

Thanks


Solution

  • Simply modifiy the items ObservableList:

    @Override
    public void start(Stage primaryStage) {
        ListView<String> listView = new ListView<>();
        for (int i = 0; i < 26; i++) {
            listView.getItems().add(Character.toString((char) ('a'+i)));
        }
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    
        MenuButton menuButton = new MenuButton();
    
        Button btn = new Button("Modify");
        btn.setOnAction((ActionEvent event) -> {
            // create menu items from selection
            menuButton.getItems().clear();
            for (String s : listView.getSelectionModel().getSelectedItems()) {
                menuButton.getItems().add(new MenuItem(s));
            }
        });
    
        Scene scene = new Scene(new VBox(listView, menuButton, btn));
    
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    As with any List there are multiple ways to remove and add elements to the list, like add, remove, clear, ect.


    Adding duplicates can be prevented by using a Set, e.g.

    Set<String> items = new shelfBtn.getItems().stream()
                                               .map(MenuItem::getText)
                                               .collect(Collectors.toCollection(HashSet::new));
    for(String shelf : shelfs){ 
        if (items.add(shelf)) {
            ...
        }
    }