在两个Jtable之间拖放

时间:2020-05-17 08:53:46

标签: java swing drag-and-drop jtable

因此,我设法使Jtable从table1到table2以一种方式工作。但是我真的不知道如何使它同时工作(从table2到table1)。我试图将transferhandler放在两个Jtables上,但是我想它们相互干扰。所以我很困。有人可以解释我该怎么做吗? 这是实现(当我尝试对第二个Jtable执行相同操作时,它不起作用):

table1.setDragEnabled(true);
table2.setTransferHandler(new TransferHandler() {
            public boolean canImport(TransferSupport support) {
                if (!support.isDrop()) {
                    return false;
                }

                if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    return false;
                }
                return true;
            }

            public boolean importData(TransferSupport support) {
                if (!canImport(support)) {
                    return false;
                }
                int row2 = table1.getSelectedRow();
                int rows[] = table1.getSelectedRows();
                if (rows.length > 1){
                    Object strings2[][] = new String[rows.length][2];
                    for (int i=0; i<rows.length; i++) {
                        for (int j = 0; j < 2; j++)
                            strings2[i][j] = table1.getValueAt(rows[i], j);
                    }
                    String pathToCopy, pathToPaste;
                    for (int k=0; k<strings2.length; k++) {
                        if (path.equals("C:\\"))
                            pathToCopy = text1.getText() + trimmer(strings2[k][0].toString());
                        else
                            pathToCopy = text1.getText() + "\\" + trimmer(strings2[k][0].toString());
                        if (path2.equals("C:\\"))
                            pathToPaste = path2 + trimmer(strings2[k][0].toString());
                        else
                            pathToPaste = text2.getText() + "\\" + trimmer(strings2[k][0].toString());
                        File toCopy = new File(pathToCopy);
                        File toPaste = new File(pathToPaste);
                        try {
                            copyPaste(toCopy, toPaste);
                        } catch (IOException e) {
                            System.out.println("Nie można skopiować pliku!");
                        }
                    }
                    return true;
                }
                else {
                    if (row2 != -1) {
                        for (int i = 0; i < 2; i++)
                            strings[i] = table1.getValueAt(row2, i);
                    }
                    String pathToCopy, pathToPaste;
                    if (path.equals("C:\\"))
                        pathToCopy = text1.getText() + trimmer(strings[0].toString());
                    else
                        pathToCopy = text1.getText() + "\\" + trimmer(strings[0].toString());
                    if (path2.equals("C:\\"))
                        pathToPaste = path2 + trimmer(strings[0].toString());
                    else
                        pathToPaste = text2.getText() + "\\" + trimmer(strings[0].toString());
                    File toCopy = new File(pathToCopy);
                    File toPaste = new File(pathToPaste);
                    try {
                        copyPaste(toCopy, toPaste);
                    } catch (IOException e) {
                        System.out.println("Nie można skopiować pliku!");
                    }
                    return true;
                }
            }
        }); 

0 个答案:

没有答案