我在过去三天试图解决这个问题。我是Java世界的新手。我的任务是,
有两个表T1和T2。 T1的第一列是复选框。 一旦T1中的复选框将所选行复制到T2。当我取消选中该复选框时,它应自动从T2中删除所选行。 这里只有鼠标释放事件没有按钮动作。
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]);
}
}
}
}
这是我目前正在使用的代码。
任何人都可以帮助我......
when unchecking one row (row number 8)
如果取消选中一行,我在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)