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
Section: Default DnD Support
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.

Demo - BasicDnD

Now we will look at a simple demo, calledBasicDnD, that shows you what you get for free. As you see from the screen shot, BasicDnD contains a table, a list, a tree, a color chooser, a text area, and a text field.

All of these components are standard out-of-the-box componentsexcept for the list. This list has been customized to bring up a dialog showing where the drop would occur, if it accepted drops.

The following areas accept drops:

By default, none of the objects has default drag and drop enabled. At startup, you can check the "Turn on Drag and Drop" check box to see what drag and drop behavior you get for free.

Basic Drag and Drop behavior
This figure has been reduced to fit on the page.
Click the image to view it at its natural size.

Try this: 
  1. Click the Launch button to run BasicDnD usingJava™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult theexample index.Launches the BasicDnD example
  2. Select an item in the list and, while holding down the mouse button, begin to drag. Nothing happens because the drag has not yet been enabled on the list.
  3. Select the "Turn on Drag and Drop" check box.
  4. Press the selected item in the list and begin to drag. Drop the text back onto the list. A dialog shows where the text would appear if the list actually accepted drops. (The default behavior for a list would be to show a "does not accept data" cursor.)
  5. Drag the selected text over a text area. The insertion point for the text is indicated by a blinking caret. Also, the cursor changes to indicate that the text area will accept the text as a copy.
  6. Release the mouse and watch the text appear in the text area.
  7. Select some text in one of the text areas.
  8. Press the mouse button while the cursor is over the selected text and begin to drag.
  9. Note that this time, the cursor for a drag action appears. Successfully dropping this text into another component will cause the text to be removed from the original component.
  10. Hold the Control key down and press again on the selected text. Begin dragging and the copy cursor now appears. Move the cursor over the text area and drop. The text appears in the new location but is not removed from the original location. The Control key can be used to change any Move to a Copy.
  11. Select a color from the color chooser. The selected color appears in the Preview panel. Press and hold the mouse button over the color in the Preview panel and drag it over the other components. Note that none of these components accepts color.
  12. Try dropping text, color, and even files, onto the list. A dialog will report the attempted action. The actual drop can be implemented with an additional six lines of code that have been commented out in theBasicDnD.java source file.

Next we will look at theTransferHandler class, the workhorse of the drag and drop mechanism.

« 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: Default DnD Support
Next page: TransferHandler Class

[8]ページ先頭

©2009-2025 Movatter.jp