// 
//     Licensed to the Apache Software Foundation (ASF) under one
//     or more contributor license agreements.  See the NOTICE file
//     distributed with this work for additional information
//     regarding copyright ownership.  The ASF licenses this file
//     to you under the Apache License, Version 2.0 (the
//     "License"); you may not use this file except in compliance
//     with the License.  You may obtain a copy of the License at
// 
//       http://www.apache.org/licenses/LICENSE-2.0
// 
//     Unless required by applicable law or agreed to in writing,
//     software distributed under the License is distributed on an
//     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//     KIND, either express or implied.  See the License for the
//     specific language governing permissions and limitations
//     under the License.
//

= DevFaqSwitchingMenusByContext
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqSwitchingMenusByContext
:description: Apache NetBeans wiki DevFaqSwitchingMenusByContext
:toc: left
:toc-title:
:syntax: true

=== How can I change the contents of a menu according to the selected file in the editor?

* Create your action and let it implement link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/actions/Presenter.Menu.html[Presenter.Menu]
* Return a special `JMenuItem` subclass that implements link:http://bits.netbeans.org/dev/javadoc/org-openide-awt/org/openide/awt/DynamicMenuContent.html[DynamicMenuContent] from `getMenuPresenter()`
* Implement link:http://bits.netbeans.org/dev/javadoc/org-openide-awt/org/openide/awt/DynamicMenuContent.html[DynamicMenuContent] methods to return the desired menu content, using link:http://bits.netbeans.org/dev/javadoc/org-openide-windows/org/openide/windows/TopComponent.Registry.html[TopComponent.Registry] for finding the selected file in the editor.

link:https://bitbucket.org/jglick/dynamicmenudemo/[https://bitbucket.org/jglick/dynamicmenudemo/] also does something similar.

=== Can I hide or show a whole menu or toolbar?

To hide a menu or toolbar you have to edit your `layer.xml` and append `_hidden` to the name of the desired menu or toolbar. You may also hide `*.instance` files.

<syntaxhighlight lang="xml" enclose="div" highlight="6">
<folder name="Menu">

[source,xml]
----

   <!-- Hide View menu -->
   <folder name="View_hidden"/>
   <folder name="SomeMenu">
       <!-- Hide a single menu item -->
       <file name="SomeAction.instance_hidden"/>
   </folder>
----

</folder>
<folder name="Toolbars">

[source,xml]
----

   <!-- Hide Edit toolbar -->
   <folder name="Edit_hidden"/>
----

</folder>
</syntaxhighlight >

It's generally much easier to do this from the NetBeans IDE, link:http://forums.netbeans.org/post-77476.html[as described here].

Note that to hide the Navigate menu one has to declare GoTo_hidden instead of Navigate_hidden (the menu was originally named GoTo, but was later renamed to Navigate by the means of Bundle.properties).

=== Apache Migration Information

The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.

This page was exported from link:http://wiki.netbeans.org/DevFaqSwitchingMenusByContext[http://wiki.netbeans.org/DevFaqSwitchingMenusByContext] , 
that was last modified by NetBeans user Jglick 
on 2011-12-14T00:20:23Z.


*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.
