2

How can I attach an Accelerator to a MenuItem inside a MenuButton (using JavaFX 2.2)? I tried it the following way:

public class Minimal extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
    primaryStage.setScene(SceneBuilder.create()
        .root(StackPaneBuilder.create()
            .children(MenuButtonBuilder.create()
                  .text("MenuButton")
                  .items(
                    MenuItemBuilder.create()
                      .text("MenuItem A")
                      .accelerator(new KeyCodeCombination(KeyCode.B, KeyCombination.ALT_DOWN))
                      .onAction(new EventHandler<ActionEvent>() {
                          @Override public void handle(ActionEvent e) {
                            System.out.println(e.getEventType() +
                                               " occurred on Menu Item A");
                          }
                        })
                      .build()
                   )
                  .build())
            .build())
        .build());
    primaryStage.show();
}

public static final void main(String[] args)
{
    Minimal.launch(args);
}

}

It compiles fine but gives me an exception at runtime when I press the KeyCombination. Selecting the MenuItem with the mouse works as expected. Here's the exception (there are more method calls in the stack trace, but none of them is from my application):

java.lang.NullPointerException
    at com.sun.javafx.scene.control.skin.MenuButtonSkinBase$5.run(MenuButtonSkinBase.java:306)
    at com.sun.javafx.scene.KeyboardShortcutsHandler.processAccelerators(KeyboardShortcutsHandler.java:296)
    at com.sun.javafx.scene.KeyboardShortcutsHandler.dispatchBubblingEvent(KeyboardShortcutsHandler.java:119)

Since the Stack trace says 'MenuButtonSkinBase', it tried disabling all custom styles I use, but no luck.

Edit: Here is the jdk version output i used to compile the code:

java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b11)
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing)

Thanks in advance!

Oromis
  • 347
  • 4
  • 13
  • Working fine for me. Can you show rest of the code? or may be the relevant code like how to you add this button to the scene hierarchy. – Bhupen Nov 09 '12 at 19:00
  • I changed the example to a full application. This causes the error on my machine. Might a bug in my JavaFX-Implementation be the reason? – Oromis Nov 09 '12 at 23:50
  • Still working for me. I'll suggest you to get the latest jdk, compile and run the program again and if it still doesnt work, knock on oracle forums or log a bug. And, in the meanwhile continue w/o the accelerators :) – Bhupen Nov 10 '12 at 17:38
  • http://stackoverflow.com/questions/12710468/using-javafx-2-2-mnemonic i think this would help you... – Anshul Parashar Nov 06 '13 at 11:49
  • Did you figure out the problem? I'm stuck with the same... – user1879408 Jun 12 '14 at 13:31
  • @user1879408 As far as I can remember, it was a combination of my JDK and my JavaFX version. Try updating both. – Oromis Jun 13 '14 at 12:07

0 Answers0