|
-
August 16th, 2016, 04:43 AM
#1
How to copy checked rows from one jTable to another?
Hi All,
My task is like below,
1. I've a data in Table t1 with first column as checkboxes.
2. when ever i check the particular checkbox, the entire row must be copied to another table.
3. when i uncheck any particula checkbox from the first table, that particular copied row must be deleted from second table.
The above is my issue past 4 days. Guys any one who could help me with the code and explanation. I really am stuck at this in entire project. I've tried a code for this,
Code:
DefaultTableModel model = (DefaultTableModel) jtable1.getModel();
DefaultTableModel model1 = (DefaultTableModel) jtable2.getModel();
int checkeditemno = 0;
int rownum[] = jtable1.getSelectedRows();
for (int i = 0; i < rownum.length; i++) {
Boolean isChecked;
isChecked = (Boolean) jtable1.getValueAt(rownum[i], 0);
if (isChecked) {
try {
checkeditemno++;
String name = (String) model.getValueAt(rownum[i], 1);
String phone = (String) model.getValueAt(rownum[i], 2);
model1.addRow(new Object[]{name, phone});
} catch (NumberFormatException e) {
System.err.println(e.getMessage());
}
} else if (!isChecked) {
int rows = model1.getRowCount();
// isChecked = (Boolean) model.getValueAt(rownum[i], 0);
{
for (int j = rows - 1; j >= 0; j--) {
model1.removeRow(j);
checkeditemno--;
}
}
}
}
Errors in the code are
1. when i uncheck first checkbox, it deletes all the rows in the second table. The screen shots are below,
code1.PNGcode2.PNG
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|