java JPanel中的Java自定义拖放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10923725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Custom Drag and Drop in JPanel
提问by user1441004
Java Custom Drag and Drop - no callbacks to TransferHandler.
Java 自定义拖放 - 没有对 TransferHandler 的回调。
I want to implement Custom Drag and Drop functionality for a JPanel subclass. I following the guidelines of the standard DnD Tutorial:
我想为 JPanel 子类实现自定义拖放功能。我遵循标准 DnD 教程的指导方针:
Drag and Drop and Data Transfer
On the surface, things seem pretty strightforward, but when I actually try it, I get no indication that any DnD behaviour is happening. In fact, none of the methods in my TransferHandler are called.
从表面上看,事情似乎很简单,但当我实际尝试时,我没有任何迹象表明任何 DnD 行为正在发生。事实上,我的 TransferHandler 中的任何方法都没有被调用。
So, let's review what I did...
那么,让我们回顾一下我所做的......
I made my own Container class which declares itself to extend JPanel:
我创建了自己的 Container 类,它声明自己扩展 JPanel:
public class DnDUnitPanel extends JPanel
{
...
}
I copied the ListTransferHandler from this Demo:
我从这个 Demo 中复制了 ListTransferHandler:
renamed the class as DndUnitTransferHandler, trimmed out code that is referring to JList objects, and installed System.out.println() statments on each of the 5 methods in there.
将该类重命名为 DndUnitTransferHandler,删减了引用 JList 对象的代码,并在其中的 5 个方法中的每一个上安装了 System.out.println() 语句。
I then instantiate two different instances of DnDUnitPanel like this:
然后我像这样实例化 DnDUnitPanel 的两个不同实例:
DnDUnitPanel topPanel = new DnDUnitPanel(new GridLayout(0, 4, 6, 6), true);
// topPanel.setDragEnabled(true);
topPanel.setName("Top Panel");
topPanel.setTransferHandler(new DnDUnitTransferHandler());
DnDUnitPanel bottomPanel = new DnDUnitPanel(new GridLayout(0, 4, 6, 6), true);
// bottomPanel.setDragEnabled(true);
bottomPanel.setName("Bottom Panel");
bottomPanel.setTransferHandler(new DnDUnitTransferHandler());
(and I also create some JLabel instances and add() them to the panels (not shown)).
(并且我还创建了一些 JLabel 实例并将它们添加()到面板(未显示))。
When I try to drag a JLabel from one panel to another, nothing happens. So I went back and reread this page:
当我尝试将 JLabel 从一个面板拖到另一个面板时,没有任何反应。所以我回去重读了这个页面:
in particular, what it says about setDragEnabled():
特别是关于 setDragEnabled() 的说明:
turns on drag support. (The default is false.) This method is
defined on each component that supports the drag gesture; the link
takes you to the documentation for JList.
JPanel does not have a setDragEnabled() method. So, I asked myself, what does that really mean: "component that supports the drag gesture"?
JPanel 没有 setDragEnabled() 方法。所以,我问自己,这到底是什么意思:“支持拖动手势的组件”?
I first figured this must mean something that is declared to implement MouseListener and/or MouseMotionListener. I modified DnDUnitPanel to declare that it implements both and provided all the methods. Having done so, I could see that mousePressed(), mouseClicked(), mouseDragged(), etc. were getting called, but still nothing in the TransferHandler was getting called, and still no drag cursor indicating something was dragged or is ready to be dropped.
我首先认为这一定意味着声明了实现 MouseListener 和/或 MouseMotionListener 的东西。我修改了 DnDUnitPanel 以声明它实现了两者并提供了所有方法。这样做后,我可以看到 mousePressed()、mouseClicked()、mouseDragged() 等被调用,但 TransferHandler 中仍然没有任何东西被调用,并且仍然没有拖动光标指示某些东西被拖动或准备好掉了。
I then looked at the source for JList itself and decided that "supports the drag gesture" really just means something that has a 'dragEnabled' property with the associated getter and setter.
然后我查看了 JList 本身的源代码,并决定“支持拖动手势”实际上只是意味着具有关联 getter 和 setter 的“dragEnabled”属性的东西。
So, I declared the property and provided the getter and setter on DnDUnitPanel by just copying code straight from JList itself (thinking maybe something I don't fully understand is calling isDragEnabled() and looking for a true value to initiate DnD behavior.)
因此,我通过直接从 JList 本身复制代码来声明了该属性并在 DnDUnitPanel 上提供了 getter 和 setter(想着也许我不完全理解的是调用 isDragEnabled() 并寻找一个真正的值来启动 DnD 行为。)
Unfortunately, providing those (and uncommenting the calls above to DnDUnitPanel.setDragEnabled()) also had no effect.
不幸的是,提供这些(并取消注释上面对 DnDUnitPanel.setDragEnabled() 的调用)也没有效果。
So... the TransferHandler never gets called(). Obviously, something important is missing here, but I'm not seeing what that might be.
所以...... TransferHandler 永远不会被调用()。显然,这里缺少一些重要的东西,但我不知道那可能是什么。
I'm at a dead end for what to try next.
我对接下来要尝试的东西束手无策。
Anybody see what is missing here?
有人看到这里缺少什么吗?
回答by Xeon
Basically you need a data source.
基本上你需要一个数据源。
Look at the other Stackoverflow question, hereand here(a good explanation of DnD)
看看其他Stackoverflow 问题,这里和这里(对 DnD 的一个很好的解释)