Drag and Drop in a JTable (ContentTable)

Hello Friends,

I am designing a tool which can be used for daily task distribution, according to time (as we setup in a outlook calendar) using JTable.

Now I am planning to use a Drag and Drop option where in the task from "one date and time" can be moved to "another date and time" i.e row/column.

E.g : Suppose the new task which is scheduled at 12:30pm on 22/03/07 has to be rescheduled at 3:00pm 26/03/07. The drag and drop should automatically recognise.

It would be greate if anyone can throw in thier ideas.. 10 DUKE points..

;-)

Thanks and Rgds,

Pradeep

[611 byte] By [KINOa] at [2007-11-26 22:31:08]
# 1
You should use your own table model ( http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableModel.html) and let drag & drop events restructure the model. here is a really good article on d&d: http://www.rockhoppertech.com/java-drag-and-drop-faq.html
jEti182a at 2007-7-10 11:36:30 > top of Java-index,Desktop,Core GUI APIs...
# 2
in that is a link to this one: http://www.javaworld.com/javaworld/jw-03-1999/jw-03-dragndrop.html which is going more into depth if youre questions aren't answered in the faq
jEti182a at 2007-7-10 11:36:30 > top of Java-index,Desktop,Core GUI APIs...
# 3
and this one... http://java.sun.com/docs/books/tutorial/uiswing/dnd/intro.html
suparenoa at 2007-7-10 11:36:30 > top of Java-index,Desktop,Core GUI APIs...
# 4

Here's an example of drag and drop of a cell in JTable.

You can move or copy (using Ctrl) a cell to another location:

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.GridLayout;

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.BorderFactory;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.ListSelectionModel;

import javax.swing.TransferHandler;

import javax.swing.table.DefaultTableModel;

public class DnD_Demo extends JFrame {

public DnD_Demo() {

setTitle("DnD Demo (Version 3)");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel(new GridLayout(1,1));

panel.add(createTable("Table A"));

getContentPane().add(panel,BorderLayout.CENTER);

pack();

}

private JPanel createTable(String tableId) {

DefaultTableModel model = new DefaultTableModel();

model.addColumn("Column 0");

model.addColumn("Column 1");

model.addColumn("Column 2");

model.addColumn("Column 3");

model.addRow(new String[]{tableId+" 00", tableId+" 01", tableId+" 02", tableId+" 03"});

model.addRow(new String[]{tableId+" 10", tableId+" 11", tableId+" 12", tableId+" 13"});

model.addRow(new String[]{tableId+" 20", tableId+" 21", tableId+" 22", tableId+" 23"});

model.addRow(new String[]{tableId+" 30", tableId+" 31", tableId+" 32", tableId+" 33"});

JTable table = new JTable(model);

table.getTableHeader().setReorderingAllowed(false);

table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

JScrollPane scrollPane = new JScrollPane(table);

scrollPane.setPreferredSize(new Dimension(400,100));

table.setDragEnabled(true);

table.setTransferHandler(new TableTransferHandler());

JPanel panel = new JPanel();

panel.add(scrollPane);

panel.setBorder(BorderFactory.createTitledBorder(tableId));

return panel;

}

public static void main(String[] args) {

new DnD_Demo().setVisible(true);

}

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) {

for (int i = 0; i < flavors.length; i++) {

if (DataFlavor.stringFlavor.equals(flavors[i])) {

return true;

}

}

return false;

}

}

class TableTransferHandler extends StringTransferHandler {

public JTable target;

public int row = -1;

public int col = -1;

protected String exportString(JComponent c) {

JTable table = (JTable)c;

row = table.getSelectedRow();

col = table.getSelectedColumn();

return table.getValueAt(row, col).toString();

}

protected void importString(JComponent c, String str) {

target = (JTable)c;

int row = target.getSelectedRow();

int col = target.getSelectedColumn();

target.getModel().setValueAt(str, row, col);

}

protected void cleanup(JComponent c, boolean remove) {

JTable table = (JTable)c;

if (remove)

table.getModel().setValueAt("", row, col);

}

}

}

Message was edited by:

Rodney_McKay

Rodney_McKaya at 2007-7-10 11:36:30 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks all for your help.. Also additionally I am using my Database to store all information of tasks. Whenever the task has been moved from one date to other, it should also be changed!
KINOa at 2007-7-10 11:36:30 > top of Java-index,Desktop,Core GUI APIs...
# 6

This means that :

1. There may be 1 or more tasks on a particular day/time. When we drag and drop that should be appended to the next time/day and nothing should be erased here.

2. I am using "Expand" checkbox wherein when user clicks on it the cells will show the tasks with details else only number of tasks are shown.

Kindly help me to resolve the problem with your ideas..

Thanks and Rgds,

Pradeep

KINOa at 2007-7-10 11:36:30 > top of Java-index,Desktop,Core GUI APIs...
# 7

Hello All..

Unfortunately, still I am not able to solve the problem!!

I dont think, my JTable will work as a string. Reason, I have a multiline cells for my JTable. When clicked on a "check" box it expands. This I feel I need to work like an object.

My plans : when i drag and drop (click on target cell) a dialog will open and I will select which text should be move from source to target. Rest should be in source. This works as I have a Vector for storing the text in multiline(Cell) JTable.

I am totally stuck with this problem since very long.. could anyone help me solve this issue?

Thanks a lot!!!!!!!!!!!!!!!!!!!!

Rgds,

Pradeep

KINOa at 2007-7-10 11:36:30 > top of Java-index,Desktop,Core GUI APIs...
# 8

> This works as I have a Vector for storing the text in multiline(Cell) JTable

Then you need to customize the import/export code. The export will need to create a text string delimited by some character. Then the import will need to parse this string and create the Vectore before storing it in your TableModel.

camickra at 2007-7-10 11:36:30 > top of Java-index,Desktop,Core GUI APIs...
# 9
Hello Friend,Thanks a lot for your suggesstion.. Can you give me an example how to drag and drop a Multiline(cell) Jtable?Awarded.. 3 Duke for your reply.. Thanks!Rgds,KINO
KINOa at 2007-7-10 11:36:30 > top of Java-index,Desktop,Core GUI APIs...