在javafx中获取menuitem快捷方式操作

时间:2013-01-24 05:42:31

标签: javafx

我的代码如下:

 public static int mShortcut=50;

 @FXML private void OnSignIn(ActionEvent event )
 {

      setShortcut(101);
      CheckShortCut();


 }
 public void setShortcut(int shortcut) 
{
     mShortcut=shortcut;  
     mMenuItemProdType.getAccelerator();
     CheckShortCut();

}
 public  void CheckShortCut()
{
    switch(mShortcut)
    {
        case 101:
             System.out.println("Enter in 3 Case");

             mMenuItemProdType.setAccelerator(new KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN, KeyCodeCombination.SHORTCUT_DOWN));
             break;
        case 50:
              System.out.println("Enter in 50 Case");

             mMenuItemProdType.setAccelerator(null);
            break;
         default:
             mMenuItemProdType.setAccelerator(null);
             break;
    }

} 

我的要求用户如果没有登录就无法访问快捷键,所以在OnSignIn中我放了两个方法setShortcut(101);和CheckShortCut();但在上面的代码登录后我无法获得捷径事件所以任何想法我怎么能解决它?

1 个答案:

答案 0 :(得分:0)

我通过禁用菜单项解决了这个问题。

@FXML private MenuItem signMenuItem;
@FXML private MenuItem openMenuItem;
@FXML private MenuItem saveMenuItem;

@FXML
private void OnSignIn(ActionEvent event) {
    if (sign("admin", "1234")) {
        openMenuItem.setDisable(false);
        saveMenuItem.setDisable(false);
    }
}

private boolean sign(String name, String pass) {
    // do sign in
    return true;
}
@Override
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
    this.signMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.ENTER, KeyCombination.CONTROL_DOWN, KeyCombination.SHORTCUT_DOWN));
    this.openMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCombination.CONTROL_DOWN, KeyCombination.SHORTCUT_DOWN));
    this.saveMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN, KeyCombination.SHORTCUT_DOWN));
    openMenuItem.setDisable(true);
    saveMenuItem.setDisable(true);
}