0

I have this code that should create my JTable column hearders based on the field names returned from my query. For some reason my code is not working, I am very new to Java and I have been doing research but cant seem to find where my issue is. Any help will be greatly appreciated.

public void initTable(){    
    try {
        DefaultTableModel tblModel = new DefaultTableModel() 
        {
           @Override
           public boolean isCellEditable(int row, int column) 
           {
              return false;
           }
        };

        tblMain.setModel(tblModel);
        tblMain.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //tblMain.getTableHeader().setReorderingAllowed(false);

        Connection dbconn = dbConn();
        Statement stmt = dbconn.createStatement();

        String qry = "SELECT * FROM Services";
        ResultSet rs = stmt.executeQuery(qry);          

        int numCols = rs.getMetaData().getColumnCount();
        System.out.println("Num Cols: " + numCols);

        for (int col = 1; col <= numCols; col++){
            tblModel.addColumn(rs.getMetaData().getColumnLabel(col));
            //tblModel.addColumn("Tmp");
            System.out.println(col + " - " + rs.getMetaData().getColumnLabel(col));
        }

        int row = 0;
        while (rs != null && rs.next()){
            tblModel.addRow(new Object[0]);
            tblModel.setValueAt(rs.getString("ServiceID"), row, 0);
            tblModel.setValueAt(rs.getString("Institution"), row, 1);
            tblModel.setValueAt(rs.getString("Doctor"), row, 2);
            tblModel.setValueAt(rs.getString("Street"), row, 3);
            tblModel.setValueAt(rs.getString("City"), row, 4);
            tblModel.setValueAt(rs.getString("State"), row, 5);
            tblModel.setValueAt(rs.getString("ZipCode"), row, 6);
            tblModel.setValueAt(rs.getDate("Date"), row, 7);
            tblModel.setValueAt(rs.getDouble("Cost"), row, 8);
            tblModel.setValueAt(rs.getInt("ServiceTypeID"), row, 9);
            tblModel.setValueAt(rs.getString("Comments"), row, 10);
            row++;
        }   
        rs.close();         

    }
    catch (Exception e){
        e.printStackTrace();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Maira
  • 27
  • 1
  • 5
  • 1
    See this question: [JTable won't show column headers](http://stackoverflow.com/questions/2320812/jtable-wont-show-column-headers?rq=1) – Thomas Fritsch Apr 14 '17 at 16:31
  • 1
    Post a proper [mcve] that demonstrates the problem. Until a problem is solved you don't know what code is causing the problem. The above suggestion is guessing that you are not use a JScrollPane to display the JTable and therefore the problem is completely unrelated to the code you just posted. If this suggestion doesn't help then then `MCVE` you post should be with hard coded data. That is we don't have access to your data base so you can't use the database to demonstrate the problem. – camickr Apr 14 '17 at 17:27

1 Answers1

0

Not really related to your problem but I would like to suggest a better way to add the data to the model.

Instead of creating an empty array and then using setValueAt(...) you can change the looping code to look something like:

while (rs != null && rs.next())
{
    Vector<Object> row = new Vector<Object>(11);
    tblModel.addRow(new Object[0]);
    row.add(rs.getString("ServiceID"));
    row.add(rs.getString("Institution"));
    ...
    row.add(rs.getString("Comments"));
    tblModel.addRow( row );
} 

Reasons for this approach:

  1. The DefaultTableModel stores its data in a Vector. So the data from the Array is copied to the Vector. Save resources by just using an Vector from the beginning.

  2. The code is shorter to type. This also makes it easier to rearrange columns since you don't need to worry about indexes.

  3. Every time you invoke the setValueAt(...) method an event is generated to tell the table to paint itself. So this is happening 12 times, once for the addrow() method and once for each setValueAt() method. By just using the addRow() method at the end you only generate one event.

camickr
  • 321,443
  • 19
  • 166
  • 288