下面的代码能实现这个功能,主要是导入java.awt.dnd.*; 给textArea添加,TextDropTargetListener 这个代码还有个进度条的的功能,找个大点的txt文件就能看出来. 文件读取没用缓冲区,还可以优化. import java.awt.*; import java.awt.datatransfer.*; import java.awt.dnd.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.text.*;/** * This is a test class to test drag and drop behavior. Drop items into the text * area to see the MIME types of the drop target. */ public class DropTargetTest { public static void main(String[] args) { JFrame frame = new DropTargetFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }/** * This frame contains a text area that is a simple drop target. */ class DropTargetFrame extends JFrame { public DropTargetFrame() { setTitle("DropTarget"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); textArea = new JTextArea("Drop items into this text area.\n"); new DropTarget(textArea, new TextDropTargetListener(textArea)); add(new JScrollPane(textArea), "Center"); } private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 300; private JTextArea textArea; class TextDropTargetListener implements DropTargetListener { /** * Constructs a listener. * * @param aTextArea * the text area in which to display the properties of the * dropped object. */ public TextDropTargetListener(JTextArea aTextArea) { textArea = aTextArea; } public void dragEnter(DropTargetDragEvent event) { int a = event.getDropAction(); if ((a & DnDConstants.ACTION_COPY) != 0) textArea.append("ACTION_COPY\n"); if ((a & DnDConstants.ACTION_MOVE) != 0) textArea.append("ACTION_MOVE\n"); if ((a & DnDConstants.ACTION_LINK) != 0) textArea.append("ACTION_LINK\n"); if (!isDragAcceptable(event)) { event.rejectDrag(); return; } } public void dragExit(DropTargetEvent event) { } public void dragOver(DropTargetDragEvent event) { // you can provide visual feedback here } public void dropActionChanged(DropTargetDragEvent event) { if (!isDragAcceptable(event)) { event.rejectDrag(); return; } } public void drop(DropTargetDropEvent event) { if (!isDropAcceptable(event)) { event.rejectDrop(); return; } event.acceptDrop(DnDConstants.ACTION_COPY); Transferable transferable = event.getTransferable();
DataFlavor[] flavors = transferable.getTransferDataFlavors(); for (int i = 0; i < flavors.length; i++) { DataFlavor d = flavors[i]; //textArea.append("MIME type=" + d.getMimeType() + "\n"); try { if (d.equals(DataFlavor.javaFileListFlavor)) { java.util.List<File> fileList = (java.util.List<File>) transferable .getTransferData(d); for (File f : fileList) { // textArea.append(f + "\n"); textArea.setDocument(new PlainDocument()); setTitle(f.getName()); openFile(f); } } else if (d.equals(DataFlavor.stringFlavor)) { String s = (String) transferable.getTransferData(d); textArea.append(s + "\n"); } } catch (Exception e) { textArea.append(e + "\n"); } } textArea.append("\n"); event.dropComplete(true); } public boolean isDragAcceptable(DropTargetDragEvent event) { // usually, you check the available data flavors here // in this program, we accept all flavors return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0; } public boolean isDropAcceptable(DropTargetDropEvent event) { // usually, you check the available data flavors here // in this program, we accept all flavors return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0; } private JTextArea textArea; } public void openFile(final File f) throws IOException { // set up stream and reader filter sequence
FileInputStream fileIn = new FileInputStream(f); ProgressMonitorInputStream progressIn = new ProgressMonitorInputStream(this, "Reading " + f.getName(), fileIn); final Scanner in = new Scanner(progressIn);
// the monitored activity must be in a new thread. Runnable readRunnable = new Runnable() { public void run() { final JTextArea textArea = new JTextArea();
while (in.hasNextLine()) { String line = in.nextLine(); textArea.append(line); textArea.append("\n"); } in.close();
// set content pane in the event dispatch thread EventQueue.invokeLater(new Runnable() { public void run() { setContentPane(new JScrollPane(textArea)); validate(); } });
} }; Thread readThread = new Thread(readRunnable); readThread.start(); }}
给textArea添加,TextDropTargetListener
这个代码还有个进度条的的功能,找个大点的txt文件就能看出来.
文件读取没用缓冲区,还可以优化.
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;/**
* This is a test class to test drag and drop behavior. Drop items into the text
* area to see the MIME types of the drop target.
*/
public class DropTargetTest {
public static void main(String[] args) {
JFrame frame = new DropTargetFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}/**
* This frame contains a text area that is a simple drop target.
*/
class DropTargetFrame extends JFrame {
public DropTargetFrame() {
setTitle("DropTarget");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); textArea = new JTextArea("Drop items into this text area.\n"); new DropTarget(textArea, new TextDropTargetListener(textArea));
add(new JScrollPane(textArea), "Center");
} private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 300; private JTextArea textArea; class TextDropTargetListener implements DropTargetListener {
/**
* Constructs a listener.
*
* @param aTextArea
* the text area in which to display the properties of the
* dropped object.
*/
public TextDropTargetListener(JTextArea aTextArea) {
textArea = aTextArea;
} public void dragEnter(DropTargetDragEvent event) {
int a = event.getDropAction();
if ((a & DnDConstants.ACTION_COPY) != 0)
textArea.append("ACTION_COPY\n");
if ((a & DnDConstants.ACTION_MOVE) != 0)
textArea.append("ACTION_MOVE\n");
if ((a & DnDConstants.ACTION_LINK) != 0)
textArea.append("ACTION_LINK\n"); if (!isDragAcceptable(event)) {
event.rejectDrag();
return;
}
} public void dragExit(DropTargetEvent event) {
} public void dragOver(DropTargetDragEvent event) {
// you can provide visual feedback here
} public void dropActionChanged(DropTargetDragEvent event) {
if (!isDragAcceptable(event)) {
event.rejectDrag();
return;
}
} public void drop(DropTargetDropEvent event) {
if (!isDropAcceptable(event)) {
event.rejectDrop();
return;
} event.acceptDrop(DnDConstants.ACTION_COPY); Transferable transferable = event.getTransferable();
DataFlavor[] flavors = transferable.getTransferDataFlavors();
for (int i = 0; i < flavors.length; i++) {
DataFlavor d = flavors[i];
//textArea.append("MIME type=" + d.getMimeType() + "\n"); try {
if (d.equals(DataFlavor.javaFileListFlavor)) {
java.util.List<File> fileList = (java.util.List<File>) transferable
.getTransferData(d);
for (File f : fileList) {
// textArea.append(f + "\n");
textArea.setDocument(new PlainDocument());
setTitle(f.getName());
openFile(f);
}
} else if (d.equals(DataFlavor.stringFlavor)) {
String s = (String) transferable.getTransferData(d);
textArea.append(s + "\n");
}
} catch (Exception e) {
textArea.append(e + "\n");
}
}
textArea.append("\n");
event.dropComplete(true);
} public boolean isDragAcceptable(DropTargetDragEvent event) {
// usually, you check the available data flavors here
// in this program, we accept all flavors
return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
} public boolean isDropAcceptable(DropTargetDropEvent event) {
// usually, you check the available data flavors here
// in this program, we accept all flavors
return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
} private JTextArea textArea;
} public void openFile(final File f) throws IOException
{
// set up stream and reader filter sequence
FileInputStream fileIn = new FileInputStream(f);
ProgressMonitorInputStream progressIn
= new ProgressMonitorInputStream(this, "Reading " + f.getName(), fileIn);
final Scanner in = new Scanner(progressIn);
// the monitored activity must be in a new thread. Runnable readRunnable = new
Runnable()
{
public void run()
{
final JTextArea textArea = new JTextArea();
while (in.hasNextLine())
{
String line = in.nextLine();
textArea.append(line);
textArea.append("\n");
}
in.close();
// set content pane in the event dispatch thread
EventQueue.invokeLater(new
Runnable()
{
public void run()
{
setContentPane(new JScrollPane(textArea));
validate();
}
});
}
}; Thread readThread = new Thread(readRunnable);
readThread.start();
}}