0

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 checking the chekboxes

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)
Ram
  • 1
  • 3
  • `There is no button action here only mouse release event.` - this seems to be wrong. You need to work with button action listeners. – SomeDude Aug 15 '16 at 12:02
  • But my project demands only mouse click events. There are no buttons on the form. – Ram Aug 15 '16 at 12:09
  • Is it not possible with out buttons? – Ram Aug 15 '16 at 12:09
  • Checkbox is a button – SomeDude Aug 15 '16 at 12:14
  • I'm using net beans IDE, to get check boxes in my jTable, I went to jtable properties and selected particular column as Boolean where I need check boxes. I didn't use any buttons as such. if Checkbox is a button can you please explain the logic behind it. – Ram Aug 15 '16 at 12:33
  • @trashgod your link is not working. Can somebody help me with the code. My entire project is on halt just for this. – Ram Aug 16 '16 at 06:13
  • I've updated my question and screenshots, code and the error message. Please somebody can help with the code. – Ram Aug 16 '16 at 06:30
  • As discussed [here](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender), model values of type `Boolean` are rendered and edited with a check box by default, for [example](http://stackoverflow.com/q/8739207/230513). – trashgod Aug 16 '16 at 06:32
  • When I selected 4 rows in T1 they are copied to T2. but when I uncheck the 1st check box in T1 then it deletes the all copied rows in T2. – Ram Aug 16 '16 at 06:42
  • You need to store the corresponding boolean data in the `TableModel`. For better guidance, please edit your question to include a [mcve] that shows your revised approach. – trashgod Aug 16 '16 at 12:30
  • isChecked = (Boolean) BillRedemptionTable.getValueAt(rownum[i], 1); isChecked is a Boolean value in which all checked items are stored. If you understood my issue can you please help me with the code part. Thank you @trashgod – Ram Aug 16 '16 at 13:18
  • Absent your [mcve], I don't have enough information to respond meaningfully. – trashgod Aug 16 '16 at 22:04
  • Hi guys I got the answer thanks to everyone – Ram Aug 17 '16 at 14:00

0 Answers0