title: Copy cell data from TableView to clipboard
body: <p>I have a TableView with -depending on user input- 4 to 10 columns. I want to be able to copy selected cells to the system clipboard. I tried this piece of code that I found in SO, but <code>position.getColumn()</code> always returns -1, so it doesn´t work. Also, <code>table.getSelectionModel().getSelectedCells()</code> returns only as many items as there are rows. I expected it to return one item per cell instead. Can someone point out my error?</p>
  
  <pre><code>TableView&lt;TableEntry&gt; table = new TableView&lt;&gt;();
          //copy to clipboard
          table.setOnKeyPressed(e -&gt; {
              if (e.getCode() == KeyCode.C &amp;&amp; e.isControlDown()) {
                  StringBuilder clipboardString = new StringBuilder();
  
                  ObservableList&lt;TablePosition&gt; positionList = table.getSelectionModel().getSelectedCells();
  
                  int prevRow = -1;
  
                  for (TablePosition position : positionList) {
  
                      int row = position.getRow();
                      int col = position.getColumn();
  
  
                      Object cell = (Object) table.getColumns().get(col).getCellData(row);
  
                      // null-check: provide empty string for nulls
                      if (cell == null) {
                          cell = "";
                      }
  
                      // determine whether we advance in a row (tab) or a column
                      // (newline).
                      if (prevRow == row) {
  
                          clipboardString.append('\t');
  
                      } else if (prevRow != -1) {
  
                          clipboardString.append('\n');
  
                      }
  
                      // create string from cell
                      String text = cell.toString();
  
                      // add new item to clipboard
                      clipboardString.append(text);
  
                      // remember previous
                      prevRow = row;
                  }
  
                  // create clipboard content
                  final ClipboardContent clipboardContent = new ClipboardContent();
                  clipboardContent.putString(clipboardString.toString());
  
                  // set clipboard content
                  Clipboard.getSystemClipboard().setContent(clipboardContent);
              }
  
          });
  </code></pre>