Finishing Touches

With the DnD API, we can add a few more features to help us achieve the goal of user interface maturity. Since we can’t drop our objects on other leaves, we really should keep the drag cursor consistent. If we’re over a folder, we can accept the drag and show the “ok to drop here” cursor. If we’re anywhere else, we’ll reject the drag and show the “no drop” version. The current implementation of our handlers allows only nodes to be moved. We can include support for “copy or move” drags. (We can also keep our cursor in sync with the new support so users know whether they’re copying or moving.) Once all this is working, we should also set up autoscrolling on our tree to give the user access to any off-screen parts of the tree.

Dynamic Cursors

If your application can use the new DnD support built into the 1.4 release, you shouldn’t have to worry about cursor management. If you’re using one of the older SDKs or building fancier custom support, read on.

The DnD package ships with some standard cursors for indicating “ok to drop” and “not ok to drop.” These cursors are displayed automatically when you accept( ) or reject( ) an item as it is dragged over potential drop targets. Here’s a look at the dragEnter( ) and dragOver( ) event handlers that check to see whether the mouse is over a folder in the tree. If it’s not, we reject the drag. This rejection causes the cursor to update its appearance. (Note that with the new cursor features in Java 2, you can define your own ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.