This review aims to introduce functional interface applied to a concrete application : creating menu with graphical API Swing.
For this review purpose, we will consider a simple application that offers a traditional
File menu, with three actions :
With Swing, each menu item should be connected to the functional interface ActionListener, in order to perform an associated action. Since method reference could be now interpreted as functional interface, we will consider the
Controller interface which provides a functional method for each action it could handle.
To build our menu, we will first setup a dedicated class for building a JMenu entry, named
MenuBuilder. The following snippet shows our class header. We also made our class implements Supplier interface as it aims to provide a JMenu.
Menu bar builder
Finally, we need to setup a component which will be in charge of creating a JMenuBar, and will only contains the two following atttributes :
- Reference to our application controller (as we will use it to connect item to action).
- Target JMenuBar instance that this builder will fill up.
Then creating a menu will be as simple as following, we only have to use the previously defined
MenuBuilder class, with method reference from our
Controller instance as ActionListener for creating menu entry :
Last step, we have to define a static factory method for using our class which will prevent from misusage. Here we only have one menu, but you could add method as much as menu you have.
Bonus : Item delayed activation
Our menu is working, but let’s improve it. Imagine you want to set the
Save item disabled until a file has been opened. To do so, we will use the method you probably have noticed while looking the UML class diagram for the
As you can see, the returned Runnable instance is only enabling the associated menu item, and could then be used in order to activate the entry. To integrate this behavior for the
Save item, we will update our
Controller interface, and make it extends Consumer for Runnable type :
And use the associated
accept(Runnable) method into the file menu creation :
Controller implementation will just have to store the given Runnable and call it during the