Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:11:09+00:00 2026-05-13T08:11:09+00:00

I’m currently trying to receive key events during a drag and drop, but it

  • 0

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" http://img685.imageshack.us/img685/1884/pico.png
(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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:11:10+00:00Added an answer on May 13, 2026 at 8:11 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.