4

I'm using JTable + JScrollPane + DefaultTableModel.

I need to implement Drag-n-Drop on JTableHeader. I want to drag a column header to my component and do some work depending on the column dragged.

I've tried setTransferHandler() on my JTableHeader but dragging is still not working.

Where can I start? Is it possible to implement DnD on JTableHeader and save the "move-columns" feature (available 'out-of-the-box' in JTable)?

aymeric
  • 3,877
  • 2
  • 28
  • 42
Afel
  • 71
  • 5
  • 1
    Specify "not working": you cannot initiate a drag (not even the move-columns), you can move columns but not drop on your component, you can drag-and-drop but the drop is not doing what you expected, ... – Robin Sep 19 '12 at 06:14
  • I can rearrange columns but I can't initiate a drag (cursor is not changing, drop-methods of my component does not fire on mouse-button-release) – Afel Sep 19 '12 at 07:03
  • you may consider to put your update as an answer to your question (and accept it), easier to find for future users with the signal "solved" :-) – kleopatra Sep 19 '12 at 08:00
  • Thanks @kleopatra . Too bad I can accept my own answer only after 2 days. Will check it later. – Afel Sep 19 '12 at 08:34

1 Answers1

3

SOLVED: Question closed. I've implemented dnd using

DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(tableHeader, ...)  

One thing to mention: dnd breaks default 'rearrange columns' behavior. My workaround was

    public void dragGestureRecognized(DragGestureEvent dge) {
        if (dge.getDragAction() == DnDConstants.ACTION_COPY)
            return;
        try {
            dge.startDrag(null, new MyTransferable());
        } catch (InvalidDnDOperationException e2) {
            System.out.println(e2);
        }
    }

so that I can rearrange columns with ctrl-pressed. It's enough for me.

Afel
  • 71
  • 5