Over a million developers have joined DZone.

Tip: How to Create Submenus on the NetBeans Platform

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

This tip shows the way I usually create an action for a NetBeans Platform application and then include it into a submenu.

  1. Create a new module, then use the Action wizard to create my DemoAction.

  2. The first panel asks for the type of Action, I leave Always Enabled selected. In the next panels, I fill the forms with all required data, as shown below:






  3. Nothing new, as you can see. And then let's tweak the layer.xml file entries that were created by the wizard shown above. This is what was generated:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE
    filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN"
    "http://www.netbeans.org/dtds/filesystem-1_2.dtd">
    <filesystem>
    <folder name="Actions">
    <folder name="Edit">
    <file name="org-casaburo-submenuDemo-DemoAction.instance">
    <attr name="delegate" newvalue="org.casaburo.submenuDemo.DemoAction"/>
    <attr name="displayName" bundlevalue="org.casaburo.submenuDemo.Bundle#CTL_DemoAction"/>
    <attr name="iconBase" stringvalue="org/casaburo/submenuDemo/add.png"/>
    <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/>
    <attr name="noIconInMenu" boolvalue="false"/>
    </file>
    </folder>
    </folder>
    <folder name="Menu">
    <folder name="Edit">
    <file name="org-casaburo-submenuDemo-DemoAction.shadow">
    <attr name="originalFile" stringvalue="Actions/Edit/org-casaburo-submenuDemo-DemoAction.instance"/>
    <attr name="position" intvalue="100"/>
    </file>
    </folder>
    </folder>
    <folder name="Toolbars">
    <folder name="File">
    <file name="org-casaburo-submenuDemo-DemoAction.shadow">
    <attr name="originalFile" stringvalue="Actions/Edit/org-casaburo-submenuDemo-DemoAction.instance"/>
    <attr name="position" intvalue="0"/>
    </file>
    </folder>
    </folder>
    </filesystem>

     

    Now, let's say I want to place my Demo Action menu item under "My Submenu". I modify the layer.xml as below (notice the lines 20 to 25 below):

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN"
    "http://www.netbeans.org/dtds/filesystem-1_2.dtd">

    <filesystem>
    <folder name="Actions">
    <folder name="Edit">
    <file name="org-casaburo-submenuDemo-DemoAction.instance">
    <attr name="delegate" newvalue="org.casaburo.submenuDemo.DemoAction"/>
    <attr name="displayName" bundlevalue="org.casaburo.submenuDemo.Bundle#CTL_DemoAction"/>
    <attr name="iconBase" stringvalue="org/casaburo/submenuDemo/add.png"/>
    <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/>
    <attr name="noIconInMenu" boolvalue="false"/>
    </file>
    </folder>
    </folder>
    <folder name="Menu">
    <folder name="Edit">
    <folder name="My Submenu">
    <file name="org-casaburo-submenuDemo-DemoAction.shadow">
    <attr name="originalFile" stringvalue="Actions/Edit/org-casaburo-submenuDemo-DemoAction.instance"/>
    <attr name="position" intvalue="100"/>
    </file>
    </folder>
    </folder>
    </folder>
    <folder name="Toolbars">
    <folder name="File">
    <file name="org-casaburo-submenuDemo-DemoAction.shadow">
    <attr name="originalFile" stringvalue="Actions/Edit/org-casaburo-submenuDemo-DemoAction.instance"/>
    <attr name="position" intvalue="0"/>
    </file>
    </folder>
    </folder>
    </filesystem>

    And this is the result:

    That's all!

     

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}