JTable仅显示1行 [英] JTable only displaying 1 row

查看:96
本文介绍了JTable仅显示1行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在JTable中显示结果集的一部分(两列),我只能看到一行. JTable在现有的GUI中,结果集传递到GUI类中的方法. 当我执行Vector size()时,它将返回一个. 这告诉我列表中只有一个条目.我应该在4个条目中看到的条目.我看到的条目是我期望的最后一个条目,是4个条目.

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); 


        }

推荐答案

这是告诉我列表中只有一个条目.我应该看到4个条目.我看到的条目是我期望的最后一个条目,应该是4."

"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."

您将在每次迭代中创建一个新的DefaultTableModel.不要那样做.

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

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

将以上内容带出.而是先设置模型.然后只需在循环中.addRow(row)

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);
}

DefaultTableModel(columnNames, 0)的构造函数,其中0是初始行数.因此,您要做的就是在while(rs.next())中动态地逐行添加行.对于每行数据,您只需输入datamodal.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)

更新

就像我说的那样,我不知道您在做什么错,但是我的代码没有错.这是一个测试程序

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();
        } 

    }
}

这篇关于JTable仅显示1行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆