I’ve been experimenting with adding keyboard accelerators to some of the Clojure programs I’ve written with JavaFX-based user interfaces. As part of that investigation, I tried to translate the Java program here to Clojure. The program just puts up a window with a menu bar containing only a “
File“ menu which itself contains one item, “
Exit“. Most programs provide a keyboard shortcut or accelerator to close the program with a
Ctrl-X (on Windows). Figuring out how to add that functionality was a bit of an issue for me.
KeyCodeCombinationaccepts a variable length argument array. In a few cases, you can get lucky and simply provide a Clojure vector of the arguments. This wasn’t one of those happy coincidences. Instead, you need to use the canonical method of packing all of those arguments into a Java array using Clojure’s
into-arrayfunction. You need to do it even if you have only one argument, as was the case for me.. That isn’t usually so bad, but this time I had a little trouble figuring out the type of array to create. Turns out you have to specify that the list of modifiers expected by the constructor are
KeyCombination.Modifierarguments in Java are
Here’s the translated program:
Looking back, it’s a little embarrassing that it took so long to figure out since the clue was right there in the error messages. But at least it’s figured out now.