为什么不能拖放...?哪里出错?
import java.awt.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;public class DragTest extends JFrame implements DragSourceListener, DragGestureListener { DragSource ds;
JTable table;
StringSelection transferable;
String []col = {"A","B","C","D","E"};
String [][]items = { {"A","C","D","E","F"},
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},}; public DragTest() {
super("Drag Test");
setSize(400,300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
table = new JTable(items, col);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); ds = new DragSource();
DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(
table, DnDConstants.ACTION_MOVE, this);
setVisible(true);
} public void dragGestureRecognized(DragGestureEvent dge) {
System.out.println("Drag Gesture Recognized!");
transferable = new StringSelection((table.getValueAt(table.getSelectedRow(), table.getSelectedColumn())).toString());
ds.startDrag(dge, DragSource.DefaultCopyDrop, transferable, this);
} public void dragEnter(DragSourceDragEvent dsde) {
System.out.println("Drag Enter");
} public void dragExit(DragSourceEvent dse) {
System.out.println("Drag Exit");
} public void dragOver(DragSourceDragEvent dsde) {
System.out.println("Drag Over");
} public void dragDropEnd(DragSourceDropEvent dsde) {
System.out.print("Drag Drop End: ");
if (dsde.getDropSuccess()) {
System.out.println("Succeeded");
}
else {
System.out.println("Failed");
}
} public void dropActionChanged(DragSourceDragEvent dsde) {
System.out.println("Drop Action Changed");
} public static void main(String args[]) {
new DragTest();
}
}
import java.awt.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;public class DragTest extends JFrame implements DragSourceListener, DragGestureListener { DragSource ds;
JTable table;
StringSelection transferable;
String []col = {"A","B","C","D","E"};
String [][]items = { {"A","C","D","E","F"},
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},}; public DragTest() {
super("Drag Test");
setSize(400,300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
table = new JTable(items, col);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); ds = new DragSource();
DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(
table, DnDConstants.ACTION_MOVE, this);
setVisible(true);
} public void dragGestureRecognized(DragGestureEvent dge) {
System.out.println("Drag Gesture Recognized!");
transferable = new StringSelection((table.getValueAt(table.getSelectedRow(), table.getSelectedColumn())).toString());
ds.startDrag(dge, DragSource.DefaultCopyDrop, transferable, this);
} public void dragEnter(DragSourceDragEvent dsde) {
System.out.println("Drag Enter");
} public void dragExit(DragSourceEvent dse) {
System.out.println("Drag Exit");
} public void dragOver(DragSourceDragEvent dsde) {
System.out.println("Drag Over");
} public void dragDropEnd(DragSourceDropEvent dsde) {
System.out.print("Drag Drop End: ");
if (dsde.getDropSuccess()) {
System.out.println("Succeeded");
}
else {
System.out.println("Failed");
}
} public void dropActionChanged(DragSourceDragEvent dsde) {
System.out.println("Drop Action Changed");
} public static void main(String args[]) {
new DragTest();
}
}
cell 与 cell
之间拖放会 failed...
拖 A 到放在 table(3, 3)...可以嗎?