This message retrieve selected values from a jTable and copies them into a line-separated string, where each line contains the cell-data of each row. - Java Swing

Java examples for Swing:JTable Row

Description

This message retrieve selected values from a jTable and copies them into a line-separated string, where each line contains the cell-data of each row.

Demo Code

/*//w  ww.j  ava 2 s. c o  m
 * Zettelkasten - nach Luhmann
 ** Copyright (C) 2001-2014 by Daniel L?decke (http://www.danielluedecke.de)
 * 
 * Homepage: http://zettelkasten.danielluedecke.de
 * 
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of 
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * Dieses Programm ist freie Software. Sie k?nnen es unter den Bedingungen der GNU
 * General Public License, wie von der Free Software Foundation ver?ffentlicht, weitergeben
 * und/oder modifizieren, entweder gem?? Version 3 der Lizenz oder (wenn Sie m?chten)
 * jeder sp?teren Version.
 * 
 * Die Ver?ffentlichung dieses Programms erfolgt in der Hoffnung, da? es Ihnen von Nutzen sein 
 * wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder 
 * der VERWENDBARKEIT F?R EINEN BESTIMMTEN ZWECK. Details finden Sie in der 
 * GNU General Public License.
 * 
 * Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm 
 * erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import javax.swing.JTable;

public class Main {
    /**
     * This message retrieve selected values from a jTable and copies them into a line-separated string,
     * where each line contains the cell-data of each row. each cell is tab-separated, each row is
     * newline-separated.<br><br>
     * Thus, a return value might look like this:<br>
     * {@code 3   This is the third entry}<br>
     * {@code 6   This is number six}<br>
     * {@code 9   My last entry}
     * @param table the table where the data was dragged (drag-source)
     * @return a prepared string that contains the copied data in proper "clipboard"-format.
     */
    public static String prepareStringForTransferHandler(JTable table) {
        int[] rows = table.getSelectedRows();
        StringBuilder sb = new StringBuilder("");
        if (rows.length > 0) {
            for (int row : rows) {
                sb.append(table.getValueAt(row, 0)).append("\t");
                sb.append(table.getValueAt(row, 1)).append("\n");
            }
        }
        return sb.toString();
    }
}

Related Tutorials