Adding a menu bar and menu items

As class FrmMain defines the main window of application SimplyHTML it should present the application's functions to the user. One way of doing this is to maintain a menu bar and menu items. In class FrmMain this is done mainly in method constructMenu which is called in FrmMain's constructor.

Fields referring to menus and menu items

Class FrmMain holds a field for each menu and menu item in its menu bar. These fields are visible to all methods within class FrmMain. By keeping fields referring to the menu structure it is possible to influence appearance and behaviour of the menu structure later on during the flow of the program.

To build a menu bar using the menu fields of class FrmMain, the fields have to be initialized first. This is done at the same time the fields are declared upon construction of a FrmMain object.

Initialization is done by creating a new instance of JMenu and JMenuItem respectively on the line of code where that field is declared. Each menu item gets associated with an instance of the action class which is meant for handling selctions of the particular menu item.

Method constructMenu

The previously initialized menu fields of class FrmMain are put together on a menu bar by method constructMenu. The method first creates a new JMenuBar object and then adds all previously initialized menus and menu items to it.

It then adds a new MenuListener to the file menu object which takes care of adjusting appearance of the file menu whenever it is selected. Finally the new menu bar is associated with the main window by calling method setJMenuBar, which was inherited from JFrame.