Menu API provides a framework to create application menu bar from a properties file. Then actions' menu are handled from a unique action handler.

Here is a partial snapshot of a menu definition file (full example is part of out tutorial):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MenuBar=File
 
File.menu=FileOpen,-,ExitApp
File.text=File
File.mnemonic=F
 
FileOpen.label=Open...
FileOpen.tip=Open a file
FileOpen.img=fileopen.png
FileOpen.accel=F
FileOpen.mnemonic=F
 
ExitApp.label=Exit
ExitApp.tip=Close application
ExitApp.img=
ExitApp.accel=X
ExitApp.mnemonic=x
ExitApp.osMenu=win,linux

 

Then, you setup the menu in your code as follows:

1
2
3
4
5
6
7
//we load our ResourceBundle containing the main menu declarations
ResourceBundle rb = ResourceBundle.getBundle(
  TutorialThreeUIStarter.class.getPackage().getName()+".menu"); 
EZEnvironment.setUserDefinedActionsResourceBundle(rb);
 
//install the action manager listener to easily handle actions
EZEnvironment.getActionsManager().addActionMenuListener(new MyActionManager());

 

Finally, you handle the actions from the action listener:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private static class MyActionManager implements PropertyChangeListener {
  @Override
  public void propertyChange(PropertyChangeEvent event) {
    if (event.getPropertyName().equals("FileOpen")) {
      File f;
 
      f = EZFileManager.chooseDirectory();
      if (f != null){
        EZLogger.info("Chosen file is: " + f.getAbsolutePath());
      }
    }
    else if (event.getPropertyName().equals("ExitApp")) {
      EZEnvironment.getActionsManager().getDefaultActionHandler()
          .handleExit();
    }
  }