title: Dragging a textarea
body: <p><strong>Code in Question:</strong></p>
  
  <pre><code>    textArea.addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
              posX = e.getX();
              posY = e.getY();
          }
      });
      textArea.addMouseMotionListener(new MouseAdapter() {
          public void mouseDragged(MouseEvent e) {
              setLocation(e.getXOnScreen() - posX, e.getYOnScreen() - posY);
          }
      });
  </code></pre>
  
  <p><strong>Background:</strong></p>
  
  <p>I have a JFrame, in that JFrame there is a JScrollPane, and in the JScrollPane there is a JTextArea called "textArea". This JTextArea take up the entire JFrame and the JFrame is undecorated. So to give some perspective, here is generally what the JFrame looks like...</p>
  
  <p><img src="https://i.stack.imgur.com/cF226.png" alt="example image"></p>
  
  <p>When the mouse clicks within the JTextArea and moves, the entire window is dragged. Everything is setup to not be focus able for this work, it's meant to be an overlay.</p>
  
  <p><strong>Issue:</strong></p>
  
  <p>The code listed above works fine and the world is at peace. But once there is enough text for the vertical scroll bar to appear (There is no horizontal because of line wrapping), dragging the window becomes an issue. When you click and just begin to move, the JFrame instantly moves much higher on the screen. The lines in JTextArea, the higher it moves up when you try to move it. I assume that the get*OnScreen() methods are issue because it's all relevant to the JTextArea.</p>
  
  <p><strong>Class in Question:</strong></p>
  
  <pre><code>import java.awt.Color;
  import java.awt.EventQueue;
  import java.awt.Font;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import javax.swing.JFrame;
  import javax.swing.JScrollPane;
  import javax.swing.JTextArea;
  import javax.swing.UIManager;
  import javax.swing.UnsupportedLookAndFeelException;
  
  public class Main extends JFrame {
      private JTextArea textArea;
      private JScrollPane textAreaScroll;
      private int posX = 0;
      private int posY = 0;
  
  public Main() {
      initComponents();
      initListeners();
      for(int i = 0; i &lt; 20; i++){
          addLine(i+" Hello");            
      }
  }
  
  public void addLine(String line){
      textArea.append("\n&gt; "+line);
      textArea.setCaretPosition(textArea.getDocument().getLength());
  }
  
  private void initListeners(){
      textArea.addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
              posX = e.getX();
              posY = e.getY();
          }
      });
      textArea.addMouseMotionListener(new MouseAdapter() {
          public void mouseDragged(MouseEvent e) {
              setLocation(e.getXOnScreen() - posX, e.getYOnScreen() - posY);
          }
      });
  }
  
  private void initComponents() {
      try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {}
  
      textAreaScroll = new JScrollPane();
      textArea = new JTextArea();
  
      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setUndecorated(true);
      setAlwaysOnTop(true);
      setAutoRequestFocus(false);
      setBackground(new Color(130,210,255,130));
      setFocusCycleRoot(false);
      setFocusable(false);
      setFocusableWindowState(false);
      setName("main");
      setOpacity(0.4f);
      setResizable(false);
  
      textAreaScroll.setBorder(null);
      textAreaScroll.setFocusable(false);
      textAreaScroll.setRequestFocusEnabled(false);
  
      textArea.setEditable(false);
      textArea.setBackground(new Color(0, 0, 0));
      textArea.setColumns(20);
      textArea.setFont(new Font("Consolas", 0, 14));
      textArea.setForeground(new Color(255, 255, 255));
      textArea.setLineWrap(true);
      textArea.setRows(5);
      textArea.setText("&gt; Hello world!\n&gt; another line!");
      textArea.setBorder(null);
      textArea.setFocusable(false);
      textArea.setRequestFocusEnabled(false);
      textAreaScroll.setViewportView(textArea);
  
      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(textAreaScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
              );
      layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(textAreaScroll, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
              );
  
      pack();
  }
  
  public static void main(String args[]) {
      EventQueue.invokeLater(new Runnable() {
          public void run() {
              new Main().setVisible(true);
          }
      });
  }
  </code></pre>
  
  <p>}</p>