TITLE:TransferHandlerを使ってJTableの行をドラック&ドロップ、並べ替え

Posted by terai at 2009-09-07

TransferHandlerを使ってJTableの行をドラック&ドロップ、並べ替え

JTableの行を複数選択し、ドラック&ドロップで並べ替えを可能にするTransferHandlerを作成します。

  • &jnlp;
  • &jar;
  • &zip;

#screenshot

サンプルコード

JTable table = new JTable(model);
table.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setTransferHandler(new TableRowTransferHandler());
table.setDropMode(DropMode.INSERT_ROWS);
table.setDragEnabled(true);

解説

上記のサンプルのTransferHandler*1は、TransferHandlerを使ったJListのドラック&ドロップによる並べ替えのものとほぼ同じです。


JTableの行をドラッグ&ドロップとは異なり、複数行を選択してDrag&Dropによる移動が可能になっています。

参考リンク

コメント

  • テスト -- terai
//http://java.sun.com/docs/books/tutorial/uiswing/examples/dnd/index.html
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.activation.*;
import javax.swing.*;
import javax.swing.table.*;
public class DnDTableTest{
  TransferHandler handler = new TableRowTransferHandler();
  String[] columnNames = {"String", "Integer", "Boolean"};
  Object[][] data = {
    {"AAA",12,true},{"aaa",1,false},{"BBB",13,true},{"bbb",2,false},
  };
  private JTable makeDnDTable() {
    JTable t = new JTable(new DefaultTableModel(data, columnNames) {
      @Override public Class<?> getColumnClass(int column) {
        return getValueAt(0, column).getClass();
      }
    });
    t.getSelectionModel().setSelectionMode(
        ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    t.setTransferHandler(handler);
    t.setDropMode(DropMode.INSERT_ROWS);
    t.setDragEnabled(true);
    t.setFillsViewportHeight(true);
    return t;
  }
  public JComponent makeUI() {
    JPanel p = new JPanel(new GridLayout(2,1));
    p.add(new JScrollPane(makeDnDTable()));
    p.add(new JScrollPane(makeDnDTable()));
    p.setBorder(BorderFactory.createTitledBorder("Drag & Drop JTable"));
    p.setPreferredSize(new Dimension(320, 320));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() { createAndShowGUI(); }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new DnDTableTest().makeUI());
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}
class TableRowTransferHandler extends TransferHandler {
  private int[] rows    = null;
  private int addIndex  = -1; //Location where items were added
  private int addCount  = 0;  //Number of items added.
  private final DataFlavor localObjectFlavor;
  private Object[] transferedObjects = null;
  private JComponent source = null;
  public TableRowTransferHandler() {
    localObjectFlavor = new ActivationDataFlavor(
      Object[].class, DataFlavor.javaJVMLocalObjectMimeType, "Array of items");
  }
  @Override protected Transferable createTransferable(JComponent c) {
    source = c;
    JTable table = (JTable) c;
    DefaultTableModel model = (DefaultTableModel)table.getModel();
    ArrayList<Object> list = new ArrayList<Object>();
    for(int i: rows = table.getSelectedRows())
      list.add(model.getDataVector().elementAt(i));
    transferedObjects = list.toArray();
    return new DataHandler(transferedObjects,localObjectFlavor.getMimeType());
  }
  @Override public boolean canImport(TransferHandler.TransferSupport info) {
    JTable t = (JTable)info.getComponent();
    boolean b = info.isDrop()&&info.isDataFlavorSupported(localObjectFlavor);
    //XXX bug?
    t.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();
    DefaultTableModel model = (DefaultTableModel)target.getModel();
    int index = dl.getRow();
    int max = model.getRowCount();
    if(index<0 || index>max) index = max;
    addIndex = index;
    target.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    try {
      Object[] values =
        (Object[])info.getTransferable().getTransferData(localObjectFlavor);
      if(source==target) addCount = values.length;
      for(int i=0;i<values.length;i++) {
        int idx = index++;
        model.insertRow(idx, (Vector)values[i]);
        target.getSelectionModel().addSelectionInterval(idx, idx);
      }
      return true;
    }catch(Exception ufe) { ufe.printStackTrace(); }
    return false;
  }
  @Override protected void exportDone(JComponent c, Transferable t, int act) {
    cleanup(c, act == TransferHandler.MOVE);
  }
  private void cleanup(JComponent src, boolean remove) {
    if(remove && rows != null) {
      JTable table = (JTable)src;
      src.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
      DefaultTableModel model = (DefaultTableModel)table.getModel();
      if(addCount > 0) {
        for(int i=0;i<rows.length;i++) {
          if(rows[i]>=addIndex) { rows[i] += addCount; }
        }
      }
      for(int i=rows.length-1;i>=0;i--) model.removeRow(rows[i]);
    }
    rows     = null;
    addCount = 0;
    addIndex = -1;
  }
}