• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Clipboard not copying right data

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!

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.
a wee bit from the empire
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic