• 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

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){
row1.append(((FieldComparison)value).getField1());
row2.append(((FieldComparison)value).getField2());
} else {
row1.append(value.toString());
row2.append(value.toString());
}
}
if(j < (columns - 1)){
row1.append("\t");
row2.append("\t");
}
}
if(i < (rows - 1)){
row1.append("\n");
row2.append("\n");
//sb.append("\n");
}
sb.append(row1);
sb.append(row2);
}
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);
System.out.println(clipBoardContents);
}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!

Thanks!
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Check out the link mentioned below.
http://www.developeriq.com/articles/view_article.php?id=473&cid=27

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.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!