简体   繁体   中英

JTable only displaying 1 row

I am trying to display part of a resultset in a JTable (Two columns), I can only manage to see one row. The JTable is in an existing GUI and the resultset is passed to a method in the GUI class. When I do a Vector size() it is returning one. This is telling me there is only one entry in the list. I should be seeing at 4 entries The entry I am seeing is the last one of what I expect would be 4.

Please see the method below: Any help would be much appreciated.

public static void getResultSet(ResultSet resultSet)  throws SQLException { 

        ResultSetMetaData metaData = resultSet.getMetaData();

                    //JTable name is resultsTable includes Scrollpane


         // names of columns

        Vector<String> columnNames = new Vector<String>();
        int columnCount = 2;
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
            System.out.println("ColumnNames "+columnNames );
        }

     // data from the table

      //  Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        while (resultSet.next()) {
            Vector<String> vector = new Vector<String>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(resultSet.getString(columnIndex)); 
            }

            data.add(vector);
            System.out.println("Vector Value = "+ data);
            System.out.println("Vector Size =" + data.size()); //Returning 1 - Should see 4 entries

            DefaultTableModel datamodel = new DefaultTableModel(data,columnNames);
            resultsTable.setModel(datamodel); 


        }

"This is telling me there is only one entry in the list. I should be seeing at 4 entries The entry I am seeing is the last one of what I expect would be 4."

You're creating a new DefaultTableModel each iteration. Don't do that.

DefaultTableModel datamodel = new DefaultTableModel(data,columnNames);
resultsTable.setModel(datamodel);

Take the above out. Instead, set the model first. Then just .addRow(row) in the loop

for (int column = 1; column <= columnCount; column++) {
     columnNames.add(metaData.getColumnName(column));
     System.out.println("ColumnNames "+columnNames );
}
DefaultTableModel datamodel = new DefaultTableModel(columnNames, 0);
table.setModel(datamodel);

while (resultSet.next()) {
    Vector<String> vector = new Vector<String>();
    for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
        vector.add(resultSet.getString(columnIndex)); 
    }
    datamodel.addRow(vector);
}

The constructor for the DefaultTableModel(columnNames, 0) where 0 is the number of initial rows. So all you need to do is add rows dynamically one by one in the while(rs.next()) . For each row of data you get in a Vector just datamodal.addRow(vector)


UPDATE

Like I said, I don't know what you're doing wrong, but there's nothing wrong with my code. Here's a test program

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TestTableModel {

    private ResultSet resultSet = null;

    public TestTableModel() throws SQLException {
        initDB();

        JTable table = new JTable(getModel(resultSet));
        JScrollPane scroll = new JScrollPane(table);

        JFrame frame = new JFrame("Test Table Model");
        frame.add(scroll);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    TestTableModel testTableModel = new TestTableModel();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    private DefaultTableModel getModel(ResultSet resultSet) throws SQLException {
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        Vector<String> columnNames = new Vector<>();

        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
            System.out.println("ColumnNames " + columnNames);
        }
        DefaultTableModel dataModel = new DefaultTableModel(columnNames, 0);

        while (resultSet.next()) {
            Vector<String> vector = new Vector<>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(resultSet.getString(columnIndex));
            }
            dataModel.addRow(vector);
        }


        return dataModel;
    }

    private void initDB() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost/...", "...", "...");
            ps = conn.prepareStatement("SELECT * FROM city");
            resultSet = ps.executeQuery();

        } catch (ClassNotFoundException | SQLException ex) {
            ex.printStackTrace();
        } 

    }
}

在此处输入图片说明

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