JTable headers to clipboard (WORKING CODE)
The following code gets selected fields in JTable into
a system clipboard. Now how to get the table headers
to the clipboard too, so that I don't have to retype
them when pasting the clipboard contents to MS Excel for example.
StringSelection stsel;
StringBuffer sbf = new StringBuffer();
int rowCount = tblDataTable.getRowCount ();
int colCount = tblDataTable.getColumnCount();
String headerCnt = tblDataTable.getAccessibleContext();
for (int col = 0; col < colCount; col++) {
sbf.append(tblDataTable.getValueAt(col));
}
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
sbf.append(tblDataTable.getValueAt(row,col));
if (col < colCount - 1) { sbf.append("\t"); }
}
sbf.append("\n");
}
stsel = new StringSelection(sbf.toString());
java.awt.datatransfer.Clipboard system = Toolkit.getDefaultToolkit().getSystemClipboard();
system.setContents(stsel, stsel);

