我正在尝试MyObject
拖到JTable
. 但不知道如何。
我可以在这些方面获得帮助吗?(最好是第 3 点)
**1.**我发现的唯一很好的例子是:
指南:http ://www.bryanesmith.com/docs/drag-and-drop-java-5/ 来源:http://www.bryanesmith.com/docs/drag-and-drop-java-5/DragAndDropPanelsDemo。爪哇
问题是我得到:
java.lang.ClassNotFoundException:RandomDragAndDropPanel(行:197)
知道为什么吗?
**2.**关于其他好例子的任何提示?
或**3.**这是我放在一起的一个例子。但它只显示String
而不是MyObject
:
我为使这项工作所做的工作是添加:
public static DataFlavor FLAVOR = new DataFlavor(MyObject.class, "My Object");
但是我遇到了问题implementing Transferable
并添加TransferHandler
到MyObject
. 知道它应该是什么样子吗?
import java.awt.Color;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.TransferHandler;
import javax.swing.table.DefaultTableModel;
public class FlavorTest extends JFrame {
// FLAVOR = STRING
private DataFlavor FLAVOR = new DataFlavor(String.class, "DataWrapper");
public FlavorTest() {
// CREATE TABLE
DefaultTableModel model = getDefaultTableModel();
JTable table = new JTable(model);
addDragSupport(table, model);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setDropMode(DropMode.INSERT_ROWS);
table.setFillsViewportHeight(true);
// ADD TABLE AND DRAG OBJECT TO PANEL
JPanel panel = new JPanel();
panel.add(new MyObject("DRAG OBJECT"));
panel.add(table);
// ADD PANEL TO JFRAME
this.add(panel);
this.pack();
this.setDefaultCloseOperation(1);
this.setVisible(true);
}
// CREATE TABLE DATA
private DefaultTableModel getDefaultTableModel() {
String[] cols = { "a", "b", "c" };
String[][] rows = { { "1", "2", "3" } };
return new DefaultTableModel(rows, cols);
}
// SET TRANSFER HANDLER TO TABLE
private void addDragSupport(final JTable table, final DefaultTableModel model) {
table.setTransferHandler(new TransferHandler() {
public boolean canImport(TransferSupport support) {
return true;
}
public boolean importData(TransferSupport support) {
JTable.DropLocation dl = (JTable.DropLocation) support.getDropLocation();
int row = dl.getRow();
Object data;
try {
data = (Object) support.getTransferable().getTransferData(FLAVOR);
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
String[] rowData = data.toString().split(",");
model.insertRow(row, rowData);
return true;
}
});
}
public static void main(String[] args) {
new FlavorTest();
}
// CONTAINS A DRAGABLE JLIST WITH A STRING
private class MyObject extends JPanel {
public MyObject(String text) {
DefaultListModel<String> model = new DefaultListModel<String>();
model.addElement(text);
JList<String> list = new JList<String>(model);
list.setModel(model);
list.setDragEnabled(true);
list.setFocusable(false);
this.add(list);
this.setBorder(BorderFactory.createLineBorder(Color.black));
}
}
}