views:

84

answers:

1

I have this code to get a row from a JTable that is generated by a DragEvent for a DropTarget in some component C which may or may not be the JTable:

public int getRowFromDragEvent(DropTargetDragEvent event) {
        Point p = event.getLocation();
        if (event.getSource() != this.table)
        {
            SwingUtilities.convertPointToScreen(p, 
                event.getDropTargetContext().getComponent());
            SwingUtilities.convertPointFromScreen(p, this.table);
            if (!this.table.contains(p))
            {
                System.out.println("outside table, would be row 
                  "+this.table.rowAtPoint(p));
            }
        }
        return this.table.rowAtPoint(p);            
    }

The System.out.println is just a hack right now. What I'm wondering, is why the System.out.println doesn't always print "row -1". When the table is empty it does, but if I drag something onto the table's header row, I get the println with "row 0". This seems like a bug... or am I not understanding how rowAtPoint() works?

+1  A: 

Which version of Java do you have?

Looks like a bug to me: see http://bugs.sun.com/view_bug.do?bug_id=6291631

If this is the case for you, you can add an extra check:

if (!this.table.contains(p))
        {
            if(p.y < 0) 
              System.out.println("clicked above table");
            else
              System.out.println("outside table, would be row 
              "+this.table.rowAtPoint(p));
        }
Fortega
JRE 6u16. thanks!
Jason S
Jason S
Thats probably because it didn't receive any votes yet...
Fortega