I'm trying to solve this issue since past three days. I'm new to Java world. My task is,
There are two tables T1 and T2. T1's first column is checkbox. As soon as checkbox in T1 the selected row should be copied to T2.And also when I uncheck the checkbox, it should automatically remove the selected row from T2. There is no button action here only mouse release event.
private void setTotalAmounts() {
DefaultTableModel model = (DefaultTableModel) BillRedemptionTable.getModel();
DefaultTableModel model1 = (DefaultTableModel) jTable2.getModel();
int checkeditemno = 0;
int rownum[] = BillRedemptionTable.getSelectedRows();
for (int i = 0; i < rownum.length; i++) {
boolean isChecked;
isChecked = (Boolean) BillRedemptionTable.getValueAt(rownum[i], 1);
if (isChecked) {
try {
checkeditemno++;
//fetch values from first table rows
int billno = Integer.parseInt((String) model.getValueAt(rownum[i], 2));
int billamount = Integer.parseInt((String) model.getValueAt(rownum[i], 4));
String artype = (String) model.getValueAt(rownum[i], 5);
//add the above values to second table row
model1.addRow(new Object[]{billno, billamount, artype});
} catch (NumberFormatException e) {
System.err.println(e.getMessage());
}
} else if (!isChecked) {
//this code is to remove rows from second table where checkboxes unchecked in the first table
for (int j = 0; j < jTable2.getRowCount(); j--) {
model1.removeRow(rownum[i]);
}
}
}
}
This is the code i'm using currently.
Can any one help me out here...
when unchecking one row (row number 8)
When unchecked one row, I got the below error in Netbeans IDE
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.removeElementAt(Vector.java:558)
at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:462)
at org.pawnshop.ui.BillRedemption.setTotalAmounts(BillRedemption.java:118)
at org.pawnshop.ui.BillRedemption.BillRedemptionTableMouseReleased(BillRedemption.java:479)
at org.pawnshop.ui.BillRedemption.access$300(BillRedemption.java:37)
at org.pawnshop.ui.BillRedemption$4.mouseReleased(BillRedemption.java:356)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)