title: How should I drag content from one array of JTextFields to another?
body: <p>I figured out how to drag to highlight multiple cells in a  GridLayout-designed grid (which wasn't too hard) and how to drag a cell from one such grid to another (which involved brute force and math, but it turned out not to be all that hard, either). </p>
  
  <p>But the code looks and feels hacked.</p>
  
  <p><strong>How should I have done it?</strong></p>
  
  <p>Here are code fragments that typify what I did to drag content (one <code>char</code>):</p>
  
  <p>For each cell in <code>txtUser[]</code> grid add mouse listener to identify the cell about to be dragged and also access its content:</p>
  
  <pre><code>  txtUser[i].addMouseListener(new java.awt.event.MouseListener() 
    {
      public void mousePressed(MouseEvent e) {   
  
        currentUserCell.index = interp(e.getXOnScreen(), ulcUser.x, txtUser[0].getWidth());
  
        if(txtUser[currentUserCell.index].getText().length() &gt; 0)
  
          currentUserCell.content = txtUser[currentUserCell.index].getText().charAt(0);
      }
  </code></pre>
  
  <p>Here's <code>interp()</code>, which converts from absolute screen pixel (x) to (returned) grid element number, given the upper-left corner of the text field array and the width of one element:</p>
  
  <pre><code>  static int interp(int x, int ulc, int w){
  
      return (x - ulc)/w;
    }
  </code></pre>
  
  <p>If the user moves the frame, <code>interp()</code> above doesn't work, requiring need to <code>reorient()</code>:</p>
  
  <pre><code>  void reorient(){
  
      ulcGrid = new Point(cells[ 0][ 0].getLocationOnScreen().x, cells[ 0][ 0].getLocationOnScreen().y);
      ulcUser = new Point(txtUser[ 0]  .getLocationOnScreen().x, txtUser[ 0]  .getLocationOnScreen().y);
    }
  </code></pre>
  
  <p>(I tried to use relative pixel locations, but couldn't make it work. I may revisit this.)</p>
  
  <p>In order to drop the dragged content, the destination had better be <code>inbounds()</code>:</p>
  
  <pre><code>  boolean inbounds(int r, int c){
  
        return ! (r &gt;= N || c &gt;= N || r &lt; 0 || c &lt; 0);  
    }
  </code></pre>
  
  <p>If inbounds, the letter is dropped, as long as destination is empty:</p>
  
  <pre><code>  public  void mouseReleased(MouseEvent e) {
  
      int x, y;
  
        if(! dragging)
          return;
        dragging = false;
  
        x =  e.getLocationOnScreen().x;
        y =  e.getLocationOnScreen().y;
  
        int c = Utilities.interp(x, ulcGrid.x);
        int r = Utilities.interp(y, ulcGrid.y);
  
        if(! inbounds(r, c))
          return;
  
        if(cells[r][c].getText().length() &gt; 0)
          return;
  
        cells[r][c].setText("" + currentUserCell.content);
  </code></pre>
  
  <p>The previous method required a <code>MouseMotionAdapter</code> for each cell of the source array.</p>
  
  <p>And it just seems so hacked. One reason I say this is that I rely on several global variables, such as <code>ulcGrid</code> and <code>ulcUser</code> and <code>currentUserCell</code> and <code>dragging</code>:</p>
  
  <pre><code>  private  void txtUserMouseDragged(MouseEvent evt) 
    {                                         
      dragging = true;
    } 
  </code></pre>
  
  <p>I had a nice learning experience, but I'd rather have more-professional-looking code, most notably with fewer global variables. (I realize that a good start would be to not rely on absolute pixel addresses.)</p>
  
  <p><strong>So I'm asking where to find a better way, specifically how to identify the drag source and destination cells of a one- or two-dimensional array of text fields.</strong></p>
  
  <p>=================
  --EDIT--</p>
  
  <p>My program works. <strong>My question is</strong> about whether there is a library that would make it easier and more reliable than what I've written to drag from the one-dimenional array at the bottom of the screen below onto the large grid.</p>
  
  <p>But now that I've read the comments, maybe this is just another bad question that should be deleted. </p>
  
  <p><img src="https://i.stack.imgur.com/wCeJj.png" alt="enter image description here"></p>