Java Swing Tutorial - Java DragSource .createDragGestureRecognizer (Class <T> recognizerAbstractClass, Component c, int actions, DragGestureListener dgl)








Syntax

DragSource.createDragGestureRecognizer(Class <T> recognizerAbstractClass, Component c, int actions, DragGestureListener dgl) has the following syntax.

public <T extends DragGestureRecognizer > T createDragGestureRecognizer(Class <T> recognizerAbstractClass,        Component c,        int actions,        DragGestureListener dgl)

Example

In the following code shows how to use DragSource.createDragGestureRecognizer(Class <T> recognizerAbstractClass, Component c, int actions, DragGestureListener dgl) method.

//  w w  w.j  a v  a  2s . co m
import java.awt.BorderLayout;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class Main extends JFrame implements DragGestureListener {

  DragSource ds = new DragSource();

  String[] items = { "Java", "C", "C++", "Lisp", "Perl", "Python" };

  JList jl = new JList(items);

  public Main() {
    super("Gesture Test");
    setSize(200, 150);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    getContentPane().add(new JScrollPane(jl), BorderLayout.CENTER);

    DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(jl, DnDConstants.ACTION_COPY,
        this);
    setVisible(true);
  }

  public void dragGestureRecognized(DragGestureEvent dge) {
    System.out.println("Drag Gesture Recognized!");
  }

  public static void main(String args[]) {
    new Main();
  }
}