如何在jtable中显示搜索结果?

时间:2010-11-20 10:08:27

标签: java swing

我正在使用netbeans IDE。我想检查一下canni实际上是如何从使用netbeans绑定映射到表的jtable中搜索的。我想刷新jtable显示符合我的搜索条件的记录

3 个答案:

答案 0 :(得分:0)

DefaultTableModel model = new DefaultTableModel( results from your search );
table.setModel( model );

修改:请参阅Table From Database

答案 1 :(得分:0)

首先,我在Jcombo框中获取字段名称。

stat.q = ["Who is the coolest?", "What is your favorite color?", "Pick a movie", "Where do you want to go?", "test rating"];   

答案 2 :(得分:0)

这就是我做到的。不是专家。

返回结果集的方法:

public ResultSet actualInventoryInCencos(int idCencos) throws SQLException {

    try {
        SQL sql = new SQL();
        PreparedStatement selectPS = sql.createPStatement(cf.SELECT_INVENTORY_BY_CENCOS);
        selectPS.setInt(1, idCencos);
        ResultSet resultSet = selectPS.executeQuery();
        return resultSet;
    } catch (SQLException | NullPointerException e) {
        System.out.println(cf.ERROR_SQL + e);
        cf.e(1);
        return null;
    }

}

TableDAO中接受结果集的方法,并生成并返回包含所有查询数据的DefaultTableModel。

public DefaultTableModel createTable(ResultSet rs) throws SQLException {

    ResultSetMetaData metaData = rs.getMetaData();
    int columnCount = metaData.getColumnCount();

    //ColumnsNames
    Vector<String> columnsNames = new Vector<>();
    columnsNames.add("Column1");
    columnsNames.add("Column2");
    columnsNames.add("Column3");

    Vector<Vector<Object>> tableData = new Vector<>();

    while (rs.next()) {
        Vector<Object> vector = new Vector<>();
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            vector.add(rs.getObject(columnIndex));
        }
        tableData.add(vector);
    }

    return new DefaultTableModel(tableData, columnsNames);
}

将新模型设置为JTable

的行
yourJTable.setModel(tableDAO.createTable(inventory.actualInventoryInCencos(userData.getUserId())));