Geertjan's Blog

  • October 11, 2006

Filter the Palette!

Geertjan Wielenga
Product Manager
In Listen to the Palette!, I wrote about the palette's property change listener. Something else that the palette has is... a filter. Thanks to the NetBeans API PaletteFilter class, you can cause a category or palette item to be invalid. If a category or item is invalid, it is not shown. For example, here only one category (called "Basic") is valid, initially:

However, when the user selects the "Expert" checkbox, the palette displays an additional category, with additional items:

In this way, you can let the user (or the user's actions) trigger the content of the palette, either on category-level or on item-level. That could be useful in a variety of situations. And it is not very hard to set up. First, when the checkbox is selected, a Boolean value is sent to the class where all my palette activities are handled:

private void expertCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {

Then, in that class, the filter is defined:

public static class MyPaletteFilter extends PaletteFilter {
static boolean isCategoryExpertVisible = false;
public static void setCategoryExpertVisible(boolean isCheckedOrNot){
if( isCategoryExpertVisible != isCheckedOrNot ) {
isCategoryExpertVisible = isCheckedOrNot;controller.refresh();
public boolean isValidCategory(Lookup lookup) {
Node categoryNode = (Node)lookup.lookup( Node.class );
String categoryDisplayName = categoryNode.getDisplayName();
if (categoryDisplayName.equals("Expert") && !isCategoryExpertVisible) {
return false;
return true;
public boolean isValidItem(Lookup lookup) {
return true;

Note the line that is highlighted above—we need to call controller.refresh to wake up the controller, which will refresh the palette for us, by calling isValidCategory and isValidItem. (An alternative, less user friendly way, is to make the user do it manually in the palette, by right-clicking it and choosing "Refresh Palette".) The refresh is also done when the palette is created, so at that point the Boolean is set to its default value, which is false, so that the "Expert" category is not shown initially.

The two methods required by the PaletteFilter are isValidCategory and isValidItem. If they return true, they show something; if they return false, they hide something. What that "something" is, is up to you. Here in isValidCategory, we get the display name (set in the XML layer file) from the node and then check the Boolean isCategoryExpertVisible, which is set/unset by the checkbox. And isValidItem is only there because it is a required method; here, it doesn't filter anything, it is just a dummy method.

Note that you need to attach the filter to the palette controller. You do this when you define the palette controller:

controller = PaletteFactory.createPalette("abcde", new MyActions(), new MyPaletteFilter(), null );

And that's all that's needed to show or hide the "Expert" category.

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.