将文本从Java拖放到Web浏览器

时间:2017-08-01 12:28:31

标签: java swing drag-and-drop

我想让Java swing标签(包含文本)可拖动,以便可以将其放入浏览器窗口(js监听器拾取文本)。使其可拖动很容易,但如何将其传输到浏览器窗口?

我一直在搜索关于此的文档,但我发现的大部分内容都来自很久以前(从1999年开始!):

http://www.javaworld.com/article/2076358/swing-gui-programming/how-to-drag-and-drop-with-java-2--part-1.html

1 个答案:

答案 0 :(得分:0)

以下是一个小示例,说明如何扩展TransferHandler以将文本从JLabel(或任何其他组件)拖动到其他正在运行的应用程序中:

import javax.swing.*;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * @author Mikle Garin
 * @see https://stackoverflow.com/questions/45437809/drag-and-drop-text-from-java-to-web-browser
 */

public class TextDragHandler extends TransferHandler implements MouseListener, MouseMotionListener
{
    /**
     * Drag source component.
     */
    private final JComponent component;

    /**
     * Text to drag.
     */
    private final String text;

    /**
     * Whether or not drag was armed.
     * This flag is used to avoid multiple export operations.
     */
    private boolean armed;

    /**
     * Constructs new {@link TextDragHandler}.
     *
     * @param component drag source component
     * @param text      text to drag
     */
    public TextDragHandler ( final JComponent component, final String text )
    {
        super ();
        this.armed = false;
        this.text = text;
        this.component = component;
        this.component.addMouseListener ( this );
        this.component.addMouseMotionListener ( this );
    }

    @Override
    public void mouseClicked ( final MouseEvent e )
    {
        // Do nothing
    }

    @Override
    public void mousePressed ( final MouseEvent e )
    {
        if ( component.isEnabled () && SwingUtilities.isLeftMouseButton ( e ) )
        {
            armed = true;
        }
    }

    @Override
    public void mouseReleased ( final MouseEvent e )
    {
        if ( SwingUtilities.isLeftMouseButton ( e ) )
        {
            armed = false;
        }
    }

    @Override
    public void mouseEntered ( final MouseEvent e )
    {
        // Do nothing
    }

    @Override
    public void mouseExited ( final MouseEvent e )
    {
        // Do nothing
    }

    @Override
    public void mouseMoved ( final MouseEvent e )
    {
        // Do nothing
    }

    @Override
    public void mouseDragged ( final MouseEvent e )
    {
        if ( armed )
        {
            exportAsDrag ( component, e, getSourceActions ( component ) );
            armed = false;
        }
    }

    @Override
    public int getSourceActions ( final JComponent c )
    {
        return COPY;
    }

    @Override
    protected Transferable createTransferable ( final JComponent c )
    {
        return new StringSelection ( text );
    }

    /**
     * Sample {@link TextDragHandler} usage.
     *
     * @param args run arguments
     */
    public static void main ( final String[] args )
    {
        final JFrame frame = new JFrame ( "Drag example" );

        final JLabel label = new JLabel ( "DRAG ME" );
        label.setTransferHandler ( new TextDragHandler ( label, label.getText () ) );
        label.setBorder ( BorderFactory.createEmptyBorder ( 50, 50, 50, 50 ) );
        frame.add ( label );

        frame.setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );
        frame.pack ();
        frame.setLocationRelativeTo ( null );
        frame.setVisible ( true );
    }
}

这里最重要的是exportAsDrag ( ... )调用,它启动系统内的拖动操作,并将您的Transferable实例(在本例中为StringSelection实例)传递到该拖动操作中。