【问题标题】:DefaultTableModel in a JFrame only creating on Row - JAVAJFrame 中的 DefaultTableModel 仅在 Row 上创建 - JAVA
【发布时间】:2013-10-15 03:36:03
【问题描述】:

我试图从数据库中获取一些行来填充 JFrame 中的 JTable,但它只获取第一行/条目(始终为第 1 行)。它是存储在表格中的房屋列表 + 详细信息

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

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

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

【问题讨论】:

    标签: java sql swing jdbc jtable


    【解决方案1】:

    删除行:

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

    换一种方式,你可能想使用滚动窗格:

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

    【讨论】:

      猜你喜欢
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 2014-10-13
      • 2016-09-23
      相关资源
      最近更新 更多