
Animate JTree Drops #27
Chapter 3, Tables and Trees
|
145
HACK
The special rendering is a two-step process. First, getTreeCellRendererComponent( )
figures out if the cell to be rendered is the drop target, and if so, it sets a
local
boolean
. It sets another
boolean
to indicate that the drop target cell is a
leaf. Having set these
boolean
s, it returns the superclass’s implementation.
In short order, the renderer’s
paint( )
method is called. In the
paint( )
, you
can use the
boolean
s to apply special rendering. In this version, a drop tar-
get that is a leaf gets a line drawn in its top inset, suggesting that the
dropped item will be inserted before this node. If rendering a drop target
that is a branch—i.e., it’s not a leaf—then the special rendering puts a box
around the component.
Running the Code
The main method, shown in Example 3-17, builds a reorderable tree and
puts it in a
JFrame.
Example 3-17. Testing the drag-and-drop JTree
public static void main (String[] args) {
JTree tree = new DnDJTree( );
DefaultMutableTreeNode root = new DefaultMutableTreeNode("People");
DefaultMutableTreeNode set1 = new DefaultMutableTreeNode("Set 1");
DefaultMutableTreeNode set2 = new DefaultMutableTreeNode("Set 2");
DefaultMutableTreeNode set3 = new DefaultMutableTreeNode("Set 3");
set1.add (new DefaultMutableTreeNode ("Chris"));
set1.add (new DefaultMutableTreeNode ("Kelly"));
set1.add (new DefaultMutableTreeNode ("Keagan")); ...