Reputation: 25
I have 3 MenuButtons in my JavaFX-Application, each one filled with CheckMenuItems. I want to get the Text of every selected Item of the MenuButton. How shall i proceed?
Upvotes: 1
Views: 2003
Reputation: 2210
Take all MenuItems
, filter selected and map them to String
.
MenuButton mb = new MenuButton();
mb.getItems().addAll(new CheckMenuItem("One"), new CheckMenuItem("Two"), new CheckMenuItem("Three"));
mb.getItems().stream().forEach((MenuItem menuItem) -> menuItem.setOnAction(ev -> {
final List<String> selectedItems = mb.getItems().stream()
.filter(item -> CheckMenuItem.class.isInstance(item) && CheckMenuItem.class.cast(item).isSelected())
.map(MenuItem::getText)
.collect(Collectors.toList());
System.out.println(selectedItems);
}));
Upvotes: 1