Listen to the Palette!

Don't like providing drag and drop support? Hate those data flavors and transferables? Never mind—you can provide your users with a Component Palette, despite your aversion to drag and drop programming. Here, for example, is a palette that does not make use of any drag and drop functionality. Instead of dragging, you select an item. When you do so, it appears on the TopComponent:

The key to this approach is PaletteController.addPropertyChangeListener. Use this method to listen to the palette. When an item is selected in the palette, do something for/with the selected item.

A second, more fundamental, key to this approach is the realisation that each item in the palette is a node just like any other node provided by the Nodes API. So, a typical pattern is to add the palette to the TopComponent via this line in the Constructor:

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

Then, the createPalette() method looks as follows (note especially the highlighted lines):

public static PaletteController createPalette() {
    try {
        controller = PaletteFactory.createPalette("Cards", new MyActions(), null, null );
    } catch (IOException ex) {
    controller.addPropertyChangeListener( new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if( PaletteController.PROP_SELECTED_ITEM.equals( evt.getPropertyName() ) ) {
                Lookup selItem = controller.getSelectedItem();
                if( null != selItem ) {
                    Node selNode = (Node)selItem.lookup( Node.class );
                    if( null != selNode ) {
                        Image selectedImage = selNode.getIcon(BeanInfo.ICON_COLOR_32x32);
                        String selectedName = selNode.getDisplayName();
                        TestTopComponent.findInstance().setCard(selectedName, selectedImage);
    return controller;

So the property change listener listens to the controller, which handles the palette. When an item is selected, we get it from the controller and create a Lookup. Then we create a node from the lookup. And the node is like any other, it exists for display purposes, so it has a display name and an image. Sending that display name and image to the TopComponent, where they are attached to a JLabel, is all you need to do to have an item in a palette appear in a TopComponent, without any drag or drop functionality being needed. Pretty handy.

In other news. NetBeans web services guru Milan Kuchtiak started blogging on Sunday (yesterday). Click here to check it out, it already contains two interesting blog entries on JAX-WS web services in the context of J2SE applications. The second part shows how to add the J2SE JAR file as a wrapper to a web application.


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.


« November 2015