Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4481452

JTable drop (import) support: misleading documentation OR bug

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs



      Name: bsC130419 Date: 07/18/2001


      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)

      The Swing Data Transfer document included with the docs for 1.4beta states:
      "There are four components that do not have default TransferHandlers:
      JFileChooser, JList, JTable, and JTree. For these four components, a DropTarget
      is installed that indicates a potential insertion point. All that is required
      to fully support imports is for the developer to write and install a custom
      TransferHandler."

      This is not true in the case of JTable.

      First of all, there is no indication of a potential insertion point that I can
      see while dragging an item into the table.

      Secondly, after writing a custom TransferHandler, I realized that a mechanism
      is completely lacking by which to determine what cell within a JTable the drop
      occurred over. To accomplish this, it is necessary to set a new DropTarget on
      the JTable with a custom DropTargetListener that can intercept the drop event
      and determine the cursor location at the time of drop.

      Either the documentation is incorrect or a facility for determining drop
      locations is lacking in the current TransferHandler design.

      --------------- code ---------------

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

      /**
       * the purpose of this class is to illustrate that adding a custom
       * TransferHandler is NOT sufficient for drop-enabling a JTable
       */
      class JTableNoDrop {

          static class TestTransferHandler extends TransferHandler {
              public TestTransferHandler() {
                  // we don't want to transfer properties, which is what the
                  // superclass does
                  super(null);
              }

              public boolean canImport(JComponent comp,DataFlavor[] flavors) {
                  // just cheat and return true (accept all imports)
                  return true;
              }

              public boolean importData(JComponent comp,Transferable t) {
                  System.out.println("TestTransferHandler would import data here...");
                  // I could cast comp to a JTable and do setValueAt(row,col) here with
                  // the data in t, but how would I know what row & col are? I can't
                  // without writing a DropTargetListener, associating it with a new
                  // DropTarget, and setting that DropTarget on the JTable.
                  return false;
              }

              // the remaining methods in TransferHandler seem to deal with exporting
              // data, so we won't bother overriding them

          }

          public static void main(String[] args) {

              final int SIZE = 5;

              // set up the table to drag from
              Object[][] junkData = new Object[SIZE][SIZE];
              for (int i = 0; i < SIZE; i++) {
                  for (int j = 0; j < SIZE; j++) {
                      junkData[i][j] = "Object " + (SIZE * (i+j));
                  }
              }
              JTable dragTable = new JTable(junkData,new Object[] {"A","B","C","D","E"});
              dragTable.setDragEnabled(true); // this works fine to enable drags

              JLabel label = new JLabel("Try to drag from top table to bottom.");

              JTable dropTable = new JTable(SIZE,SIZE);

              // This will NOT be sufficient for drops
              // There is also NO indication of a "potential drop point" as per the
              // Swing data transfer docs
              dropTable.setTransferHandler(new TestTransferHandler());

              JFrame frame = new JFrame("JTable Drop Test");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.getContentPane().setLayout(new BorderLayout());

              frame.getContentPane().add(dragTable,BorderLayout.NORTH);
              frame.getContentPane().add(label,BorderLayout.CENTER);
              frame.getContentPane().add(dropTable,BorderLayout.SOUTH);

              frame.pack();
              frame.setVisible(true);
          }

      }

      (Review ID: 128358)
      ======================================================================

            shickeysunw Shannon Hickey (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: