JDK-4165575 : JTable doesn't support drag and drop correctly
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0,1.2.1,1.2.2
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.6,windows_nt
  • CPU: generic,x86
  • Submitted: 1998-08-11
  • Updated: 2001-07-12
  • Resolved: 2001-07-12
Related Reports
Duplicate :  
Description
The built-in selection mechanism in JTable interferes with drag gesture recognition in DragGestureRecognizer (drag-n-drop API), which prevent drag-n-drop with selection in JTable from working. 

JTable needs some specialized work to support drag and drop correctly.

Name: rlT66838			Date: 06/10/99


Take the example program below to try out these three test cases

1)
Hold the CTRL key down and click on the top JTable entry.
Release the CTRL key and do not release the mouse button.  Then, 
drag down off the JTable and then release the mouse button.
The cursor is now all messed up and nothing works anymore.

2)
Hold the SHIFT key (MOVE KEY) down and try to drag off the 
JTable.  Nothing happens.

3)
Hold the CTRL key down.  Drag off the JTable and go over the 
drop target.  Notice the cursor is a NO cursor - all is OK so
far.  Now release the CTRL key so you can be in move mode.
A drop action occurs.  Should not.


import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.*;


public class dnd 
{
	public static DragSourceListener  m_dSourceListen;
	public static String[] COLUMNS = { "1", "2", "3" };
	public static String[][] ROWS = { { "1", "1", "1" },
									  { "2", "2", "2" },
									  { "3", "3", "3" } };

	public static void main(String[] args)
	{
		JFrame frame = new JFrame();
		frame.getContentPane().setLayout(null);
		frame.setBounds(20,20, 700, 700);

		JTable table = new JTable(ROWS, COLUMNS);
		table.setBounds(0, 0, 200, 200);
		frame.getContentPane().add(table);


		JLabel labelTarget = new JLabel("THE TARGET");
		labelTarget.setBounds(0, 300, 100, 100);
		labelTarget.setBackground(Color.green);
		frame.getContentPane().add(labelTarget);

		// setup the drag source
		DragSource dSource = DragSource.getDefaultDragSource();
		SymGestListener dGestListen = new SymGestListener();
		m_dSourceListen = new SymSourceListener();
		dSource.createDefaultDragGestureRecognizer(table, DnDConstants.ACTION_MOVE, dGestListen);

		// setup the drop source
		DropTarget t = new DropTarget(labelTarget, 
									  DnDConstants.ACTION_MOVE,
									  new SymDropListener(),
									  true);

		frame.show();
	}

	public static class SymGestListener implements DragGestureListener
	{
		public void dragGestureRecognized(DragGestureEvent dge) 
		{
			dge.startDrag(DragSource.DefaultCopyNoDrop, new StringSelection("TEST"), m_dSourceListen);
		}
	}

	public static class SymSourceListener implements DragSourceListener
	{
		public void dragEnter(DragSourceDragEvent dsde) 
		{
		}
		public void dragOver(DragSourceDragEvent dsde)
		{
		}
		public void dropActionChanged(DragSourceDragEvent dsde)
		{
		}
		public void dragExit(DragSourceEvent dsde)
		{
		}
		public void dragDropEnd(DragSourceDropEvent dsde)
		{
		}
	}

	public static class SymDropListener implements DropTargetListener
	{
		public void dragEnter(DropTargetDragEvent dtde)
		{
			dtde.acceptDrag(DnDConstants.ACTION_MOVE);
		}

		public void dragOver(DropTargetDragEvent dtde)
		{
			dtde.acceptDrag(DnDConstants.ACTION_MOVE);
		}
		
		public void dropActionChanged(DropTargetDragEvent dtde)
		{
			System.out.println("Got Drop Action Change Event");
			dtde.acceptDrag(DnDConstants.ACTION_MOVE);
		}

		public void dragExit(DropTargetEvent dte)
		{
		}

		public void drop(DropTargetDropEvent dtde)
		{
			System.out.println("Got Drop event");
			dtde.acceptDrop(DnDConstants.ACTION_MOVE);
		}
	}
}
(Review ID: 84212)
======================================================================

Comments
EVALUATION This is something that Philip needs to add to JTable when he has time. nancy.schorr@eng 1999-05-13 This was done as part of 4290983, refer to it for details on enabling dnd support in table. scott.violet@eng 2001-07-12
13-05-1999