Example #1
0
  public DragLabel(String s) {
    this.setText(s);
    this.setOpaque(true);
    this.dragSource = DragSource.getDefaultDragSource();
    this.dgListener = new DGListener();
    this.dsListener = new DSListener();

    // component, action, listener
    this.dragSource.createDefaultDragGestureRecognizer(this, this.dragAction, this.dgListener);
  }
Example #2
0
  public DragLabel(String s, int a) {
    if (a != DnDConstants.ACTION_NONE
        && a != DnDConstants.ACTION_COPY
        && a != DnDConstants.ACTION_MOVE
        && a != DnDConstants.ACTION_COPY_OR_MOVE
        && a != DnDConstants.ACTION_LINK) throw new IllegalArgumentException("action" + a);

    this.dragAction = a;
    this.setText(s);
    this.setOpaque(true);
    this.dragSource = DragSource.getDefaultDragSource();
    this.dgListener = new DGListener();
    this.dsListener = new DSListener();

    // component, action, listener
    this.dragSource.createDefaultDragGestureRecognizer(this, this.dragAction, this.dgListener);
  }
Example #3
0
 public void init() {
   DragSource dragSource = DragSource.getDefaultDragSource();
   // 将srcLabel转换成拖放源,它能接受复制、移动两种操作
   dragSource.createDefaultDragGestureRecognizer(
       srcLabel,
       DnDConstants.ACTION_COPY_OR_MOVE,
       new DragGestureListener() {
         public void dragGestureRecognized(DragGestureEvent event) {
           // 将JLabel里的文本信息包装成Transferable对象
           String txt = srcLabel.getText();
           Transferable transferable = new StringSelection(txt);
           // 继续拖放操作,拖放过程中使用手状光标
           event.startDrag(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR), transferable);
         }
       });
   jf.add(new JScrollPane(srcLabel));
   jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   jf.pack();
   jf.setVisible(true);
 }