JavaFX MenuItem.setAccelerator () not working

I want my JavaFX program to be able to dynamically change the accelerator for menu items. However, I cannot get it to work in JavaFX 2.2. In the code below, in the first menu item there is an accelerator "Ctrl + M", and it works fine. "Ctrl + M" is printed every time a menu item is selected with the mouse or with the keyboard accelerator Ctrl + M. The second menu item tries to change the first accelerator of the item for people to "Ctrl + L". Having selected the second menu item, I can click on the first menu item again and it shows that its new accelerator is “Ctrl + L”, but I cannot use the keyboard accelerator Ctrl + L to activate the first menu item. Instead, I still need to use Ctrl + M to activate the first menu item. Any suggestions on what's going wrong here?

/* * from code at <http://java-buddy.blogspot.com/2012/02/javafx-20-set-accelerator.html> */ import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.input.KeyCombination; import javafx.scene.paint.Color; import javafx.stage.Stage; public class AcceleratorExample extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Accelerator example"); Group root = new Group(); Scene scene = new Scene(root, 400, 300, Color.WHITE); MenuBar menuBar = new MenuBar(); Menu FileMenu = new Menu("File"); final MenuItem item1 = new MenuItem("Select me"); item1.setAccelerator(KeyCombination.keyCombination("Ctrl+M")); item1.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { System.out.println(item1.getAccelerator()); } }); final MenuItem item2 = new MenuItem("New accel"); item2.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { item1.setAccelerator(KeyCombination.keyCombination("Ctrl+L")); } }); FileMenu.getItems().add(item1); FileMenu.getItems().add(item2); menuBar.getMenus().add(FileMenu); menuBar.prefWidthProperty().bind(primaryStage.widthProperty()); root.getChildren().add(menuBar); primaryStage.setScene(scene); primaryStage.show(); } } 
+7
javafx-2 accelerator
source share
1 answer

Accelerator dynamic change currently has some behavior, but this is a bug that was fixed for 8u20:

https://javafx-jira.kenai.com/browse/RT-35182

+4
source share

All Articles