问题描述
我有几个 JList 都可以作为 DnD COPY 操作的来源和目的地.它们工作得很好,但一方面 - 元素被添加到列表的底部,而不是我想要的放置目标行.
I've got a couple of JList both being able to be sources and destinations of DnD COPY actions. They work fine, but for one thing - elements are added at the bottom of the lists, instead at the drop destination row, as I'd like.
由于 Oracle BasicDnD 示例 包含此功能(事实上,它是我能够通过 Google 找到的唯一应用程序),我一直在查看它的 source 看看我是否能够去适应它.我试图设置 TransferHandler 对象,这是我想我缺少的,但没有出现新的行为.那么,我可能遗漏/做错了什么(其他)?
Since Oracle BasicDnD example includes this feature (and, in fact, it's the only application I've been able to find through Google with it), I've been taking a look at its source to see if I was able to adapt it. I tried to set the TransferHandler object, which is what I guess I'm missing, but no new behavior appeared. So, what (else) may I be missing/doing wrong?
下面显示了我用于这些列表的类.
Below is shown the class I use for these lists.
使用构造函数中的布尔标志是因为,对于其中一个列表,如果丢弃操作被拒绝,则必须删除该元素,但不能在另一个列表中删除该元素.这样我就可以处理了.StringListModel
是 AbstractListModel 来处理字符串列表.它能够在底部和请求的位置添加元素,清除列表并删除某些元素.它做自己的事情,触发相应的事件,仅此而已.
The boolean flag in the constructor is used because, for one of the lists, if the drop action if rejected, the element must be removed, but not in the another. This way I can handle that. StringListModel
is an extension of AbstractListModel to handle list of Strings. It is able to add elements both at bottom and at a requested position, clears the list, and remove certain elements. It does its stuff, fires the corresponding events and nothing further.
下面是我对 MadProgrammer 建议的实现:
Below is shown my implementation of MadProgrammer's suggestion:
打印语句用于在调用方法时获得反馈(现在永远不会).
The print statements are there to get feedback when the methods are called (which is, right now, never).
推荐答案
这是一个使用(较新的)Transferable
API 的完整工作示例.这是基于您链接的示例.
This is a fully working example that uses the (newer) Transferable
API. This is based on the example you have linked.
请注意,我没有使用 String
的麻烦,因为这只会让人们感到困惑,相反,我正在传输对象.
Note, I've not bothered with using String
s, as this just confuses people, instead, I'm transferring objects.
另请注意,我使用的是移动"API,因此名称将从一个列表移动到另一个列表...
Also note, that I'm using a "move" API, so names will be moved from one list to the other...
这篇关于将字符串添加到 JList 的确切位置,而不是在底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!