Hi, I'd like to drag and drop external files (e.g. from windows explorer) into a JTable. Anybody got some example code as how this is done?
+3
A:
Simply use the DropTarget class to receive drop events. You can distinguish between drops into the current table (available columns/rows) and into the scroll pane (to e.g. add new rows)
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SwingTest extends JFrame{
private JTable table = new JTable();
private JScrollPane scroll = new JScrollPane(table);
private DefaultTableModel tm = new DefaultTableModel(new String[]{"a","b","c"},2);
public SwingTest() {
table.setModel(tm);
table.setDropTarget(new DropTarget(){
@Override
public synchronized void drop(DropTargetDropEvent dtde) {
Point point = dtde.getLocation();
int column = table.columnAtPoint(point);
int row = table.rowAtPoint(point);
// handle drop inside current table
super.drop(dtde);
}
});
scroll.setDropTarget(new DropTarget(){
@Override
public synchronized void drop(DropTargetDropEvent dtde) {
// handle drop outside current table (e.g. add row)
super.drop(dtde);
}
});
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(scroll);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(800,600);
this.setVisible(true);
}
public static void main(String[] args) {
new SwingTest();
}
}
reallyinsane
2009-04-07 06:57:56