Swing JTextfield DnD将现有文本替换为导入的文本 [英] Swing JTextfield DnD replace the existing text with the imported text

查看:106
本文介绍了Swing JTextfield DnD将现有文本替换为导入的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本字段,可以在它们之间拖放文本。我想要的是每次我拖动文本时,它将用拖放的文本替换现有的文本数据。

I have two text fields and I can drag and drop the text between them. What I want is that every time I drag the text it will replace the existing text data with the text which was dragged and dropped.

import java.awt.Container;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class DragDropText extends JFrame {

    public static void main(String[] args) {
        new DragDropText().setVisible(true);
    }

    public DragDropText() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextField field1 = new JTextField("Life's a drag", 20);
        JTextField field2 = new JTextField("and then you drop", 20);
        field1.setDragEnabled(true);
        field2.setDragEnabled(true);
        Container content = getContentPane();

        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        content.add(field1);
        content.add(field2);

        pack();
    }
}


推荐答案

您可以通过创建和设置TransferHandler的子类来达到效果。

You can achieve the effect by creating and setting a subclass of TransferHandler.

此示例适用于JTextComponent的任何子类。您必须添加适当的检查以使其更强大。

This is an example that will work for any subclass of JTextComponent. You'll have to add the appropriate checks to make it robust.

您可以在此处找到更多信息: http://download.oracle.com/javase/tutorial/uiswing/dnd/transferhandler.html

You can find more info here: http://download.oracle.com/javase/tutorial/uiswing/dnd/transferhandler.html.

import java.io.*;
import java.awt.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.text.*;

public class DragDropText extends JFrame {

    public static void main(String[] args) {
        new DragDropText().setVisible(true);
    }

    public DragDropText() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextField field1 = new JTextField("Life's a drag", 20);
        JTextField field2 = new JTextField("and then you drop", 20);
        field1.setDragEnabled(true);
        field2.setDragEnabled(true);
        field1.setTransferHandler(new CustomTransferHandler());
        field2.setTransferHandler(new CustomTransferHandler());

        Container content = getContentPane();

        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        content.add(field1);
        content.add(field2);

        pack();
    }
}

class CustomTransferHandler extends TransferHandler {
    public int getSourceActions(JComponent c) {
        return COPY_OR_MOVE;
    }

    public Transferable createTransferable(JComponent c) {
        return new StringSelection(((JTextComponent) c).getSelectedText());
    }

    public void exportDone(JComponent c, Transferable t, int action) {
        if(action == MOVE)
            ((JTextComponent) c).replaceSelection("");
    }

    public boolean canImport(TransferSupport ts) {
        return ts.getComponent() instanceof JTextComponent;
    }

    public boolean importData(TransferSupport ts) {
        try {
            ((JTextComponent) ts.getComponent())
                .setText((String) ts
                         .getTransferable()
                         .getTransferData(DataFlavor.stringFlavor));
            return true;
        } catch(UnsupportedFlavorException e) {
            return false;
        } catch(IOException e) {
            return false;
        }
    }
}

这篇关于Swing JTextfield DnD将现有文本替换为导入的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆