How to use this tool

Aliaksei Taliuk


This page is some kind of "How to" but in very short variant because developers will understand this just to look at this. There are only 3 steps here:


1.) Writing an XML Menu Descriptor

So, we write an XML document which looks like this one:

<?xml version="1.0" encoding="UTF-8"?>
<menubar xmlns="http://www.alexblog.me"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://www.alexblog.me/projects/jmenubartool/menu-descriptor.xsd">

    <menu name="FileMenu" value="File" mnemonic="F">
        <menuitem name="SaveProjectMenuItem" value="Save project" mnemonic="s" tooltip="Tooltip example"/>
        <menuitem name="OpenProjectMenuItem" value="Open project" mnemonic="o"/>
        <separator/>
        <menuitem name="ImportMenuItem" value="Import" mnemonic="i"/>
        <menuitem name="ExportMenuItem" value="Export" mnemonic="e"/>
        <separator/>
        <menuitem name="OpenLibraryMenuItem" value="Open Library" mnemonic="l" enabled="false"/>
        <menuitem name="RenameFilesMenuItem" value="Rename files" mnemonic="r"/>
        <separator/>
        <menu name="Submenu1" value="Submenu1" enabled="true">
            <menuitem name="disabledMenuItem" value="Disabled Menu Item" enabled="false"/>
            <separator/>
            <radiobuttongroup>
                <radiobuttonmenuitem name="radiobutton1" value="Radiobutton" selected="true"/>
                <radiobuttonmenuitem name="radiobutton2" value="Another one"/>
                <radiobuttonmenuitem name="radiobutton3" value="And the las one..."/>
            </radiobuttongroup>
            <separator/>
            <checkboxmenuitem name="checkbox1" value="Checkbox"/>
            <checkboxmenuitem name="checkbox2" value="Another one but selected" selected="true"/>
            <menu name="Submenu2" value="Submenu">
                <menuitem name="SubSubMenuItem" value="MenuItem with icon" icon="images/icon.png"/>
                <separator></separator>
                <menuitem name="SubSubMenuItem2" value="MenuItem with tooltip" tooltip="Some tooltip text"/>
            </menu>
        </menu>
        <menuitem name="ExitMenuItem" value="Exit" mnemonic="x"/>
    </menu>
    <menu name="OptionsMenu" value="Options" enabled="false">
        <menuitem name="SettingsMenuItem" value="Settings" mnemonic="s"/>
    </menu>
</menubar>

XML Document should be valideted by the next XML Schema:
See source of menu-descriptor.xsd

Both menu and menuitem tags have the next attributes:

"name" - required - string - some kind of ID of the element (uses for fetching menus and menu items and when we assign an action listener). It also should be unique for all XML document space to prevent name conflicts.

"value" - required - string - the text which will displayed on the item UI.

"mnemonic" - optional - string(1) - a char which provides fast keyboard access to item

"icon" - optional - string - path to menu (menuitem) image icon

"enabled" - optional - boolean - enable/disable element

"tooltip" - optional - string- tooltip text

Both checkbox and readiobutton tags have the next attributes:

"selected" - optional - boolean- selected or not


2.) Adding JAR to project CLASSPATH

After this download jmbct-X.X.jar from download page of this project and add it to CLASSPATH of your project.


3.) Write a simple code and get your JMenuBar object

More fast variant via Util class method:

JMenuBarExt menuBar = JMenuBarFactory.createMenuBarFromXML("menu-descriptor-example.xml");

Or this:

//creates XML based creator
MenuBarCreator xmlCreator = new XMLBasedCreator(new DOMMenuParser("menu-descriptor-example.xml"));
//creates menu bar
JMenuBarExt menuBar = xmlCreator.create();

MenuBarCreator - common interface for all creators (XML-based, PLAIN-TEXT-based e.t.c.)

XMLBasedCreator - XML-based implementation which need to point a parser in constructor (DOM Parser in example)

JMenuBarExt - extension of JMenuBar class which provides simple adding of Action Listeners to menu items (finds by "name" attribute which was wrote in XML doc for the menu item). It also provides getting menu elements (JMenu, JMenuItem, JRadioButtonMenuItem, JCheckBoxMenuItem) by their names (uses with classcast operation).


And here it is

MENU SCREENSHOT EXAMPLE