Movatterモバイル変換


[0]ホーム

URL:


Documentation

The Java™ Tutorials
Drag and Drop and Data Transfer
Introduction to DnD
Default DnD Support
Demo - BasicDnD
TransferHandler Class
Export Methods
Import Methods
TransferSupport Class
Setting the Drop Mode
Demo - DropDemo
Choosing the Drop Action
Demo - ChooseDropAction
Showing the Drop Location
Location Sensitive Drop
Demo - LocationSensitiveDemo
Empty Table Drop
Drop Location Rendering
Top-Level Drop
Adding Cut, Copy and Paste (CCP)
CCP in a Text Component
CCP in a non-Text Component
Using and Creating a DataFlavor
Putting it All Together - DnD and CCP
Solving Common Data Transfer Problems
Trail: Creating a GUI With Swing
Lesson: Drag and Drop and Data Transfer
Home Page >Creating a GUI With Swing >Drag and Drop and Data Transfer
« Previous • Trail • Next »

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Location Sensitive Drop

Sometimes you have a complex component and you want the user to be able to drop on some parts of it, but not on others. Perhaps it is a table that allows data to be dropped only in certain columns; or perhaps it is a tree that allows data to be dropped only on certain nodes. Obviously you want the cursor to provide accurate feedback — it should only show the drop location when it is over the specific part of the component that accepts drops.

This is simple to accomplish by installing the necessary logic in thecanImport(TransferHandler.TransferSupport) method of theTransferHandler class. It works only with this particular version ofcanImport because it is called continuously while the drag gesture is over the bounds of the component. When this method returns true, Swing shows the drop cursor and the drop location is visually indicated; when this method returns false, Swing shows the "no-drag" cursor and the drop location is not displayed.

For example, imagine a table that allows drop, but not in the first column. ThecanImport method might look something like this:

public boolean canImport(TransferHandler.TransferSupport info) {    // for the demo, we will only support drops (not clipboard paste)    if (!info.isDrop()) {        return false;    }    // we only import Strings    if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {        return false;    }    // fetch the drop location    JTable.DropLocation dl = (JTable.DropLocation)info.getDropLocation();    int column = dl.getColumn();    // we do not support invalid columns or the first columnif (column == -1 || column == 0) {        return false;    }    return true;}

The code displayed in bold indicates the location-sensitive drop logic: When the user drops the data in such a way that the column cannot be calculated (and is therefore invalid) or when the user drops the text in the first column, thecanImport method returns false — so Swing shows the "no-drag" mouse cursor. As soon as the user moves the mouse off the first columncanImport returns true and Swing shows the drag cursor.

Next, we show a demo of a tree that has implemented location-sensitive drop.

« PreviousTrailNext »

About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights

Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.

Previous page: Showing the Drop Location
Next page: Demo - LocationSensitiveDemo

[8]ページ先頭

©2009-2025 Movatter.jp