0

I created a query to get the required data but have failed to get the JTable to populate. The table just appears empty.

Essentially, I want to get an ID from one JFrame click a button, and open another frame that contains a table connected to the database but now only have it display data related to that ID instead of all the data in that database.

Is there another way I could execute what I want to do?

Update: Managed to figure it out, turns out it was the if statement messing with my rs.next() value causing the while statement not to execute and the lack of columns added to the model for the values to be populated under.

FamilyProfile one = new FamilyProfile();
   
String query ="SELECT * FROM `family_db` WHERE Parent_ID=?";
try {
    ps = db_connect.prepareStatement(query);
    
    ps.setString(1, famID);
    rs = ps.executeQuery();

    DefaultTableModel model =  (DefaultTableModel) one.Famtbl.getModel();
    model.addColumn("Family_ID");
    model.addColumn("Family_Name");
    model.addColumn("Parent_ID");
    model.addColumn("Child_ID");
    while(rs.next())
    { 
        String n = rs.getString("Family_ID");
        String e= rs.getString("Family_Name");
        String r= rs.getString("Parent_ID");
        String d= rs.getString("Child_ID");
       
        // This will add row from the DB as the last row in the JTable. 
        //model.addRow(new Object[] {n, e, r, d});
        model.insertRow(one.Famtbl.getRowCount(), new Object[] {n, e, r, d});
    } 
    one.setVisible(true);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Reis Bash
  • 1
  • 2
  • Your `JTable`'s model is *not* a `DefaultTableModel`, and so casting it as one will not work. Instead, it appears to hold a `JTableBinding$BindingTableModel`. – Hovercraft Full Of Eels Aug 05 '21 at 10:55
  • [Here is one API entry for your table model](https://doc.formdev.com/beansbinding/org/jdesktop/swingbinding/JTableBinding.html). You obtained the table by binding, and it appears that you cannot simply add a row of data to this type of model. If you did, wouldn't it "break" the binding? – Hovercraft Full Of Eels Aug 05 '21 at 10:58
  • I removed the binding and just left the table blank. Now how do I get the table to update? – Reis Bash Aug 05 '21 at 11:12
  • Your code is create a TableModel, but I don't see where you use the `setModel()` method to add the model to your JTable. See: https://stackoverflow.com/questions/55627828/how-to-get-a-defaulttablemodel-objects-data-into-a-subclass-of-defaulttablemode/55635012#55635012 for a basic example. – camickr Aug 05 '21 at 13:49
  • 1
    **Tips for future questions:** 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Most IDEs have a keyboard shortcut specifically for formatting code. – Andrew Thompson Aug 06 '21 at 00:56

0 Answers0