The Java Tutorials have been written for JDK 8.Java教程是为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.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Technically speaking, the framework for drag and drop supports all Swing components — the data transfer mechanism is built into every JComponent
. If you wanted, you could implement drop support for a JSlider
so that it could fully participate in data transfer. While JSlider
does not support drop by default, the components you would want (and expect) to support drag and drop do provide specialized built-in support.
The following components recognize the drag gesture once the setDragEnabled(true)
method is invoked on the component. For example, once you invoke myColorChooser.setDragEnabled(true)
you can drag colors from your color chooser:
JColorChooser
JEditorPane
JFileChooser
JFormattedTextField
JList
JTable
JTextArea
JTextField
JTextPane
JTree
The following components support drop out of the box. If you are using one of these components, your work is done.
JEditorPane
JFormattedTextField
JPasswordField
JTextArea
JTextField
JTextPane
JColorChooser
The framework for drop is in place for the following components, but you need to plug in a small amount of code to customize the support for your needs.
JList
JTable
JTree
For these critical components, Swing performs the drop location calculations and rendering; it allows you to specify a drop mode; and it handles component specific details, such as tree expansions. Your work is fairly minimal.
You can also install drop support on top-level containers, such as JFrame
and JDialog
. You can learn more about this in Top-Level Drop.