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

    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.setSize(300,100);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLayout(new FlowLayout());
            dragLabel = new JLabel("text to drag");
            dragLabel.setTransferHandler(new TransferHandler("text"));
            MouseAdapter ma = new MouseAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    JComponent comp = (JComponent)e.getSource();
                    TransferHandler th = comp.getTransferHandler();
                    th.exportAsDrag(comp, e, TransferHandler.COPY);
                    super.mouseDragged(e);
                }
            };
            dragLabel.addMouseMotionListener(ma);
            dragLabel.addKeyListener(new KeyAdapter() {
     
                @Override
                public void keyPressed(KeyEvent e) {
                    dragLabel.setText("Key pressed: "+e.getKeyText(e.getKeyCode()));
                }
     
            });
            dragLabel.setFocusable(true);
            dragLabel.addFocusListener(new FocusListener() {
     
                public void focusGained(FocusEvent e) {
                    dragLabel.setForeground(Color.GREEN);
                }
     
                public void focusLost(FocusEvent e) {
                    dragLabel.setForeground(Color.RED);
                }
            });
            this.add(dragLabel);
        }
    }
    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
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

Similar Threads

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