Java Swing How to - Drag and drop between two JTextArea








Question

We would like to know how to drag and drop between two JTextArea.

Answer

  //w ww .j a  va2  s. c om

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    JTextArea textarea = new JTextArea();
    textarea.setDragEnabled(true);

    textarea.setText("Drag target");

    frame.getContentPane().add(BorderLayout.CENTER, textarea);

    JTextField textarea1 = new JTextField();
    textarea1.setText("Drop target");
    frame.getContentPane().add(BorderLayout.SOUTH, textarea1);

    frame.setSize(500, 300);
    frame.setVisible(true);
    frame.setLocation(100, 100);
  }
}