How do I drag and drop a row in a JTable?

前端 未结 5 1553
既然无缘
既然无缘 2020-12-08 01:03

How do you setup a JTable to be able to drag a row to a different index in the table. For example if I have 5 rows and I want to drag the 4th row to the 2nd position?

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 01:27

    perhaps sth. like this:

        table.addMouseMotionListener(new MouseMotionListener() {
        public void mouseDragged(MouseEvent e) {
            e.consume();
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.MOVE);
        }
    
        public void mouseMoved(MouseEvent e) {
        }
    });
    

提交回复
热议问题