tisdag 13 december 2011

JavaFX 2 - Mnemonic, what? Eh?

It is easier to write the word "mnemonic" then pronounce it, but what is it and what is it used for?

On Windows when you use the Alt-key in some applications you will get access to the menu. The character which describe the shortcut is called a mnemonic. This is used to enhance the usability of the application and give the power users a way of access parts in the application faster. So how is this done in JavaFX, and how can we use it to increase the usability of our applications?

In JavaFX we have the class Mnemonic which represent the shortcut. By using a key combination and a node we can use the mnemonic to bind a shortcut to the given object. If you have taken a look into the API of JavaFX you might have stumble upon the function setMnemonicParsing(boolean) in various classes (MenuItems, Buttons, Labels, ...). This function can be used to tell that you like to enabled parsing of mnemonic in that particular object. Let us have a look on a small demo which utilize the mnemonics.

@Override
 public void start(Stage stage) throws Exception {
  VBox mainLayout = new VBox();
  
  MenuBar menuBar = new MenuBar();
  Menu menu1 = new Menu("_Menu");
  menu1.setMnemonicParsing(true);
  menu1.getItems().addAll(createMenuItem("Menu 1"), createMenuItem("Menu 2"));
  
  Menu menu2 = new Menu("_Other");
  menu2.setMnemonicParsing(true);
  menu2.getItems().addAll(createMenuItem("Other 1"), createMenuItem("Other 2"));
  
  menuBar.getMenus().setAll(menu1, menu2);
  
  mainLayout.getChildren().setAll(menuBar);

  Scene scene = new Scene(mainLayout, 300, 100);
  
  stage.setTitle("Demo of mnemonic");
  stage.setScene(scene);
  stage.sizeToScene();
  stage.show(); 
 }

Note that some code is omitted, and only the code which we are interested in are shown.

Line 6: Here we create a menu which we will be using in our menu. Take a note to the '_' in the name. This defines where the mnemonic parser should find the letter to use in the shortcut. In this example it is the letter 'm'.

Line 7: Here we define that we like to enable mnemonic for the menu1 object. By enable the mnemonic we tell the framework to create a mnemonic object for us which binds the key combination of Alt + m to access the menu1 object.

This is actually all we need to add to enable mnemonics in the application. Currently the menu items has parse mnemonic enabled by default. But I kept the setMnemonicParsering(booelan) in the code for better explanation. Below is an example on the mnemonic when the user has pressed the alt-key on Windows. Note that on other OS there might be a other key to press. Like the Command button on MacOS.


So is this good and do it work? To be hones. I would like to say no. Currently I have not manage to bind any other key combination that work on other component then the Menu. Digging in the code a bit shows that mnemonic can only be triggered by using the Alt-key on windows, and what if you implement a own  mnemonic how do you then catch that event without implementing a custom key event handler? It do increase the usabillity in the application by giving me a way of accessing the menu without using the mouse. However for the moment the mnemonic of a menu is a bit buggy. Typing you key combination sometimes open both the menu in the demo above.

If you are looking for a custom shortcut mnemonics for your buttons your currently left alone implementing it.

1 kommentar: