Results 1 to 2 of 2
  1. #1
    qwerty4030 is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default Drag and Drop disables Event Listeners?

    When a exportAsDrag() is called, all event listeners stop working until the drag is completed. Is there a way to get at least keylistener to work or catch the keyboard input somewhere?
    Here is a simple compilable program that creates a jframe with a jlabel inside. The label has a mouselistener, focuslistener, and keylistener.
    The mouselistener calls exportAsDrag from the mousedragged event, the focus listener changes the text color (focus = green, not focused = red). The keylistener changes the label text to the key pressed.
    Dnd works correctly: you can drag the label text to any program native, java ,ect... but i really want the keylistener to work WHILE dragging. If this is not possible why does dragging disable the event listeners or cause them not to function?
    Problem: Keylistener, Focuslistener.. ect stop working when exportasdrag is called.
    Java Code:
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.TransferHandler;
    public class Dnd extends JFrame {
        public static void main(String[] args) {
            new Dnd().setVisible(true);
        JLabel dragLabel;
       public Dnd() {
           this.setTitle("Drag and drop");
            this.setLayout(new FlowLayout());
            dragLabel = new JLabel("text to drag");
            dragLabel.setTransferHandler(new TransferHandler("text"));
            MouseAdapter ma = new MouseAdapter() {
                public void mouseDragged(MouseEvent e) {
                    JComponent comp = (JComponent)e.getSource();
                    TransferHandler th = comp.getTransferHandler();
                    th.exportAsDrag(comp, e, TransferHandler.COPY);
            dragLabel.addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e) {
                    dragLabel.setText("Key pressed: "+e.getKeyText(e.getKeyCode()));
            dragLabel.addFocusListener(new FocusListener() {
                public void focusGained(FocusEvent e) {
                public void focusLost(FocusEvent e) {
    The program I want to this to work with will have a graphical display in a window and a user should be able to drag components around the display as well as to other windows/displays/dialogs ect... The problem is that the graphical display is larger than the window so it is scrollable by using the arrow keys. I want the user to click&hold(drag) on a component and be able to scroll using the arrow keys and drop to a component that was offscreen when the drag initiated.
    I think a possible solution is to use a mouselistener to do the drag operations manually within the window, and on mouseexited call exportasdrag. However this might cause conflicting behavior if you drag a component off and back into the window so I would prefer only one implementation of dnd. I also noticed this behavior occurs is practically all applications: during a drag operations, key input is ignored...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

Similar Threads

  1. drag and drop - disable intra component drop
    By tomba in forum AWT / Swing
    Replies: 4
    Last Post: 12-09-2009, 02:01 PM
  2. Drag and drop
    By thayalan in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2009, 04:04 PM
  3. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 03:10 PM
  4. Multiple listeners per event
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-03-2008, 11:06 AM
  5. Action Event and Listeners
    By lost1 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 05:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts