Search |
||
Top-Level Drop with Swing and Java SE 6Posted by shan_man on September 15, 2006 at 1:06 PM PDT
Although my blog has been quiet for the last few months, it's certainly not for lack of content to share. In reality, this blog that you're reading now is one of three that have been living in various stages of completion in my unposted blogs folder. For many bloggers, I suspect it doesn't work like this. They have something worthwhile to say (we can hope) and they post it. In my case, however, I like to use this space both to keep in touch with you, and also to present technical content that (we can hope) teaches you something new. As such, it takes me a bit longer while I construct a demo, grab screenshots, and prepare the technical discussion. These items I work on in parallel with other blogs and responsibilities. And then, when they're finally ready, I initiate contact.
This brings me to today's installment. I'd like to present details on
a Swing Drag and Drop RFE that we implemented back in build 53 of
JDK 6. This addition to the
platform, tracked under RFE
4519484,
provides support for dropping onto top-level containers, such as The type of application I'm referring to is what I call an "editor"—an application designed for editing one or more documents. These applications include IDEs, image manipulation programs, and editors for many other types of documents. Such an application typically includes, at a minimum: a menu, a toolbar, an area for editing documents, and perhaps a list or other mechanism for switching between open documents. For this demonstration, I've put together just such an arrangement in the form of a simple text editor. Please ensure that you have a recent build of JDK 6 (build 76 or later) installed, and then click here to launch the demo. Note that you'll need to accept a security dialog upon launch, as an editor application just isn't useful without permission to read files. Rest assured that the demo opens only the files you tell it to, and only for reading; it has no write capability. The following screen shot shows what the application looks like when it's launched:
As you can see, this demo application contains all of the components that I've mentioned above.
In addition, it also adds something that is particularly useful in this type of
application—drop support for opening files! By allowing drops of files, the user is saved
from having to navigate through a file dialog to open files and instead they can drag files from
their native file system directly to the application for editing. Go ahead and try it now:
drag a file (or a few) from your native desktop or file system, and drop into the document area
at the right. The
Supporting drops of files like this is extremely easy. It requires only that you create
a simple
Once you have this
Notice that the only location that accepts drops is the document area. This makes sense,
of course, since the
Also notice that it's actually only a subset
of the document area that accepts file drops; those areas that are covered by an
editor component do not. The reason for this is that the text area component that we're
using as an editor has it's own default
For starters, let's take advantage of the new Java SE 6 support, change one line of code,
and simply change our
Drops are now accepted on almost every area of the application, including the menu bar,
toolbar, and even the frame's title bar! In fact, drops are accepted on any area that isn't
covered by a component with its own
Like the text area component, the document selector at the left (a
Fantastic! There's a caveat here, however. While we've decided that it's acceptable to
remove the default
The correct
solution, for the time being, is to instead provide the text components with their own
custom
Before we conclude, I'd like to take you one step further into providing a better user experience
for file drops. If you haven't noticed yet, I'd like to point out that when you drag over the
demo, the mouse cursor changes to the drag and drop "move" cursor. This doesn't exactly make sense,
since you're not really moving the file into the editor. In playing with native applications that
support file drops, I've discovered that they all tend to use "copy" as a better representation
of this situation. That is, when a file is dragged from the file system, where both the "move" and
"copy" actions are supported, the applications I've inspected typically chose "copy".
While, in Java, the results of the drop are identical, it seems worthwhile to provide user
feedback consistent with what users expect. To do so requires that only a few additional
lines be added to the
You can try this out with the demo now. From the "Demo" menu, please select "Use COPY Action", and then try dragging over the application again. You'll now see, as illustrated in the following screen shot, that the drag and drop "copy" cursor is used.
And there you have it—top-level drop support with Java SE 6. So what am I planning next? Well, I'm right on the heels of another visit to St. Petersburg, Russia and I hope to come home with more pictures and stories to share. And I'm definitely in for many hours of flight time, which gives me the chance to stop dragging my feet and finish some of my other blogs. Until then, please take care, and feel free to share your questions and comments. »
Related Topics >>
Swing Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|