如何将从MongoDB检索到的整数值打印到jtable中



我正在尝试将整数值(存储在MongoDB中(打印到jtable中。这是MongoDB条目之一:

Inserted Document: 108 
{
  "_id": {
    "$oid": "5912effdf9c13f1ab9377eb6"
  },
  "SrNo": "53",
  "Brand": "Vivo",
  "Product Name": "Y51L",
  "Price": 10000,
  "Brand Reputation": 7,
  "Features": 10,
  "Rating": 4
}

我可以打印字符串值,如产品名称,品牌和srno.但我无法打印整数值,如评级,功能等。

这是代码:

    JTable table = new JTable();                                                                             
    String[] columns = new String[]{"SrNo","Brand","Product Name","Price"};
    DefaultTableModel model = new DefaultTableModel(columns,0);
    JScrollPane scrollPane = new JScrollPane(table);
    frame2.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame2.setBounds(100, 100, 677, 392);                                                               
    frame2.setVisible(true);
    frame2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

    DBCursor cursor = coll.find();
                 //int i=1;
                 while (cursor.hasNext())
                 {
                     DBObject obj = cursor.next();
                     String SrNo = (String)obj.get("SrNo");
                     //ObjectId id = (ObjectId)obj.get("_id");
                     String Brand = (String)obj.get("Brand");
                     String ProductName = (String)obj.get("Product Name");
                    //String price = (String)obj.get("Price");
                     //String price = Integer.toString((String)obj.get("Price"));
                     //int BrandReputation = Integer.parseInt((String)obj.get("Brand Reputation"));
                     //String brandrep = String.valueOf(BrandReputation);
                     //int Features = Integer.parseInt((String)obj.get("Features"));
                     //int Ratings = Integer.parseInt((String)obj.get("Rating"));
                     model.addRow(new Object[]{SrNo,Brand,ProductName});

                 }
                 table.setModel(model);
                 cursor.close();
                 mongoClient.close();

在 while 循环中,您可以看到注释的代码行,这些都是我尝试打印整数值但都失败的方法。有没有其他方法可以将整数值打印到 jtable 中?

由于数据被添加到对象数组中,因此 j\Just 将所有内容视为对象:

Object serialNumber = obj.get("SrNo");
Object brand = obj.get("Brand");
Object productName = obj.get("Product Name");
Object price = obj.get("Price");
model.addRow( new Object[] {serialNumber, brand, productName, price} );

现在的诀窍是告诉 TableModel 每列的数据类型是什么,因此您需要重写 TableModel 的 getColumnClass(...) 方法。

一些通用代码将是:

DefaultTableModel model = new DefaultTableModel(columns, 0)
{
    @Override
    public Class getColumnClass(int column)
    {
        for (int row = 0; row < getRowCount(); row++)
        {
            Object o = getValueAt(row, column);
            if (o != null)
            {
                return o.getClass();
            }
        }
        return Object.class;
    }
};

或者你可以更具体:

DefaultTableModel model = new DefaultTableModel(columns, 0)
{
    @Override
    public Class getColumnClass(int column)
    {
        switch (column)
        {
            case 3: return Double.class;
            default: return String.class;
        }
    }
};

最新更新