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.
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...