Geertjan's Blog

  • July 9, 2012

Less than 50 Lines of Code to Create a Java Palette in NetBeans

Geertjan Wielenga
Product Manager

Want to drag and drop Java code snippets into the palette, in the same way as can be done for HTML files? If so, create a new module and add a class with the content below and you're done. You'll be able to select a piece of Java code, drag it into the palette (Ctrl-Shift-8 to open it), where you'll be able to set a name, tooltip, and icons for the snippet, and then you'll be able to drag it out of the palette into any Java files you like.

The palette content is persisted across restarts of the IDE.

package org.netbeans.modules.javasourcefilepalette;
import java.io.IOException;
import javax.swing.Action;
import org.netbeans.api.editor.mimelookup.MimeRegistration;
import org.netbeans.spi.palette.DragAndDropHandler;
import org.netbeans.spi.palette.PaletteActions;
import org.netbeans.spi.palette.PaletteController;
import org.netbeans.spi.palette.PaletteFactory;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.datatransfer.ExTransferable;
public class JavaSourceFileLayerPaletteFactory {
private static PaletteController palette = null;
@MimeRegistration(mimeType = "text/x-java", service = PaletteController.class)
public static PaletteController createPalette() {
try {
if (null == palette) {
return PaletteFactory.createPalette(
//Palette Actions:
new PaletteActions() {
@Override public Action[] getImportActions() {return null;}
@Override public Action[] getCustomPaletteActions() {return null;}
@Override public Action[] getCustomCategoryActions(Lookup lkp) {return null;}
@Override public Action[] getCustomItemActions(Lookup lkp) {return null;}
@Override public Action getPreferredAction(Lookup lkp) {return null;}
//Palette Filter:
//Drag and Drop Handler:
new DragAndDropHandler(true) {
@Override public void customize(ExTransferable et, Lookup lkp) {}
} catch (IOException ex) {
return null;

In my layer file, I have this content:

<folder name="JavaPalette">
<folder name="Snippets"/>

That's all. Run the module. Open a Java source file and the palette will automatically open. Drag some code into the palette and a dialog will pop up asking for some details like display name and icons. Then the snippet will be in the palette and you'll be able to drag and drop it anywhere you like. Use the Palette Manager, which is automatically integrated, to add new categories and show/hide palette items.

Related blog entry, for which the above is a big simplification: Drag/Drop Snippets into Palette

Join the discussion

Comments ( 4 )
  • Patrick Guasura Wednesday, July 11, 2012


    I have am NEW to NetBeans. I am going through yous screen cast on the Top 10 Netbeans APIs. I would like to download these screen casts. Where can I download a copy of each one.

    I am very much interested in developing applications using NetBeans.I hope you will reply soon.


  • Enrico Scantamburlo Sunday, July 15, 2012

    I took a look at the code (but not tested it) and I think that it should be

    if (null == palette) {

    palette = ...



    return palette;

    or you can just remove the palette field.

  • guest Sunday, December 16, 2012

    Great tutorial! I used it to build a java palette that contains all the macros in netbeans with a tooltip description for each (Visual Macros). That way I don't have to remember them. It just basically drops the abbreviation for the macro onto the document and uses the robot class to type the TAB key to expand it.

    The only problem is for user defined macros - the tab doesn't get pressed to expand. Any idea how or (where to look in the APIs) to override the editor drop to always include a keypress after dropped?

    Thanks man!


  • JJJ3 Sunday, May 4, 2014

    Thx soooooo much. Im 12 and im learning how to write Java code using NetBeans IDE which Im sure your familiar with. I want to be just like you and the rest of the employess of oracle.Thx again!!!

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.