简体   繁体   中英

tableModel.addRow() not showing anything in JTable but row object has values

I am using JTable in swing to display a MySQL table, although the table values are present in the row object (as in the output in screenshot) but jtable in the application is empty. The output in the screenshot shoes the Object[] array but when I pass this in the addRow method, it doesn't show anything on the JTable GUI.

private void tableSubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  

    //fetch the name of the database to be created
    String dbName = tableDBName.getText();
    //fetch the root password 
    String password = new String(tableDBPass.getPassword());
    // fetch table name
    String tableName = tableTableName.getText();        

    DefaultTableModel tableModel = (DefaultTableModel)tableViewTable.getModel();
    int rowCount = tableModel.getRowCount();
    for(int i = 0; i<rowCount; i++){
        tableModel.removeRow(0);
    }

    try{
        // create connection
        CreateConnection newCon = new CreateConnection();
        Statement newStat = newCon.initiate(dbName, password);

        // result set contains the data fetched from the MySQL table
        ResultSet rs = newStat.executeQuery("select * from "+tableName);

        // get column names and column count
        ResultSetMetaData metaData = rs.getMetaData();
        int n_columns = metaData.getColumnCount();

        ArrayList<String> columnNames = new ArrayList<>();


        for(int i = 1; i<=n_columns; i++){
            columnNames.add(metaData.getColumnName(i));
        }


        while(rs.next()){
            ArrayList row = new ArrayList(n_columns);

            for(int i = 1; i<=n_columns; i++){
                row.add(rs.getObject(i));
            }
            Object[] obj = row.toArray();
            tableModel.addRow(obj);
            System.out.println("table updated..: "+Arrays.toString(obj));
        }
    }catch(SQLException e){
        System.out.println(e.getMessage());
    }

}

屏幕截图

Besides this I deleted all the rows that are initially present when you insert the JTable in the form.

DefaultTableModel tableModel = (DefaultTableModel)tableViewTable.getModel();

So you start with an empty TableModel, which means the model has no columns or rows.

So even though you invoke the addRow() method, your model has no columns so there is nothing to display in the JTable

ArrayList<String> columnNames = new ArrayList<>();

for(int i = 1; i<=n_columns; i++){
     columnNames.add(metaData.getColumnName(i));
 }

The above code does nothing. All it does is add some data to an ArrayList, but that ArrayList is never used.

What you want to do is:

  1. Use a Vector , not an ArrayList
  2. Then you need to create an empty DefaultTableModel with just the columns names. Read the API for the appropriate constructor.

Then in your logic that iterates through the ResultSet you want to:

  1. Again use a Vector for each row of data
  2. add the data to the Vector
  3. invoke the addRow(...) method of the model. The model will also accept a Vector so there is no need to convert it to an array

After all the data is added to the model you then need to:

  1. Use tableModelView.setModel(...) to replace the model in your table.

Now your table model will have both columns and rows of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM