The NetBeans Platform has a very powerful quick search feature that you can reuse and extend in your own platform applications. When I decided to do that recently, I wanted to put it onto the menu bar as it appears in the IDE. So I set out to find out how to do that.
The most useful resource I found in my search is the Quick Search Integration Tutorial from the NetBeans website. It is a good tutorial that explains how to extend the quick search feature. And how to put it onto the toolbar in your own platform application (the very last section). The part that is missing, however, is how to put it onto the menu bar instead. 🙂
I knew that the change would have something to do with the layer file. But besides knowing that it would involve the menu folder, I had no idea what to put where. And so I began my exploration to find out how the IDE does it.
I created a new standalone module, and I created a layer file for that module. By expanding the XML Layer node in the projects view in the IDE, I could see the layer file in context. This means that you can see everything defined in the file system, even though these items are not in the particular layer file itself. The important point here is that a standalone module effectively inherits all the layer file entries from the IDE. And so I knew that the layer in context would have the entries that I need. Expanding the Menu Bar folder, I saw a Quick Search entry. By right-clicking on it and choosing Go to Declaration, I opened the layer file where that was originally defined. The relevant part of the layer file is shown below.
<attr name="originalFile" stringvalue="Actions/Edit/org-netbeans-modules-quicksearch-QuickSearchAction.instance"/>
<attr name="position" intvalue="9010"/>
I put that part into my own module’s layer file, and when I executed the application the quick search box appeared. But it appeared directly next to the Help menu, which is not what I had in mind.
The quick search box in the wrong spot
So back to the original layer file I went, and I found this very interesting entry just above the quick search one (also in filesystem/menu):
<attr name="instanceCreate" methodvalue="javax.swing.Box.createHorizontalGlue"/>
<attr name="position" intvalue="9005"/>
Once I added that, the quick search bar appeared exactly where I wanted it. 🙂
The quick search box in its final location
And just like that, our application now has a powerful quick search feature!