I am developing an application which now has an extensive Menu with Accelerators.
I am looking for a way to make it possible for the user to bind the keys for certain functions and save the key bindings for the next startup.
Is there a way to load MenuItem accelerators for an FXML file from a configuration file?
Otherwise, I would probably need to serialize a map for each MenuItem function individually.
Thanks in advance for any info.
You can use KeyCombination.getName() to get a String
representation of a MenuItem
accelerator and KeyCombination.keyCombination(String) to create a MenuItem
accelerator from a String
representation. See example:
KeyCombination accelerator = new KeyCodeCombination(KeyCode.D, KeyCombination.CONTROL_DOWN);
MenuItem menuItem = new MenuItem("Click me");
menuItem.setAccelerator(accelerator);
...
//save accelerator to the config file
String accString = menuItem.getAccelerator().getName();
saveAcceleratorToConfig(accString); // save string to file
...
//load accelerator from the config file
String accString = loadAcceleratorFromConfig(); //load string from file
menuItem.setAccelerator(KeyCombination.keyCombination(accString));