Geertjan's Blog

  • September 26, 2006

Letting the User Fill a Palette

Geertjan Wielenga
Product Manager
Something I have been wanting to do for a while is work out how to update a Component Palette on the fly. Yesterday's card game is a typical example—possibly the user won't like the set of cards provided. So, the user wants to be able to upload their own set of cards. Now, after working out how to let the user do this, I'm letting the game start with an empty palette. The user can type in the name of the new card and select an image, while indicating the suit to which the combination must belong:

When "Save" is clicked, a check is done to see whether the "Cards" folder exists in the user directory. If not, it is created. A check is also done to see whether the "Cards" folder contains a folder with the name of the suit ("Hearts" or "Clubs", etc.) If the folder for the suit doesn't exist, it is created. Then a file with the name specified (such as "Jack of Clubs") is created in the specified suit's folder. The file has a ".card" extension, recognized by a dataloader that was generated by the File Type wizard. The content of the file is the card's name and image.

So at this point, I have a folder structure within the config folder in the user directory, looking like this:



----Jack of Clubs.card

The TopComponent's Constructor includes this line:

associateLookup( Lookups.fixed( new Object[] { PaletteSupport.createPalette() } ) );

And that line refers to this method:

public static PaletteController createPalette() {
try {
return PaletteFactory.createPalette( "Cards", new MyActions(), null, new MyDnDHandler() );
} catch (IOException ex) {
return null;

And that's all. The palette is created from the "Cards" folder, which is found in my user directory. To initialize the folder structure, I created a set of folders in the layer.xml file, without any content:

<folder name="Cards">
<folder name="Clubs">
<folder name="Diamonds">
<folder name="Hearts">
<folder name="Spades">

Now, whenever I click Save, the IDE automatically loads the new card into the palette:

The card's image appears in the palette and, because of the drag and drop functionality in the TopComponent, as described in a few recent tutorials, I can drag a card and drop it on the drop target. And then the name is shown in red below the card, to indicate which card was dropped last:

David Kaspar, the NetBeans engineer behind the Visual Library 2.0, got me started with this approach. He told me about Repository.getDefault().getDefaultFileSystem(), which is the key to writing folders and files to the user directory. "That's interesting," I thought. Then I went back to my office and googled for a few minutes... and guess where I ended up? In this very blog. Thanks a lot Jens Trapp (who I will probably see tomorrow in Germany)! Your approach to Options window settings, described in this useful blog entry, by me helped me understand how this approach works, but in the context of the Component Palette!

Be the first to comment

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