Java / Swing: JTable.rowAtPoint not working correctly for points outside the table?

I have this code to get a string from a JTable that is created by a DragEvent for a DropTarget in some C component, which may or may not be 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);            
    }

System.out.println is just a hack right now. I am wondering why System.out.println does not always print "line -1". When the table is empty, it does, but if I drag something into the title bar of the table, I get println with "row 0". This seems like a mistake ... or do I not understand how it works rowAtPoint()?

+3
source share
1 answer

What version of Java do you have?

: . http://bugs.sun.com/view_bug.do?bug_id=6291631

, :

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));
        }
+1

Source: https://habr.com/ru/post/1742299/


All Articles