From Pain to Gain: Swing and the NetBeans Platform in the Real World

Tags:
Other extensions needed

A little more work was necessary with ListView, since the thumbnail-rendering components required a custom renderer (to provide the “slide” look and for overlaying extra information). Also needed was a grid layout with a predefined cell size. See Figure 9.

The standard JList makes these things quite easy to achieve. It’s a matter of setting a few properties:  

jList.setCellRenderer(...);
jList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
jList.setFixedCellWidth(150);
jList.setFixedCellHeight(150);

But unfortunately NetBeans Platform developers left these methods out of ListView (much like the control of colors). With a similar workaround to the one used for the colors problem (i.e. accessing the inner JList), it was easy for me to add the missing methods to EnhancedListView, as you see in Listing 9.

Listing 9. New methods in ListView.

public class EnhancedListView extends ListView {

...
...
...

public void setCellRenderer (ListCellRenderer listCellRenderer) {
jList.setCellRenderer(listCellRenderer);
}

public void setFixedCellWidth (int size) {
jList.setFixedCellWidth(size);
}

public void setFixedCellHeight (int size) {
jList.setFixedCellHeight(size);
}

public void setLayoutOrientation (int layoutOrientation) {
jList.setLayoutOrientation(layoutOrientation);
}

}


Listing 10. Retrieving the Node object from a custom cell renderer.

import org.openide.explorer.view.Visualizer;

public class ThumbnailRenderer extends JComponent implements ListCellRenderer {

final public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
this.hasFocus = cellHasFocus;
this.selected = isSelected;
this.index = index;
Node node = Visualizer.findNode(value);
thumbnail = (Thumbnail)node.getLookup().lookup(Thumbnail.class);
return this;
}

...
...
...


As a final point, custom ListCellRenderers must add an extra step through a utility class, named Visualizer, to retrieve the Node associated to the current object to render, as shown in Listing 10 (the usual JList approach, that is a cast applied to the value parameter, would not work).

Conclusions

Well, what can I say... blueMarine has survived its crisis, and its open-source “advertising” has already brought me a business opportunity for an application related to photo management (which reuses some of the back-end components of blueMarine – track my blogs if you want to know more about it). And without NetBeans, I would have dropped blueMarine and missed this opportunity.

What about blueMarine itself? Redesigning around the NetBeans Platform required some time, of course. It’s likely that you’ll need to deeply redesign an existing application if you want to take full advantage of all NetBeans Platform features (even though you could choose a lower-impact, more incremental way of working than I did). But I’m quite pleased with the results, and now I can add new features much faster than before.

To me, this means that Swing and the NetBeans Platform are now mature technologies for the desktop. I’m no longer forced to waste time reinventing the wheel. On the contrary: I can move very rapidly from an idea for my application, to an effective implementation.

0
Average: 5 (1 vote)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

jiji530 replied on Fri, 2009/06/26 - 8:29pm

thanks for your post.perhaps you will like abercrombie,ed hardy,mortgage rates,tiffanysanded hardy Is not it?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.