Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Drag and Drop of cell content between 2 tables

843806Jan 17 2008 — edited Jan 17 2008
Hi Guys,

Iam into implementing drag and drop of cell contents between 2 different tables,say Table1 and Table2.(Iam not dragging and dropping rows or cells,Just copying the content of one cell to another).

Have extended the java tutorial class "TableTransferHandler" and "StringTransferHandler" under http://java.sun.com/docs/books/tutorial/uiswing/examples/dnd/index.html#ExtendedDndDemo to satisfy my needs.

The drag is enabled for Table1 and table2 as follows.

jTable1.setDragEnabled(true);
jTable1.setTransferHandler(new TableTransferHandler());

jTable2.setDragEnabled(true);
jTable2.setTransferHandler(new TableTransferHandler());

Dont be taken aback with the code I have put.It just to show what I have done..
My questions are put at the end of the post..

//String Transfer Handler class.
public abstract class StringTransferHandler extends TransferHandler {
    
    protected abstract String exportString(JComponent c);
    protected abstract void importString(JComponent c, String str);
    protected abstract void cleanup(JComponent c, boolean remove);
    
    protected Transferable createTransferable(JComponent c) {
        return new StringSelection(exportString(c));
    }
    
    public int getSourceActions(JComponent c) {
        return COPY_OR_MOVE;
    }
    
    public boolean importData(JComponent c, Transferable t) {
        if (canImport(c, t.getTransferDataFlavors())) {
            try {
                String str = (String)t.getTransferData(DataFlavor.stringFlavor);
                importString(c, str);
                return true;
            } catch (UnsupportedFlavorException ufe) {
            } catch (IOException ioe) {
            }
        }

        return false;
    }
    
    protected void exportDone(JComponent c, Transferable data, int action) {
        cleanup(c, action == MOVE);
    }
    
    public boolean canImport(JComponent c, DataFlavor[] flavors) {
    	
    	 JTable table = (JTable)c;    	 
         int selColIndex = table.getSelectedColumn();
        for (int i = 0; i < flavors.length; i++) {
            if ((DataFlavor.stringFlavor.equals(flavors))&& (selColIndex !=0)) {
return true;
}
}
return false;
}
}
//TableTransferHandler class
public class TableTransferHandler extends StringTransferHandler {
private int[] rows = null;
private int addIndex = -1; //Location where items were added
private int addCount = 0; //Number of items added.

protected String exportString(JComponent c) {
JTable table = (JTable)c;
rows = table.getSelectedRows();


StringBuffer buff = new StringBuffer();
int selRowIndex = table.getSelectedRow();
int selColIndex = table.getSelectedColumn();
String val = table.getValueAt(selRowIndex,selColIndex).toString();
buff.append(val);

return buff.toString();
}

protected void importString(JComponent c, String str) {

JTable target = (JTable)c;
DefaultTableModel model = (DefaultTableModel)target.getModel();
//int index = target.getSelectedRow();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
target.setValueAt(str, row, column);
}
protected void cleanup(JComponent c, boolean remove) {
}
}
Now I want to put in the following functionality into my program...

[1]prevent dragging and dropping text in to the same table.That means I dont want to drag a cell content from Table1  and drop to another cell in Table1. Want to drag and drop cell content only from Table1 to Table2.

[2]Change cursor on a un-defined Target. That means how to prevent a drag from a particular column in Table1.Also how to prevent a drop to a particular column in Table2. How to change the cursor to a "NO-DRAG" cursoror "NO-DROP" cursor.

Could it be done using Drag Source Listener and drop Target Listener?.

If yes,How can these listeners attached to the table and how to do it?

If No,How Could it be done?


[3]Want to change the background colour of the cell being dragged and also the background colour of the target cell where the drop is made...

[4]Is there any out of the box way to make an undo in the target cell(where drop was made) so that the old cell value is brought back.

How can I extend my code to take care of the above said things.

Any help or suggestions is greatly appreciated.....

Edited by: Kohinoor on Jan 17, 2008 10:58 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 14 2008
Added on Jan 17 2008
0 comments
242 views