Copying Cell Values to the Clipboard from a JTable Component
Given the cell values in the example code below, typing control-c in a
table component while all cells have been selected causes the
following text to be copied to the clipboard. In particular, the cell
values are formatted as an HTML table.
Note: Control-c only works if the selected cells do not contain
a null value.
Here is the example code with initial cell values.
<html>
<table>
<tr>
<th id=0>A
<th id=1>B
<th id=2>C
<tr id=0>
<td>A1
<td>B1
<td>C1
<tr id=1>
<td>A2
<td>B2
<td>C2
<tr id=2>
<td>A3
<td>B3
<td>C3
</table>
</html>
JTable table = new JTable();
// Add data
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addColumn("A");
model.addColumn("B");
model.addColumn("C");
model.addRow(new Object[]{"A1", "B1", "C1"});
model.addRow(new Object[]{"A2", "B2", "C2"});
model.addRow(new Object[]{"A3", "B3", "C3"});
Post a comment