Results 1 to 8 of 8
  1. #1
    mramjava's Avatar
    mramjava is offline Member
    Join Date
    Aug 2016
    Posts
    3
    Rep Power
    0

    Default 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,
    Java 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,
    How to copy checked rows from one jTable to another?-code1.png
    How to copy checked rows from one jTable to another?-code2.png

    can any one please help me urgently.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to copy checked rows from one jTable to another?

    Can you put a listener on the checkbox and then simply react to the clicking on that?
    It looks to me like you are attempting to refresh the entire second table at the moment.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to copy checked rows from one jTable to another?

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    mramjava's Avatar
    mramjava is offline Member
    Join Date
    Aug 2016
    Posts
    3
    Rep Power
    0

    Default Re: How to copy checked rows from one jTable to another?

    I'm using Net beans 8 for this project. I've added those check boxes in the jTable properties. How can I put a listener to those check boxes? The second table refreshes on the selections in the first table. My code is not working as per my requirements, please suggest some other way if possible.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to copy checked rows from one jTable to another?

    I haven't ever used a Swing GUI builder (well, apart from one go over a decade ago). Swing was never designed to work with them, and it usually shows in the code that ends up being produced.

    Surely it allows you to add a listener to a checkbox?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    mramjava's Avatar
    mramjava is offline Member
    Join Date
    Aug 2016
    Posts
    3
    Rep Power
    0

    Default Re: How to copy checked rows from one jTable to another?

    If I add checkbox on a jFrame it is possible to add a listner to it. But now it's the case of a jTable with checkboxes, which makes it hard to add individual listeners to checkboxes. If you look at my code, it clearly shows how i'm getting perticular checked row details.

    Boolean isChecked;
    isChecked = (Boolean) jtable1.getValueAt(rownum[i], 0);

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to copy checked rows from one jTable to another?

    Well, my first suggestion would be to not use the GUI builder.

    My second would be to use a custom TableModel that reacts to setValueAt. Specifically the value where the checkbox is.
    Add a TableModelListener to that model and setValueAt should notify whenever the checkbox changes, supplying the row, or maybe the object representing that row of data.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to copy checked rows from one jTable to another?

    Rob Camick's Table Cell Listener might be useful here...
    https://tips4java.wordpress.com/2009...cell-listener/

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. copy & paste of JTable rows
    By javice in forum AWT / Swing
    Replies: 0
    Last Post: 05-23-2012, 10:42 PM
  2. Jtable rows
    By riddhishah28 in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2011, 06:24 AM
  3. How to Add,Remove Rows in JTable
    By Dinesh_rockz in forum AWT / Swing
    Replies: 2
    Last Post: 02-03-2011, 05:30 AM
  4. Highlight rows of a jtable
    By simmi in forum AWT / Swing
    Replies: 5
    Last Post: 08-20-2009, 01:13 PM
  5. Deleting All rows in the JTable
    By surot in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 10:44 AM

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
  •