开发者

Prevent a JTable with a DragGestureListener from hijacking row resize mouse drag

开发者 https://www.devze.com 2023-04-11 04:08 出处:网络
I\'m working with a JTable that can have variable row heights. I also recently added Drag-and-drop support for my JTable by using a DragGestureListener to listen for drag events and call \"startDrag\"

I'm working with a JTable that can have variable row heights. I also recently added Drag-and-drop support for my JTable by using a DragGestureListener to listen for drag events and call "startDrag" if necessary.

The trouble I am having is that now even my row resize mouse movements are being captured by the DragGestureListener interface, and I need to find a way to filter out row drag gestures which are also row resizing gestures. Here's some representative code:

Class MyTable implements DragGestureListener
{
    .
    .
    .
    public void dragGestureRecognized(DragGestureEvent dge)
    {
        // Return early if selected row can't be dragged.

        /* ====== Attempt to filter out row resize drags: Attempt 1 ======= */
        if(getCursor().getType() == Cursor.N_RESIZE_CURSOR)
        {
            // Works, but does not seem robust. 
            return;
        }

        /* ====== Attempt to filter out row resize drags: Attempt 2 ======= */
        // Get starting position of mouse drag and the cell bounds of the cell in the row being dragged.
        Point dragStartPoint = dge.getDragOrigin(); 
        Rectangle cellRect = getCellRect(dragStartPoint);

        // Get mouse position relative to table.
        Point mousePosition = MouseInfo.getPointerInfo().getLocation();
        Point relativePosition = . . .; // calculate position relative to table.
        if(Math.abs(relativePosition.y - cellRect.y) < 5)
        {
            // Filter out a drag operation within 5 pixels of cell boundary
            // Seems to work, but is flaky, especially if row is resized quickly.
            return;  
        }
        .
        .
        .            
        dge.startDrag(DragSource.DefaultMoveDrop,
                      fDragImage, 
                      new Point(5,5), 
                      new StringSelection(""), // Transferable data
                 开发者_StackOverflow     myDragSourceListener); 
    }
    .
    .
    .
}

Now, what is the best / recommended / most robust way to filter out row resize drags from regular row drags?

NOTE: You've also noticed by now that I am using the java.awt.dnd package to achieve Drag-and-Drop and not the TransferHandler interface. In case you're curious, the only reason I am doing this is to have fine-grained control over drag and drop cues and drag images.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号