views:

203

answers:

1

Hi there,

I'm currently trying to receive key events during a drag and drop, but it seems to me that the focus is taken away while dragging so that I can't listen to any key events.

I'm dragging a JComponent subclass that implements KeyListener and requests the focus in the DragSourceListener's dragEnter method, but my assumption is that the focus is taken away from it afterwards.

Now, who's got the focus and how can I take it away back to my JComponent. Or is there a different approach that is more suitable for dnd?

Thank you in advance.

UPDATE:

It's a lot of code necessary to make this work so I'm only going to post some snippets to show you what I'm trying to do:

public class Stone extends JComponent implements Serializable, KeyListener {

    public Stone(...) {

     //...

     setFocusable(true);
     addKeyListener(this);

     this.dragSource = DragSource.getDefaultDragSource();
     this.dgListener = new StoneDGListener();
     this.dsListener = new StoneDSListener();

     this.dragSource.createDefaultDragGestureRecognizer(
      this, 
      DnDConstants.ACTION_MOVE, 
      this.dgListener
     );

     //...

    }

    //...

    public void keyPressed(KeyEvent e) {
        System.out.println("Stone: "+e.getKeyCode());
    }

    //...

    public class StoneDSListener implements DragSourceListener, Serializable {

     //...  

     @Override
     public void dragEnter(DragSourceDragEvent dsde) {
      //... 
      Stone.this.requestFocus();
      addKeyListener(Stone.this);
     }

     //...
    }
}

What happens is that before I'm dragging the Stone component my JPanel has the focus so it receives any keys I'm pressing. During the drag I can't listen to any pressed keys(so I don't know who's got the focus) even though I'm requesting it when in dragEnter() and after I release the Stone any key events are send to the Stone.

It's probably not important for the question but to illustrate what I'm doing here's a screenshot:

image showing the "drag" (Here I'm dragging the Stone from the collection below to the game field on the top). In this state I don't know how to find out what keys are pressed. I need to figure this out in order to be able to rotate the Stone.

+2  A: 

Not sure who has focus during a drag and drop. But an alternative solution to your problem would be to add a KeyEventDispatcher for your Stone class to the KeyboardFocusManager. From the JavaDoc:

The KeyboardFocusManager is both a centralized location for client code to query for the focus owner and initiate focus changes, and an event dispatcher for all FocusEvents, WindowEvents related to focus, and KeyEvents+.

+ my emphasis.

Basically we use similar sort of code to intercept KeyEvents before they hit the Component that has focus.

Just gave it a quick test for your particular drag and drop context and it seems to work alright (as long as your application has focus within the operating system). Essentially something along the lines of:

Public Stone(...) {
    // ...

    KeyboardFocusManager fm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    fm.addKeyEventDispatcher(
        new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(KeyEvent e) {
                System.out.println("Key Press: " + e.getKeyChar());
                return false;
            }            
        } 
    );

   // ... 
}

You will need to do a bit of leg-work on enabling and disabling when the user is no longer dragging and dropping as my test currently prints all the time.

I also wonder if it is possible to use the KeyboardFocusManager to determine who actually ends up with focus during a drag and drop?

Anyway, I hope this gives you a few new ideas to try.

Clinton
That looks really promising and works very well so far. I haven't had time to work out the enabling/disabling part, but that doesn't seem to be very complicated. I'll accept your answer as soon as I found the time to give this a try. Thanks a lot !
André Hoffmann
Worked very well. Thanks!
André Hoffmann