0

I have to 2 tables. JTable1 and Jtable2. Jtable1 has data displayed from the database. Jtable2 should be having checkbox for every row in Jtable1. I want number of rows in Jtable2 equal Jtable1. These 2 tables are beside eachother.

Here is an example:

https://i.stack.imgur.com/SmVEG.jpg

In this example there is a checkbox beside every data item. I want a checkbox generated in JTable2 for every data item in Jtable1.

Here is a picture to get a better idea:

https://i.stack.imgur.com/pNvNa.png

Here is code:

   public void fetch()
     {

    try
    {

    String sqlp = "select * from american";
 pst =   (OraclePreparedStatement) conn.prepareStatement(sqlp);   
  rs =    (OracleResultSet) pst.executeQuery(sqlp);

   jTable1.setModel(DbUtils.resultSetToTableModel(rs));


  row =jTable1.getRowCount();

   while(i!=row)
     {
    jTable2.add(check);
    }  
    }
   catch(Exception ex )
  {
    JOptionPane.showMessageDialog(null, ex);
  }

    }
camickr
  • 321,443
  • 19
  • 166
  • 288
beginner
  • 29
  • 1
  • 9
  • If this is not a duplicate, please edit your question to include a [mcve] that shows your revised approach after reviewing these [examples](http://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bjtable%5d%20getcolumnclass%20jcheckbox%20). – trashgod Oct 15 '16 at 09:34
  • i want checkbox itself to be displayed. Not any selected values or anything. – beginner Oct 15 '16 at 09:34
  • For [example](http://stackoverflow.com/a/4528604/230513). – trashgod Oct 15 '16 at 09:35
  • this example shows selection of checkboxes. In my case, checkbox itself is not showing for every value in the 1st table. I want just the checkbox in each row of column 'choice' for each row in `JTable1` – beginner Oct 15 '16 at 09:38
  • Please cite an example that conforms to your requirements. – trashgod Oct 15 '16 at 09:39
  • I edited the post. Posted an example – beginner Oct 15 '16 at 09:46
  • I need help in this, none of the questions similar to this have answered my question. I really need help. Please do not mark this duplicate – beginner Oct 15 '16 at 09:56
  • Without your [mcve], I don't know where you're stuck, so I really don't know where to start. If your question is is not a duplicate, then it's certainly too broad. – trashgod Oct 15 '16 at 10:20
  • You might start with this complete [example](http://stackoverflow.com/a/34742409/230513) – trashgod Oct 15 '16 at 10:29

1 Answers1

1
jTable2.add(check);

You can't just add a JCheckBox to every row in the table. A table does not contain Swing components. It renders data contained in the TableModel of the table.

You need to add data (representing a check box) to the TableModel used by the table.

The DbUtils class returns a TableModel complete with data. So you have a couple of options:

  1. Use the DefaultTableModel returned by the DbUtils class and add a new column of data to the model. Read the DefaultTableModel API. You can use the addColumn(...) method to add a new column of data to the model.

The column is added to the end of the model so you will then need to change the view of the table to display the column at the beginning of the table. You can use the moveColumn(...) method of the JTable to do this.

Finally when you create the JTable you will need to override the getColumnClass(...) method to return Boolean.class so the proper renderer/editor can be used.

  1. Another option is to create a custom TableModel that wraps your current model with a checkbox. Check out: How to add checkbox in Jtable populated using rs2xml for an example of this approach

  2. Finally, just don't use DbUtils, then you can be fully in control of the data in your TableModel. Check out: Java Resultset to JTable with Checkbox for an example of this approach.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288