我想用O.Reilly-Swing.Hacks Hack 69. Translucent Drag-and-Drop的预览实现一个简单的JComponent-Drag-and-Drop。
我的问题是,如果TransferHandler启动“拖动”,MouseMotionListener停止执行mouseDragged()。

这是一些示例代码:

一个带有绿色和红色侧面的小窗口。
绿色的Side不会开始拖动,始终执行mouseDragged(),但永远不会达到exportDone()。

红边通过exportAsDrag()启动拖动,但是之后mouseDragged()将不再起作用。

public class Drag extends JPanel implements Transferable, MouseMotionListener, MouseListener {
public Drag() {
    this.setTransferHandler( new TransferHandler() {
        @Override
        protected Transferable createTransferable( JComponent c ) {
            return (Drag)c;
        }
        @Override
        public boolean canImport( JComponent comp, DataFlavor[] transferFlavors ) {
            return false;
        }
        @Override
        public int getSourceActions( JComponent c ) {
            return MOVE;
        }
        @Override
        protected void exportDone( JComponent source, Transferable data, int action ) {
            super.exportDone( source, data, action );
            System.out.println( "done" );
        }
    } );
    this.setPreferredSize( new Dimension( 200, 100 ) );
    this.addMouseMotionListener( this );
    this.addMouseListener( this );
}
@Override
public void mouseDragged( MouseEvent e ) {
    System.out.println( "drag" );
}
@Override
public void mouseMoved( MouseEvent e ) { }
@Override
public void mousePressed( MouseEvent e ) {
    if( e.getX() > getWidth() / 2 ) {
        System.out.println( "EXPORT" );
        this.getTransferHandler().exportAsDrag( this, e, TransferHandler.MOVE );
    } else {
        System.out.println( "no Export" );
    }
}
@Override
public void paint( Graphics g ) {
    super.paint( g );
    g.setColor( Color.GREEN );
    g.fillRect( 0, 0, getWidth() / 2, getHeight() );
    g.setColor( Color.RED );
    g.fillRect( getWidth() / 2, 0, getWidth(), getHeight() );
}
public boolean isDataFlavorSupported( DataFlavor flavor ) {
    return false;
}
public DataFlavor[] getTransferDataFlavors() {
    return new DataFlavor[] {};
}
public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException, IOException {
    return new Object();
}
@Override
public void mouseClicked( MouseEvent e ) { }
@Override
public void mouseEntered( MouseEvent e ) { }
@Override
public void mouseExited( MouseEvent e ) { }
@Override
public void mouseReleased( MouseEvent e ) { }

static public void main( String[] s ) {
    JFrame f = new JFrame();
    f.setSize( 200, 200 );
    f.getContentPane().setLayout( new BorderLayout() );
    Drag d = new Drag();
    f.getContentPane().add( d, BorderLayout.NORTH );
    f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    f.setVisible( true );
}

}

最佳答案

启动拖动 Action 后,将不将运动事件作为正常的鼠标拖动事件进行调度。在拖动过程中,鼠标移动时会触发DragSourceDragEvent。下面的示例在拖动红色区域时将打印“DRAGMOUSEMOVED”。只需将下面的源粘贴到您的构造函数中即可。 DragSourceDragEvent具有大多数MouseEvent方法,因此它应该是一个不错的选择。

DragSource.getDefaultDragSource().addDragSourceMotionListener(new DragSourceMotionListener() {
    @Override
    public void dragMouseMoved(DragSourceDragEvent dsde) {
        System.out.println("DRAGMOUSEMOVED");
    }
});

09-26 05:56