自定义ResultSetTableModel不在JXTable中显示结果

时间:2017-03-15 11:59:31

标签: java swing jtable

我修改了this code,希望它可以做我想要的。但是它并没有取代JXTable中的结果。

MY POJO

public class Product {
private String stockCode;
private String name;
private String description;
private String category;
private String type;
private Double price;
private Integer quantity;
//getters and setters -auto generated.
}

这是我的自定义模型

public class ResultSetTableModelProduct extends AbstractTableModel {

private ResultSet rs;
private ResultSetMetaData rsmd;
private List<String> title;
private List<Product> datas;
private int colcount = 0;

public ResultSetTableModelProduct(ResultSet rs) {
    try {
        this.rs = rs;
        this.rsmd = rs.getMetaData();
        this.colcount = this.rsmd.getColumnCount();
        title = new ArrayList<>();
        for (int j = 1; j <= this.colcount; j++) {
            title.add(this.rsmd.getColumnName(j));
        }
        title.set(0, "Stock Code");
        title.set(1, "Name");
        title.set(2, "Desc");
        title.set(3, "type");
        title.set(4, "cat");
        title.set(5, "Price");
        title.set(6, "Quantity");

        datas = new Vector<Product>();
        while (rs.next()) {
            Product pro = new Product();

            pro.setStockCode(rs.getString("stock_code"));
            pro.setName(rs.getString("name"));
            pro.setDescription(rs.getString("description"));
            pro.setType(rs.getString("type_code"));
            pro.setCategory(rs.getString("cat_code"));
            pro.setPrice(rs.getDouble("price"));
            pro.setQuantity(rs.getInt("quantity"));
            System.out.println("Product name:" + pro.getName());//this is printing fine
            datas.add(pro);
            System.out.println("Is there Data in datas? " + datas);//Yes, printing too

        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public String getColumnName(int column) {
    // TODO Auto-generated method stub
    return title.get(column);

}

@Override
public int getColumnCount() {
    // TODO Auto-generated method stub
    return title.size();
}

@Override
public int getRowCount() {
    // TODO Auto-generated method stub
    return datas.size();
}

@Override
public Object getValueAt(int row, int column) {
    // TODO Auto-generated method stub
    Product p = new Product();
    switch (column) {
        case 0:
            return p.getStockCode();
        case 1:
            return p.getName();
        case 2:
            return p.getDescription();
        case 3:
            return p.getType();
        case 4:
            return p.getCategory();
        case 5:
            return p.getPrice();
        case 6:
            return p.getQuantity();
        default:
            throw new ArrayIndexOutOfBoundsException(column);
    }
}

@Override
public void setValueAt(Object aValue, int row, int col) {

    if (col < 0 || col >= this.getColumnCount()) {
        throw new ArrayIndexOutOfBoundsException(col);
    } else {
        Product p = new Product();
        switch (col) {
            case 0:
                p.setStockCode(aValue.toString());
                break;
            case 1:
                p.setName(aValue.toString());
                break;
            case 2:
                p.setDescription(aValue.toString());
                break;
            case 3:
                p.setType(aValue.toString());
                break;
            case 4:
                p.setCategory(aValue.toString());
                break;
            case 5:
                p.setPrice((Double) aValue);
                break;
            case 6:
                p.setQuantity((Integer) aValue);
                break;
            default:
                throw new ArrayIndexOutOfBoundsException(col);
        }
    }
}

@Override
public Class<?> getColumnClass(int columnIndex) {
    switch (columnIndex) {
        case 0:
            return String.class;//stcode
        case 1:
            return String.class;//name
        case 2:
            return String.class;//desc
        case 3:
            return String.class;//typecode
        case 4:
            return String.class;//cat
        case 5:
            return Double.class;//price
        case 6:
            return Integer.class;//quantity
        default:
            throw new ArrayIndexOutOfBoundsException(columnIndex);
    }
}

public void addRow(Product product) {
    this.datas.add(product);
    this.fireTableRowsInserted(datas.size() - 1, datas.size() - 1);
    //also add into
}

public void remove(String id) {
    //remove both from the model and DB
}

}

以下是我初始化表格和GUI的方法

//set your table here
    connection=conn.getConnection();
    stmnt=connection.createStatement();
    ResultSet res=stmnt.executeQuery(sql);
    rsp= new ResultSetTableModelProduct(res);
    jXTable1_products.setModel(rsp);

因此,当我运行我的应用程序时,它运行正常,但只有表中未显示的条目/数据。 我缺少什么? 还可以在评论中找到额外的信息。

0 个答案:

没有答案