仅在行上创建的JFrame中的DefaultTableModel-JAVA [英] DefaultTableModel in a JFrame only creating on Row - JAVA

查看:118
本文介绍了仅在行上创建的JFrame中的DefaultTableModel-JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从数据库中获取许多行以填充JFrame中的JTable,但它仅获取第一行/条目(始终为行1).它是房屋列表和存储在表格中的详细信息

Im trying to get a number of rows from a database to populate a JTable in a JFrame but its only getting the first row/entry (row 1 always). Its a list of houses + details stored in a table

 public static DefaultTableModel buildTableModel(ResultSet rs)
            throws SQLException {

        ResultSetMetaData metaData = (ResultSetMetaData) rs.getMetaData();

        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount-1; column++) {
            columnNames.add(metaData.getColumnName(column));
        }

        // data of the table
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }
        return new DefaultTableModel(data, columnNames);
    }

如何打印表中的所有行而不是仅打印第一行?

How do I print out at all the rows in the table rather than just the first?

public void displaySaleProperties() throws SQLException{

    Connection conn = null;
    try {       
        conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
        System.out.println("Connected to database.");       

         // The Connection is obtained

        Statement Stmt = (Statement) conn.createStatement();
    //  Stmt.execute(createPropertyTable);

        ResultSet rs = Stmt.executeQuery("select * from PropertySale");

        // It creates and displays the table
        JTable table = new JTable(buildTableModel(rs));
        table.setPreferredSize(new Dimension(1150,17));
    //    JOptionPane.showMessageDialog(null, new JScrollPane(table));

        final JPanel panelOne = new JPanel();
        panelOne.setVisible(true);
        panelOne.setBackground(Color.LIGHT_GRAY);
        // JFRAME
        final JFrame topFrame = new JFrame();
        topFrame.setSize(1200, 300);
        topFrame.setLocationRelativeTo ( null );
        topFrame.setVisible(true);
        topFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // PUT TOGETHER
        topFrame.add(panelOne);

        panelOne.add(table);
        panelOne.revalidate();
        panelOne.repaint();

        // Closes the Connection

    } catch (SQLException e) {
        System.err.println("Cannot connect to database." + e);
    } finally {
    if(conn != null){
        conn.close();   
      }
    }   
}

推荐答案

使用以下方法删除行:

table.setPreferredSize(new Dimension(1150,17));

以另一种方式,您可能要使用滚动窗格:

In another way, may you want to use a scroll pane:

JTable table = new JTable(buildTableModel(rs));
JScrollPane scrollPane = new JScrollPane(table);
topFrame.add(scrollPane);

这篇关于仅在行上创建的JFrame中的DefaultTableModel-JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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