Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Clipboard not copying right data  RSS feed

Dalia Sultana
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a JTable which displays different objects in different rows. I am trying to implement the copy paste function so that the user can do CTRL+C and paste the data in an excel spread sheet. Initially I implemented the toString method for all the objects that the table displays. But, I had to modify the copy functionality, so that, for each table row, 2 excel rows can be generated. This is how I do it.

private StringBuilder getTableData(){
StringBuilder sb = new StringBuilder();
int columns = table.getColumnCount();
int rows = table.getRowCount();
StringBuilder row1 = null;
StringBuilder row2 = null;
Object value = null;

for(int i = 0; i < rows; i++) {
row1 = new StringBuilder();
row2 = new StringBuilder();
for(int j = 0; j < columns; j++) {
if(table.getModel().getValueAt(i, j) != null){
value = table.getModel().getValueAt(i, j);
if(value instanceof FieldComparison){
} else {
if(j < (columns - 1)){
if(i < (rows - 1)){
return sb;

I copy this data to the clipboard as follows:

StringBuilder sb = getTableData();
StringSelection allData = new StringSelection(sb.toString());
cb.setContents(allData, allData);
try {
Transferable content = cb.getContents(null);
String clipBoardContents = (String)content.getTransferData(DataFlavor.stringFlavor);
}catch(Exception e){
System.out.println("Transfer data error " + e.toString());

At this point I can see the proper data in the console, However when I try to do a CTRL+V on the excel spreadsheet, the data appears as if the toString method was called on each object. It appears like the clipboard data that I added has been overridden somehow. Not sure what the problem is. Please Help!

Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Check out the link mentioned below.

However there is a small error in the class DataTable (JTable is wrongly typed as jtable). Type the data into the table and copy-paste it onto to the excel sheet.

Hope this solves your problem.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!