启用只是在Swing具体的目标降 [英] Enable drop just for specific targets in Swing
问题描述
我想实现拖放放大器;在java中删除与两个 JList的
实例。
基本流程工作正常。然而,当我从一个列表拖动一个字符串,我想限制放置目标只针对第二个列表。
我注意到,当我拖着一个字符串从一个列表到我的桌面,因此创建一个包含该字符串的文件。
有什么办法避免这种情况?
公共类SampleDnD扩展的JFrame { 公共静态无效的主要(字串[] args){
新SampleDnD(); }
/ **
*框架样的新形式包含2 JList的启用拖累。
* /
公共SampleDnD(){
JList的L1 =新的JList();
JList的L2 =新的JList();
JScrollPane的jScrollPane1 =新javax.swing.JScrollPane中的();
JScrollPane的jScrollPane2 =新javax.swing.JScrollPane中的();
DefaultListModel listModel1 =新DefaultListModel();
DefaultListModel listModel2 =新DefaultListModel(); 的String [] list1的=新的String [] {1,3,5,7,9};
串[]列表2 =新串[] {0,2,4,6,8};
对于(INT指数= 0;指数 - LT; list1.length;指数++){
listModel1.add(索引,list1的[指数]);
listModel2.add(索引列表2 [指数]);
}
l1.setModel(listModel1);
l2.setModel(listModel2);
l1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
l2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
l1.setDropMode(DropMode.INSERT);
l1.setDragEnabled(真);
l2.setDragEnabled(真);
l1.setTransferHandler(新ListTransferHandler());
l2.setDropMode(DropMode.INSERT);
l2.setTransferHandler(新ListTransferHandler());
jScrollPane1.setViewportView(L1);
jScrollPane2.setViewportView(12); 容器CP =的getContentPane();
cp.setLayout(新的BoxLayout(CP,BoxLayout.Y_AXIS));
集preferredSize(新尺寸(800500));
的getContentPane()加(jScrollPane1)。
的getContentPane()加(jScrollPane2)。
调用setVisible(真);
包(); }
公共类ListTransferHandler扩展的TransferHandler {
/ **
*我们只支持导入的字符串。
* /
公共布尔canImport(TransferHandler.TransferSupport资讯){
//检查串味
如果(!info.isDataFlavorSupported(选择DataFlavor.stringFlavor)){
返回false;
}
返回true;
} / **
*捆绑起来在一个列表中选定的项目用于出口。
*
* /
保护转换createTransferable(JComponent c)在{
JList的名单=(JList的)C;
字符串值=(字符串)list.getSelectedValue();
返回新StringSelection(值);
} / **
*我们只支持移动操作。
* /
公众诠释getSourceActions(JComponent c)在{
返回TransferHandler.MOVE;
}
/ **
*执行实际进口。此演示仅支持拖放。
* /
公共布尔IMPORTDATA(TransferHandler.TransferSupport资讯){
如果(!info.isDrop()){
返回false;
}
JList的名单=(JList的)info.getComponent();
DefaultListModel的ListModel =(DefaultListModel)list.getModel();
JList.DropLocation DL =(JList.DropLocation)info.getDropLocation();
INT指数= dl.getIndex();
布尔插入= dl.isInsert();
//获取正在被丢弃的字符串。
转让T = info.getTransferable();
字符串数据;
尝试{
数据=(字符串)t.getTransferData(选择DataFlavor.stringFlavor);
}
赶上(例外五){返回false; } 如果(插入){
listModel.add(索引++数据);
}其他{
//如果项目超越当前的结尾
//列表,添加它们。
如果(指数< listModel.getSize()){
listModel.set(索引++数据);
}其他{
listModel.add(索引++数据);
}
}
返回true;
}
/ **
*从列表中删除移动的项目。
* /
保护无效exportDone(JComponent的C,传输的数据,诠释动作){
JList的源=(JList的)C;
DefaultListModel的ListModel =(DefaultListModel)source.getModel();
如果(动作== TransferHandler.MOVE)
{
listModel.remove(source.getSelectedIndex());
}
} }
}
有点晚了,但对于别人,也许有所帮助:
您需要创建自己的DatFlavor,这只能通过自己的应用所接受。
的DataFlavor myObjectDataFlavor =新的DataFlavor(MyObject.class,爪哇/ MyObject来);
通过这一个,而不是默认的 DataFlavor.javaFileListFlavor
或选择DataFlavor.stringFlavor
。类为MyObject
是只包含一个String类型的类变量或整数或任何你需要一个POJO。
I'm trying to implement drag & drop in java with two JList
instances.
The basic flow works fine. However, when I'm dragging a string from one list, I want to restrict the drop target only for the second list.
I noticed that when I'm dragging a string from one list to my desktop so it creates a file containing this string.
Is there any way to avoid such situations?
public class SampleDnD extends JFrame{
public static void main(String[] args) {
new SampleDnD();
}
/**
* new form of frame sample contain 2 JLists with drag enabled.
*/
public SampleDnD(){
JList l1 = new JList();
JList l2 = new JList();
JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
DefaultListModel listModel1 = new DefaultListModel();
DefaultListModel listModel2 = new DefaultListModel();
String[] list1 = new String[]{"1","3","5","7","9"};
String [] list2 = new String[]{"0","2","4","6","8"};
for(int index=0;index<list1.length;index++){
listModel1.add(index, list1[index]);
listModel2.add(index, list2[index]);
}
l1.setModel(listModel1);
l2.setModel(listModel2);
l1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
l2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
l1.setDropMode(DropMode.INSERT);
l1.setDragEnabled(true);
l2.setDragEnabled(true);
l1.setTransferHandler(new ListTransferHandler());
l2.setDropMode(DropMode.INSERT);
l2.setTransferHandler(new ListTransferHandler());
jScrollPane1.setViewportView(l1);
jScrollPane2.setViewportView(l2);
Container cp = getContentPane();
cp.setLayout(new BoxLayout(cp,BoxLayout.Y_AXIS));
setPreferredSize(new Dimension(800,500));
getContentPane().add(jScrollPane1);
getContentPane().add(jScrollPane2);
setVisible(true);
pack();
}
public class ListTransferHandler extends TransferHandler {
/**
* We only support importing strings.
*/
public boolean canImport(TransferHandler.TransferSupport info) {
// Check for String flavor
if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return false;
}
return true;
}
/**
* Bundle up the selected items in a single list for export.
*
*/
protected Transferable createTransferable(JComponent c) {
JList list = (JList)c;
String value = (String)list.getSelectedValue();
return new StringSelection(value);
}
/**
* We support only move actions.
*/
public int getSourceActions(JComponent c) {
return TransferHandler.MOVE;
}
/**
* Perform the actual import. This demo only supports drag and drop.
*/
public boolean importData(TransferHandler.TransferSupport info) {
if (!info.isDrop()) {
return false;
}
JList list = (JList)info.getComponent();
DefaultListModel listModel = (DefaultListModel)list.getModel();
JList.DropLocation dl = (JList.DropLocation)info.getDropLocation();
int index = dl.getIndex();
boolean insert = dl.isInsert();
// Get the string that is being dropped.
Transferable t = info.getTransferable();
String data;
try {
data = (String)t.getTransferData(DataFlavor.stringFlavor);
}
catch (Exception e) { return false; }
if (insert) {
listModel.add(index++, data);
} else {
// If the items go beyond the end of the current
// list, add them in.
if (index < listModel.getSize()) {
listModel.set(index++, data);
} else {
listModel.add(index++, data);
}
}
return true;
}
/**
* Remove the items moved from the list.
*/
protected void exportDone(JComponent c, Transferable data, int action) {
JList source = (JList)c;
DefaultListModel listModel = (DefaultListModel)source.getModel();
if(action == TransferHandler.MOVE)
{
listModel.remove(source.getSelectedIndex());
}
}
}
}
A little late, but maybe helpful for somebody else: You need to create your own DatFlavor, that will only be accepted by your own application.
DataFlavor myObjectDataFlavor = new DataFlavor(MyObject.class, "java/MyObject");
Pass this one instead of a default DataFlavor.javaFileListFlavor
or DataFlavor.stringFlavor
. Your class MyObject
is a Pojo containing only a class variable of type String or an integer or whatever you need.
这篇关于启用只是在Swing具体的目标降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!