Reputation: 3
Here's what I am trying to do, I want to make a panel follow the cursor after clicking on it, my initial idea is doing it with a drag and drop operation but the problem is the clic must be pressed for the panel to be dragged and I want the panel to be dragged just by clicking on it without keeping the clic pressed. ANY IDEAS? Thanks guys :)
public class Main {
public static void main(String[] argv) throws Exception {
JComponent panDrag = new DraggableComponent();
Icon newi = new ImageIcon("src/red.png");
JLabel labelImage = new JLabel(newi);
panDrag.add(labelImage);
JPanel panneau = new JPanel();
panneau.add(panDrag);
JFrame f = new JFrame();
f.add(panneau);
f.setSize(300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class DraggableComponent extends JPanel implements DragGestureListener, DragSourceListener, MouseListener {
DragSource dragSource;
public DraggableComponent() {
dragSource = new DragSource();
dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
this.setBackground(new Color(255, 0, 0));
this.addMouseListener(this);
}
public void dragGestureRecognized(DragGestureEvent evt) {
Transferable t = new StringSelection("aString");
dragSource.startDrag(evt, DragSource.DefaultCopyDrop, t, this);
}
public void dragEnter(DragSourceDragEvent evt) {
System.out.println("enters");
}
final JButton clickTwiceButton = new JButton();
final JButton fireEventButton = new JButton();
public void dragOver(DragSourceDragEvent evt) {
System.out.println("over" + evt.getX() + "-" + evt.getY());
this.setLocation(evt.getX() - 50, evt.getY() - 70);
}
public void dragExit(DragSourceEvent evt) {
System.out.println("leaves");
}
public void dropActionChanged(DragSourceDragEvent evt) {
System.out.println("changes the drag action between copy or move");
}
public void dragDropEnd(DragSourceDropEvent evt) {
System.out.println("finishes or cancels the drag operation");
// Invoking later for no reason, just to simulate your code
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
System.out.println("sdffdf");
clickTwiceButton.dispatchEvent(new MouseEvent(
fireEventButton,
MouseEvent.MOUSE_CLICKED,
1,
MouseEvent.BUTTON1,
0, 0,
1,
true
));
}
});
}
//******************
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouse clic");
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouse enter");
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouse exit");
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouse press");
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouse release");
//MouseEvent press = new MouseEvent(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
//this.mousePressed(press );
}
}
With this I can only drag the panel and make keep it visible which I need too, but I sill have to keep the clic pressed which I don't want :(
Upvotes: 0
Views: 78
Reputation: 324157
I would probably be easier to just do you own custom listener for the dragging.
You could start with the DragListener
found in the Moving Windows blog.
You would need to modify the code to handle the mouseMoved(...)
event instead of the mouseDragged(...)
event. You would probably need to add a "dragging" variable that you would toggle when ever the mousePressed(...)
event is generated. The first click would set the dragging variable to "true" and the second click would set the variable to "false". Then the mouseMoved(...)
code would only be executed when the variable is "true".
Upvotes: 1