Drag/Drop Snippets into Palette

Changes to the NetBeans APIs since the release of 6.1 are continually updated here. So, if you're interested in the NetBeans APIs, bookmark that page and look at it every week or so to see the latest changes. (By the way, the list of NetBeans API changes in 6.1 are found here.)

One of the post-6.1 changes (available since about a week in development builds) is this one: "Allowing user dropping text into the palette to create new custom code clips." (Read about it here.) The author of the API, Stan Aubrecht, who also added this latest enhancement, described it in his blog yesterday.

In summary, this enhancement means two different things:

  • NetBeans users. If you are a user of NetBeans IDE, you are able, since last week's post-6.1 development builds, to drag and drop HTML snippets from the editor into the palette, to create a new custom code snippet. If you hold down the Ctrl key while you drag the snippet into the palette, the snippet will be copied from the editor to the palette; if you don't hold down the Ctrl key, the snippet will be cut from the editor and pasted into the palette. Click the image below to enlarge it, it shows you what you'll see when you drop the snippet into the palette, whether copied or cut:

    You need to drag the snippet onto an existing item or a category in the palette. Currently this works for HTML files, while other existing palettes will follow.

  • NetBeans developers. This is even more interesting (in my opinion). If you provide a palette (i.e., you are creating a module that adds a palette to NetBeans IDE or another NetBeans Platform application that has an editor and a palette), you can enable the palette to provide the same functionality as the above. Let's go through the steps that make this possible. Take the following steps:
    1. Download the Java Source File Palette Sample from the Plugin Portal.

    2. Install the module and open the sample from the New Project wizard's Samples | NetBeans Modules category.

    3. Tweak the class called JavaSourceFileLayerPaletteFactory. Everything that is in bold below is what you need to add, i.e., everything else is from the class you find in the sample:
      public class JavaSourceFileLayerPaletteFactory {
          public static final String JAVA_PALETTE_FOLDER = "JavaPalette";
          private static PaletteController palette = null;
          public JavaSourceFileLayerPaletteFactory() {
          public static PaletteController createPalette() {
              try {
                  if (null == palette) {
                      //Add null for the filter, which is unused in this sample,
                      //but needs to be set if the DragAndDropHandler will be used:
                      palette = PaletteFactory.createPalette(JAVA_PALETTE_FOLDER, 
                         new MyActions(), null, new MyDragAndDropHandler());
                  return palette;
              } catch (IOException ex) {
              return null;
          private static class MyDragAndDropHandler extends DragAndDropHandler {
              MyDragAndDropHandler() {
              //Maybe you don't like the default 'add to palette' implementation,
              //so you could create your own here:
              public void customize(ExTransferable t, Lookup item) {
          private static class MyActions extends PaletteActions {
              //Add new buttons to the Palette Manager here:
              public Action[] getImportActions() {
                  return null;
              //Add new contextual menu items to the palette here:
              public Action[] getCustomPaletteActions() {
                  return null;
              //Add new contextual menu items to the categories here:
              public Action[] getCustomCategoryActions(Lookup arg0) {
                  return null;
              //Add new contextual menu items to the items here:
              public Action[] getCustomItemActions(Lookup arg0) {
                  return null;
              //Define the default action here:
              public Action getPreferredAction(Lookup arg0) {
                  return null;

      What this means is that you need to provide (at least) a default implementation of org.netbeans.spi.palette.DragAndDropHandler, as done above.

    4. Install the module. Now you can drag and drop code from the editor to which the palette applies (i.e., the Java editor, in the case of the sample above) and then automatically the "Add to Palette" dialog will appear, exactly as done for HTML files in the case of the NetBeans user scenario above.

I think this is an extremely cool enhancement and we'll see it implemented in the other palettes in the IDE too. But it's especially cool that it's possible (and so easy) to implement in your custom palettes too. Of all the NetBeans APIs, the Palette API is one of the ones that has seen the most cool enhancements over the past two years, in my opinion. Are there other things that could make it even better?


> Are there other things that could make it even better?

Well how about leveraging the community and the component idea (which is really what the palette is about) by being able to share, browse and add palettes from plugin Portals, directly from the pallette TopComponent?

It would remedy the classic problem in Java of discoverability (took me years to know about the various Swing datepicker controls out there). Perhaps it could even allow people to vote on the best ones (took me years to figure out about the best Swing datapicker).

Of course, this could be general purpose, not just for the Swing category.

Posted by Casper on May 15, 2008 at 11:48 PM PDT #

Hi Casper, I think it would be cool if one could browse palette items from within the Palette. I'm not sure how the mechanism would work, though. Here is how I see it: (1) You would create a NetBeans module that contains the snippets that you want to make available (as described in the "NetBeans Code Snippet Module Tutorial"\*), (2) You would set some kind of special property that marks your module as a "palette provider", (3) You would make your module available to an update center (4) I would need to have your update center registered within my IDE, (5) I would then right-click inside my palette and see a list of modules that have the "palette provider" property set, (6) I would then select the modules I am interested in and these would then be installed, with the items immediately being available (without a restart).

Does that make sense to you?


Posted by Geertjan on May 16, 2008 at 01:30 AM PDT #

Sounds like an awesome usability experience to me. As I mentioned, a large part of the problem is often to even know that \*something\* exist (as you demonstrated yourself a few blog entries ago, when acknowledging that you had not heard of RichFaces).

I don't know if the palette/category hierarchy is rich enough or how it would work technically, but it sure would be nice to have pluggable providers as NetBeans starts supporting more and more RAD technologies.

Posted by Casper on May 16, 2008 at 03:07 AM PDT #

What can I say? Only "About bloody time." :)) I hope you remember that I was asking you about the same functionality back in 5.0 release when I wanted to create a snippets module (a general one, not only for HTML).

Posted by kovica on May 16, 2008 at 10:33 AM PDT #

Well, where's the issue number, kovica?

Posted by Geertjan on May 16, 2008 at 10:35 AM PDT #

Yes, you are correct, there is none. :)
Don't get me wrong. I love NetBeans, I use it everyday for all my development work.

Posted by Kovica on May 16, 2008 at 05:32 PM PDT #

Ok. :-) I have created it now:

Posted by Geertjan on May 16, 2008 at 06:04 PM PDT #

PS: So, if you like it, vote for it.

Posted by Geertjan on May 16, 2008 at 06:05 PM PDT #

Hello sir,,,,I am fresher as a developer.....
I am going to develop dicom viewer kind of application in netbeans platform,,but i am little bit confused on which way i have to start the developing means i know its work process and everything but for as developing dont have a idea .....should it better to start with loading images by opening file and etc kind of work....reply me asap......

Posted by purav on April 09, 2012 at 04:55 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« August 2016