File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes problem with Drag and Drop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with Drag and Drop" Watch "problem with Drag and Drop" New topic
Author

problem with Drag and Drop

ganboy learner
Greenhorn

Joined: Jul 30, 2003
Posts: 1
Hello all,
I am currently facing a problem with Drag and Drop using jdk1.3. The code works fine with jdk1.4. I want to drag a particular node from a tree and drop it on a table. I have implemented appropriate listeners but the drop function doesn't seem to work..It doesn't throw any exception as well..i am pasting the code.. I am sorry i know its too long. But it would be great if someone can help me out. I am new to java swing
-- ganboy
import java.io.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.dnd.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.table.*;
import javax.swing.border.*;
import java.awt.datatransfer.*;
/*<applet code="DragDropApplet.class" width=900 height=300> </applet>*/
public class DragDropApplet extends JApplet implements DragSourceListener, DragGestureListener,DropTargetListener,ActionListener {
JTree jtr;
JTable jtab;
JTable jtab1;
JTable jtab2;
JPanel jPanel;
DragSource ds;
DropTarget dt1;
DropTarget dt2;
DropTarget dt3;
DropTarget dt4;
DropTarget dt5;
DropTarget dt6;
JScrollPane scroller1;
JScrollPane scroller2;
JScrollPane scroller3;
JScrollPane scroller4;
TreePath selPath;
DefaultTreeModel dmodel;
DefaultTableModel model;
DefaultTableModel model1;
DefaultTableModel model2;
DefaultMutableTreeNode top;
Hashtable hash1;
StringSelection transferable;
Vector rowVec;
Vector rowVec1;
Vector rowVec2;
public void init() {
System.out.println("got in init().........................................");
this.setSize(700,400);
hash1 = new Hashtable();
try
{
top = new DefaultMutableTreeNode("List of tables");
DefaultMutableTreeNode top1 = new DefaultMutableTreeNode("Table1");
top1.add(new DefaultMutableTreeNode("Field1"));
top1.add(new DefaultMutableTreeNode("Field2"));
top1.add(new DefaultMutableTreeNode("Field3"));
top1.add(new DefaultMutableTreeNode("Field4"));
top1.add(new DefaultMutableTreeNode("Field5"));
top.add(top1);
DefaultMutableTreeNode top2 = new DefaultMutableTreeNode("Table2");
top2.add(new DefaultMutableTreeNode("Field1"));
top2.add(new DefaultMutableTreeNode("Field2"));
top2.add(new DefaultMutableTreeNode("Field3"));
top2.add(new DefaultMutableTreeNode("Field4"));
top2.add(new DefaultMutableTreeNode("Field5"));
top.add(top2);
DefaultMutableTreeNode top3 = new DefaultMutableTreeNode("Table3");
top3.add(new DefaultMutableTreeNode("Field1"));
top3.add(new DefaultMutableTreeNode("Field2"));
top3.add(new DefaultMutableTreeNode("Field3"));
top3.add(new DefaultMutableTreeNode("Field4"));
top3.add(new DefaultMutableTreeNode("Field5"));
top.add(top3);
DefaultMutableTreeNode top4 = new DefaultMutableTreeNode("Table4");
top4.add(new DefaultMutableTreeNode("Field1"));
top4.add(new DefaultMutableTreeNode("Field2"));
top4.add(new DefaultMutableTreeNode("Field3"));
top4.add(new DefaultMutableTreeNode("Field4"));
top4.add(new DefaultMutableTreeNode("Field5"));
top.add(top4);
this.getContentPane().setLayout(new GridLayout(1,2));
dmodel = new DefaultTreeModel(top);
jtr = new JTree(dmodel);
scroller1 = new JScrollPane(jtr);
this.getContentPane().add(scroller1);
JPanel jpanel = new JPanel();
jpanel.setLayout(new GridLayout(3,1));
/* Creating Field List table*/
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
topPanel.add(new Label("Select List"), BorderLayout.NORTH);
jtab = new JTable();
model2 = (DefaultTableModel)jtab.getModel();
model2.addColumn("Field");
scroller2 = new JScrollPane(jtab);
topPanel.add(scroller2, BorderLayout.CENTER);
JButton jbtn1 = new JButton("Delete");
jbtn1.setActionCommand("delete1");
jbtn1.addActionListener(this);
JPanel jsouthPanel1 = new JPanel();
jsouthPanel1.setLayout(new BorderLayout());
jsouthPanel1.add(jbtn1,BorderLayout.EAST);
topPanel.add(jsouthPanel1, BorderLayout.SOUTH);
jpanel.add(topPanel);
/* Creating Criteria table*/
JPanel middlePanel = new JPanel();
middlePanel.setLayout(new BorderLayout());
middlePanel.add(new Label("Criteria"), BorderLayout.NORTH);
jtab1 = new JTable();
model = (DefaultTableModel)jtab1.getModel();
model.addColumn("Field");
model.addColumn("Field/Value");
model.addColumn("AND/OR");
JComboBox comboBox = new JComboBox();
comboBox.addItem("AND");
comboBox.addItem("OR");
TableColumn tempCol1 = jtab1.getColumnModel().getColumn(0);
tempCol1.setResizable(false);
TableColumn tempCol2 = jtab1.getColumnModel().getColumn(1);
tempCol2.setResizable(false);
TableColumn tempCol3 = jtab1.getColumnModel().getColumn(2);
tempCol3.setResizable(false);
tempCol3.setCellEditor(new DefaultCellEditor(comboBox));
scroller3 = new JScrollPane(jtab1);
middlePanel.add(scroller3, BorderLayout.CENTER);
JButton jbtn2 = new JButton("Delete");
jbtn2.setActionCommand("delete2");
jbtn2.addActionListener(this);
JPanel jsouthPanel2 = new JPanel();
jsouthPanel2.setLayout(new BorderLayout());
jsouthPanel2.add(jbtn2,BorderLayout.EAST);
middlePanel.add(jsouthPanel2, BorderLayout.SOUTH);
jpanel.add(middlePanel);
/* Creating sort Criteria table*/
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
bottomPanel.add(new Label("Sort Criteria"), BorderLayout.NORTH);
jtab2 = new JTable();
model1 = (DefaultTableModel)jtab2.getModel();
model1.addColumn("Field");
model1.addColumn("Sort by");
JComboBox comboBox1 = new JComboBox();
comboBox1.addItem("ASCENDING");
comboBox1.addItem("DESCENDING");
TableColumn tempCol4 = jtab2.getColumnModel().getColumn(0);
tempCol4.setResizable(false);
TableColumn tempCol5 = jtab2.getColumnModel().getColumn(1);
tempCol5.setResizable(false);
tempCol5.setCellEditor(new DefaultCellEditor(comboBox1));
scroller4 = new JScrollPane(jtab2);
bottomPanel.add(scroller4, BorderLayout.CENTER);
JButton jbtn3 = new JButton("Delete");
jbtn3.setActionCommand("delete3");
jbtn3.addActionListener(this);
JPanel jsouthPanel3 = new JPanel();
jsouthPanel3.setLayout(new BorderLayout());
jsouthPanel3.add(jbtn3,BorderLayout.EAST);
bottomPanel.add(jsouthPanel3, BorderLayout.SOUTH);
jpanel.add(bottomPanel);
this.getContentPane().add(jpanel);
MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
selPath = jtr.getPathForLocation(e.getX(), e.getY());
}
};
jtr.addMouseListener(ml);
ds = new DragSource();
DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(jtr, DnDConstants.ACTION_COPY, this);
dt1 = new DropTarget(jtab, this);
dt2 = new DropTarget(jtab1, this);
dt3 = new DropTarget(jtab2, this);
dt4 = new DropTarget(scroller2, this);
dt5 = new DropTarget(scroller3, this);
dt6 = new DropTarget(scroller4, this);
setVisible(true);
}catch (Exception e){
e.printStackTrace();
}
}
/* If delete buttons clicked */
public void actionPerformed(ActionEvent e){
if("delete1".equals(e.getActionCommand())){
int delRows[] = jtab.getSelectedRows();
for(int j=0; j<delRows.length; j++){
model2.removeRow(delRows[j]-j);
}
}
if("delete2".equals(e.getActionCommand())){
int delRows[] = jtab1.getSelectedRows();
for(int j=0; j<delRows.length; j++){
model.removeRow(delRows[j]-j);
}
}
if("delete3".equals(e.getActionCommand())){
int delRows[] = jtab2.getSelectedRows();
for(int j=0; j<delRows.length; j++){
model1.removeRow(delRows[j]-j);
}
}
}
public void dragGestureRecognized(DragGestureEvent dge) {
DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)selPath.getLastPathComponent();
/* Storing the content of the dragged node*/
if(dmtn.getChildCount() == 0){
System.out.println("Drag Gesture Recognized!" + (selPath.getLastPathComponent()).toString() );
transferable = new StringSelection(dmtn.getParent()+"."+(selPath.getLastPathComponent()).toString());
ds.startDrag(dge, DragSource.DefaultCopyDrop, transferable, this);
}
}
public void dragEnter(DragSourceDragEvent dsde) {
//System.out.println("Drag Enter");
}
public void dragExit(DragSourceEvent dse) {
}
public void dragOver(DragSourceDragEvent dsde) {
}
public void dragDropEnd(DragSourceDropEvent dsde) {
}
public void dropActionChanged(DragSourceDragEvent dsde) {
}
public void dragEnter(DropTargetDragEvent dtde) {
}
public void dragExit(DropTargetEvent dte) {
}
public void dragOver(DropTargetDragEvent dtde) {
}
public void dropActionChanged(DropTargetDragEvent dtde) {
System.out.println("Drop Action Changed");
}
public void drop(DropTargetDropEvent dtde) {
System.out.println("Inside drop function");
try {
Object obj = dtde.getSource();
Transferable tr = dtde.getTransferable();
DataFlavor[] flavors = tr.getTransferDataFlavors();
if(obj.equals((Object)dt2) || obj.equals((Object)dt5)){
System.out.println("Tab val 2 ");
for (int i = 0; i < flavors.length; i++) {
dtde.acceptDrop(DnDConstants.ACTION_COPY);
rowVec1 = new Vector();
rowVec1.add(tr.getTransferData(flavors[i]));
model.addRow(rowVec1);
jtab1.updateUI();
dtde.dropComplete(true);
return;
}
}
if(obj.equals((Object)dt3) || obj.equals((Object)dt6)){
System.out.println("Tab val 3 ");
for (int i = 0; i < flavors.length; i++) {
dtde.acceptDrop(DnDConstants.ACTION_COPY);
rowVec2 = new Vector();
rowVec2.add(tr.getTransferData(flavors[i]));
model1.addRow(rowVec2);
jtab2.updateUI();
dtde.dropComplete(true);
return;
}
}
if(obj.equals((Object)dt1) || obj.equals((Object)dt4)){
System.out.println("Tab val 1 ");
for (int i = 0; i < flavors.length; i++) {
dtde.acceptDrop(DnDConstants.ACTION_COPY);
rowVec = new Vector();
rowVec.add(tr.getTransferData(flavors[i]));
model2.addRow(rowVec);
jtab.updateUI();
dtde.dropComplete(true);
return;
}
}
} catch (Exception e) {
e.printStackTrace();
dtde.rejectDrop();
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with Drag and Drop