Drag-n-Drop JTableHeader

I am using JTable + JScrollPane + DefaultTableModel .

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

I tried setTransferHandler() on my JTableHeader , but drag and drop still doesn't work.

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

+4
source share
1 answer

SOLVED . The question is closed. I implemented dnd using

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

One note: dnd violates the default behavior of columns. 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 the columns with the Ctrl key pressed. This is enough for me.

+3
source

Source: https://habr.com/ru/post/1434991/


All Articles