make JTable Drag and Drop - Java Swing

Java examples for Swing:JTable

Description

make JTable Drag and Drop

Demo Code


//package com.java2s;
import javax.swing.*;

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DragSource;

import javax.activation.ActivationDataFlavor;
import javax.activation.DataHandler;

import javax.swing.table.DefaultTableModel;

public class Main {
    public static void makeDnD(final JTable table) {
        table.setTransferHandler(new TransferHandler() {
            private final DataFlavor localObjectFlavor = new ActivationDataFlavor(
                    Integer.class, DataFlavor.javaJVMLocalObjectMimeType,
                    "DnD_table");

            @Override/*from   ww  w .  ja  v a  2  s.  com*/
            protected Transferable createTransferable(JComponent c) {
                return new DataHandler(new Integer(table.getSelectedRow()),
                        localObjectFlavor.getMimeType());
            }

            @Override
            public boolean canImport(TransferHandler.TransferSupport info) {
                boolean b = info.getComponent() == table && info.isDrop()
                        && info.isDataFlavorSupported(localObjectFlavor);
                table.setCursor(b ? DragSource.DefaultMoveDrop
                        : DragSource.DefaultMoveNoDrop);
                return b;
            }

            @Override
            public int getSourceActions(JComponent c) {
                return TransferHandler.COPY_OR_MOVE;
            }

            @Override
            public boolean importData(TransferHandler.TransferSupport info) {
                JTable target = (JTable) info.getComponent();
                JTable.DropLocation dl = (JTable.DropLocation) info
                        .getDropLocation();
                int index = dl.getRow();
                int max = table.getModel().getRowCount();
                if (index < 0 || index > max) {
                    index = max;
                }
                target.setCursor(Cursor
                        .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                try {
                    Integer rowFrom = (Integer) info.getTransferable()
                            .getTransferData(localObjectFlavor);
                    if (rowFrom != -1 && rowFrom != index) {
                        DefaultTableModel model = (DefaultTableModel) table
                                .getModel();
                        Object o = model.getDataVector().get(rowFrom);
                        model.removeRow(rowFrom);

                        if (index > rowFrom) {
                            index--;
                        }

                        model.getDataVector().add(index, o);

                        target.getSelectionModel().addSelectionInterval(
                                index, index);
                        return true;
                    }
                } catch (Exception e) {
                    Thread.getDefaultUncaughtExceptionHandler()
                            .uncaughtException(Thread.currentThread(), e);
                }
                return false;
            }

            @Override
            protected void exportDone(JComponent c, Transferable t, int act) {
                if (act == TransferHandler.MOVE) {
                    table.setCursor(Cursor
                            .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                }
            }
        });
    }
}

Related Tutorials