Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JList中的拖放操作不起作用_Java_Swing_Jlist_Drag And Drop - Fatal编程技术网

Java JList中的拖放操作不起作用

Java JList中的拖放操作不起作用,java,swing,jlist,drag-and-drop,Java,Swing,Jlist,Drag And Drop,因此,我为我的应用程序编写了一个TransferHandler(在Oracle站点的示例中),但当我尝试移动数据时,它并没有移动。它所做的只是将索引处的数据(n-1,即,如果我正在将项移动到n个位置)复制到索引n。尽管我尝试了很多选择,但没有一个对我有效,你能检查一下有什么问题吗 import javax.swing.TransferHandler; import javax.swing.*; import java.awt.datatransfer.*; public class L

因此,我为我的应用程序编写了一个TransferHandler(在Oracle站点的示例中),但当我尝试移动数据时,它并没有移动。它所做的只是将索引处的数据(n-1,即,如果我正在将项移动到n个位置)复制到索引n。尽管我尝试了很多选择,但没有一个对我有效,你能检查一下有什么问题吗

import javax.swing.TransferHandler;
import javax.swing.*;
import java.awt.datatransfer.*;

    public class ListTransferHandler extends TransferHandler {
        private int[] indices = null;
        private int addIndex = -1; //Location where items were added
        private int addCount = 0;  //Number of items added.

        public boolean canImport(TransferHandler.TransferSupport info) {
            // Check for String flavor
            if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }
            return true;
        }

        protected Transferable createTransferable(JComponent c) {
            return new StringSelection(exportString(c));
        }

        public int getSourceActions(JComponent c) {
            return TransferHandler.MOVE;
        }

        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; }

            // Perform the actual import.  
            if (insert) {
                listModel.add(index, data);
            } else {
                listModel.set(index, data);
            }
            return true;
        }

        protected void exportDone(JComponent c, Transferable data, int action) {
            cleanup(c, action == TransferHandler.MOVE);
        }

        //Bundle up the selected items in the list
        //as a single string, for export.
        protected String exportString(JComponent c) {
            JList list = (JList)c;
            indices = list.getSelectedIndices();
            Object[] values = list.getSelectedValues();

            StringBuffer buff = new StringBuffer();

            for (int i = 0; i < values.length; i++) {
                Object val = values[i];
                buff.append(val == null ? "" : val.toString());
                if (i != values.length - 1) {
                    buff.append("\n");
                }
            }

            return buff.toString();
        }

        //Take the incoming string and wherever there is a
        //newline, break it into a separate item in the list.
        protected void importString(JComponent c, String str) {
            JList target = (JList)c;
            DefaultListModel listModel = (DefaultListModel)target.getModel();
            int index = target.getSelectedIndex();

            //Prevent the user from dropping data back on itself.
            //For example, if the user is moving items #4,#5,#6 and #7 and
            //attempts to insert the items after item #5, this would
            //be problematic when removing the original items.
            //So this is not allowed.
            if (indices != null && index >= indices[0] - 1 &&
                  index <= indices[indices.length - 1]) {
                indices = null;
                return;
            }

            int max = listModel.getSize();
            if (index < 0) {
                index = max;
            } else {
                index++;
                if (index > max) {
                    index = max;
                }
            }
            addIndex = index;
            String[] values = str.split("\n");
            addCount = values.length;
            for (int i = 0; i < values.length; i++) {
                listModel.add(index++, values[i]);
            }
        }

        //If the remove argument is true, the drop has been
        //successful and it's time to remove the selected items 
        //from the list. If the remove argument is false, it
        //was a Copy operation and the original list is left
        //intact.
        protected void cleanup(JComponent c, boolean remove) {
            if (remove && indices != null) {
                JList source = (JList)c;
                DefaultListModel model  = (DefaultListModel)source.getModel();
                //If we are moving items around in the same list, we
                //need to adjust the indices accordingly, since those
                //after the insertion point have moved.
                if (addCount > 0) {
                    for (int i = 0; i < indices.length; i++) {
                        if (indices[i] > addIndex) {
                            indices[i] += addCount;
                        }
                    }
                }
                for (int i = indices.length - 1; i >= 0; i--) {
                    model.remove(indices[i]);
                }
            }
            indices = null;
            addCount = 0;
            addIndex = -1;
        }
}
import javax.swing.TransferHandler;
导入javax.swing.*;
导入java.awt.datatransfer.*;
公共类ListTransferHandler扩展了TransferHandler{
私有int[]索引=null;
private int addIndex=-1;//添加项的位置
private int addCount=0;//添加的项目数。
公共布尔值canImport(TransferHandler.TransferSupport信息){
//检查字符串的味道
如果支持(!info.isDataFlavor(DataFlavor.stringFlavor)){
返回false;
}
返回true;
}
受保护的可转让组件(JComponent c){
返回新的StringSelection(exportString(c));
}
public int getSourceActions(JComponent c){
返回TransferHandler.MOVE;
}
公共布尔输入数据(TransferHandler.TransferSupport信息){
如果(!info.isDrop()){
返回false;
}
JList=(JList)info.getComponent();
DefaultListModel listModel=(DefaultListModel)list.getModel();
JList.DropLocation dl=(JList.DropLocation)info.getDropLocation();
int index=dl.getIndex();
布尔插入=dl.isInsert();
//获取正在删除的字符串。
可转移t=info.getTransferable();
字符串数据;
试一试{
数据=(字符串)t.getTransferData(DataFlavor.stringFlavor);
} 
catch(异常e){return false;}
//执行实际导入。
如果(插入){
添加(索引、数据);
}否则{
set(索引、数据);
}
返回true;
}
受保护的void exportDone(JComponent c、可转移数据、int操作){
清理(c,action==TransferHandler.MOVE);
}
//将列表中选定的项目捆绑起来
//作为单个字符串,用于导出。
受保护字符串exportString(JComponent c){
JList=(JList)c;
index=list.getSelectedIndices();
Object[]values=list.getSelectedValues();
StringBuffer buff=新的StringBuffer();
对于(int i=0;i=索引[0]-1&&
索引(最大值){
指数=最大值;
}
}
addIndex=指数;
字符串[]值=str.split(“\n”);
addCount=values.length;
对于(int i=0;i0){
对于(int i=0;i附加索引){
索引[i]+=addCount;
}
}
}
对于(int i=index.length-1;i>=0;i--){
模型。删除(索引[i]);
}
}
索引=空;
addCount=0;
addIndex=-1;
}
}
导出成功后,您需要从源中“删除”数据

为此,需要重写该方法

受保护的void exportDone(JComponent源、, 可转移数据, 国际行动)

在导出数据后调用。此方法应删除 移动操作时传输的数据

由于不支持移动,因此实现此方法时不执行任何操作 此实现的操作(getSourceActions不包括 移动)

基本上,您需要从那里检查
操作
类型,并确保目标组件已接受
移动
,而不仅仅是
C