Friday, December 3, 2010

Drag and Drop between two Table(Java Swing)

I have written Program for Drag and Drop between two tables.
I followed below steps,

* Creating two(table1 and table2) non editable table with single selection(drag and drop for single cell values).
* In table1 only C and D column only u can drop.
* table2 drop cell is empty then table1 drag value drop in table1 and that value will deleted from table2.
* drop cell is not empty then existing table1 value will be added to table2.


My program as below

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
* DADTableDemo : Example of drag and drop option between two tables
* @author R.Amirtharaj
*/
public class DADTableDemo {
/** Main Frame */
public JFrame myframe = new JFrame("Drag and drop demo");
/** Table 1*/
public JTable table1 = new JTable();
/** Table 2*/
public JTable table2 = new JTable();
/** Table1 Model */
public DefaultTableModel tm1 = new DefaultTableModel(0, 5) {

public boolean isCellEditable(int row, int col) {
return false;
}
};
/** Table2 Model */
public DefaultTableModel tm2 = new DefaultTableModel(0, 5) {

public boolean isCellEditable(int row, int col) {
return false;
}
};
/** Table1 ColumnNames */
public String[] proptbtitle1 = {"A", "B", "C(Drop will allow)", "D(Drop will allow)", "E"};
/** Table2 ColumnNames */
public String[] proptbtitle2 = {"Z"};

// Constructor
public static void main(String args[])
{
DADTableDemo demo = new DADTableDemo();
demo.create();
}

// create Drag and Drop demo
public void create()
{
JPanel pan = new JPanel();

// Table 1 related
tm1.setColumnIdentifiers(proptbtitle1);
table1.setModel(tm1);
table1.getTableHeader().setReorderingAllowed(false);
addTable1Details();
table1.setDragEnabled(true);
table1.setDropMode(DropMode.ON);
table1.setSelectionMode(0);
table1.getTableHeader().setReorderingAllowed(false);

// Add DAD
TransferHandler dnd1 = getDND1();
table1.setTransferHandler(dnd1);

// add table1 to scrollpane
JScrollPane sp1 = new JScrollPane(table1);

// add to panel
pan.add(sp1);

// Table 1 related
tm2.setColumnIdentifiers(proptbtitle2);
table2.setModel(tm2);
table2.getTableHeader().setReorderingAllowed(false);
addTable2Details();
table2.setDragEnabled(true);
table2.setSelectionMode(0);

// Add DAD
TransferHandler dnd2 = getDND2();
table2.setTransferHandler(dnd2);

// add table1 to scrollpane
JScrollPane sp2 = new JScrollPane(table2);

// add to panel
pan.add(sp2);

// add to frame
myframe.getContentPane().add(pan);
myframe.pack();
myframe.setVisible(true);

}

/**
* Table1 details
*/
public void addTable1Details()
{
String str[][] = {{"aaa", "bbb", "", "", "eee"},
{"fff", "ggg", "hhh", "iii", "jjj"}};
for(int i=0;i -1 && row < movesupported =" (MOVE" dl =" (JTable.DropLocation)" dcol =" (JTable.DropLocation)" row =" dl.getRow();"> -1 && row < table1.getRowCount()) {
} else {
return false;
}

int intcol = dcol.getColumn();

// Allowing drop only for C and D column for Table 1
if (intcol != 2 || intcol != 3) {
return false;
}
// fetch the data and bail if this fails

String data;
try {
data = (String) support.getTransferable().getTransferData(
DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e) {
return false;
} catch (IOException e) {
return false;
}

System.out.println("data = "+data);
if (intcol == 2) {
// if field is not empty existing value will add in Table 2
if (table1.getValueAt(row, 2) != null && !table1.getValueAt(row, 2).toString().equalsIgnoreCase("")) {
String strEID = table1.getValueAt(row, 2).toString();
// adding table2
tm2.addRow(new String[]{strEID});
}
table1.setValueAt(data, row, intcol);
table1.repaint();
}
else if (intcol == 3) {
// if field is not empty existing value will add in Table 2
if (table1.getValueAt(row, 3) != null && !table1.getValueAt(row, 3).toString().equalsIgnoreCase("")) {
String strEID = table1.getValueAt(row, 3).toString();
// adding table2
tm2.addRow(new String[]{strEID});
}
table1.setValueAt(data, row, intcol);
table1.repaint();
}
return true;
}
};

return dnd1;
}

/**
* TransferHandler for Table2
* @return TransferHandler for Table2
*/
public TransferHandler getDND2() {
TransferHandler dnd2 = new TransferHandler() {

public int getSourceActions(JComponent comp) {
return MOVE;
}
private int index = -1;

// dad data reference
String transferData = "";
public Transferable createTransferable(
JComponent comp) {
index = table2.getSelectedRow();
// fetching selected data
transferData = table2.getValueAt(table2.getSelectedRow(), 0).toString();
return new StringSelection(transferData);
}

public void exportDone(JComponent comp, Transferable trans, int action) {
if (action != MOVE) {
return;
}
tm2.removeRow(index);
}
};
return dnd2;
}
}

1 comment:

  1. C:\Users\Puppy\JavaProject\DADTableDemo.java:102: ';' expected
    for(int i=0;i -1 && row < movesupported =" (MOVE" dl =" (JTable.DropLocation)" dcol =" (JTable.DropLocation)" row =" dl.getRow();"> -1 && row < table1.getRowCount()) {
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:102: ')' expected
    for(int i=0;i -1 && row < movesupported =" (MOVE" dl =" (JTable.DropLocation)" dcol =" (JTable.DropLocation)" row =" dl.getRow();"> -1 && row < table1.getRowCount()) {
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:102: ';' expected
    for(int i=0;i -1 && row < movesupported =" (MOVE" dl =" (JTable.DropLocation)" dcol =" (JTable.DropLocation)" row =" dl.getRow();"> -1 && row < table1.getRowCount()) {
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:102: ';' expected
    for(int i=0;i -1 && row < movesupported =" (MOVE" dl =" (JTable.DropLocation)" dcol =" (JTable.DropLocation)" row =" dl.getRow();"> -1 && row < table1.getRowCount()) {
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:103: 'else' without 'if'
    } else {
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:150: class, interface, or enum expected
    return dnd1;
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:151: class, interface, or enum expected
    }
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:157: class, interface, or enum expected
    public TransferHandler getDND2() {
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:160: class, interface, or enum expected
    public int getSourceActions(JComponent comp) {
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:162: class, interface, or enum expected
    }
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:166: class, interface, or enum expected
    String transferData = "";
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:167: class, interface, or enum expected
    public Transferable createTransferable(
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:171: class, interface, or enum expected
    transferData = table2.getValueAt(table2.getSelectedRow(), 0).toString();
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:172: class, interface, or enum expected
    return new StringSelection(transferData);
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:173: class, interface, or enum expected
    }
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:175: class, interface, or enum expected
    public void exportDone(JComponent comp, Transferable trans, int action) {
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:178: class, interface, or enum expected
    }
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:180: class, interface, or enum expected
    }
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:182: class, interface, or enum expected
    return dnd2;
    ^
    C:\Users\Puppy\JavaProject\DADTableDemo.java:183: class, interface, or enum expected
    }
    ^
    20 errors

    Tool completed with exit code 1

    ReplyDelete