为什么DragHandler exportAs Drag禁用了我的MouseMotionListener?

时间:2011-02-03 13:27:43

标签: java swing event-handling drag-and-drop

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

这是一个小样本代码:

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

红色Side通过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 );
}

}

2 个答案:

答案 0 :(得分:5)

开始拖动动作后,动作事件将不会被分派为正常的鼠标拖动事件。在拖动过程中,鼠标移动时会触发DragSourceDragEvent。拖动红色区域时,以下示例将打印“DRAGMOUSEMOVED”。只需将下面的源代码粘贴到构造函数中即可。 DragSourceDragEvent具有大多数MouseEvent方法,因此它应该是一个很好的替代方法。

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

答案 1 :(得分:0)

更改此行:

this.getTransferHandler().exportAsDrag( this, e, TransferHandler.MOVE );

为:

this.getTransferHandler().exportAsDrag( this, e, TransferHandler.NONE );

当我这样做时,我看到了你期望的行为(“拖出”在“导出”后打印到控制台)。