Copy JTable column to Clipboard?

0

Here I have a table template with 4 columns and several filled lines that I get in the database. I would like to create an Action button that copies columns 2 and 3 at once to the clipboard, so you can paste, for example, into an Excel or other places. It's possible?

Follow the Jtable template I'm using.

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

public class MinhaTabelaEditavel extends AbstractTableModel {
private ArrayList linhas = null;
private String[] colunas = null;

public MinhaTabelaEditavel (ArrayList lin, String[] col){
    setLinhas(lin);
    setColunas(col);    
}

public ArrayList getLinhas() {
    return linhas;
}

public void setLinhas(ArrayList dados) {
    this.linhas = dados;
}

public String[] getColunas() {
    return colunas;
}

public void setColunas(String[] nome) {
    this.colunas = nome;
}

public int getColumnCount(){
    return colunas.length;
}

public int getRowCount (){
    return linhas.size();
}

public String getColumnName (int numCol){
    return colunas[numCol];
}

public Object getValueAt (int numLin, int numCol){
    Object[] linha = (Object[])getLinhas().get(numLin);
    return linha[numCol];
}

@Override
public boolean isCellEditable(int row, int column) {
return true;
}
    
asked by anonymous 29.01.2018 / 11:37

1 answer

2

As this answer in SOEn, you will need to convert the text to a text type which you can copy to the clipboard, then just send the text normally through the Clipboard , as below:

StringSelection stringSelection = new StringSelection(<texto a ser copiado>);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);

In your case, it would suffice to retrieve the text to be copied from the desired columns using the getValueAt() method and passing it to StringSelection .

See an example:

    
31.01.2018 / 18:09