如何打印从mongodb检索到的整数值到jtable?

时间:2017-05-10 11:04:47

标签: java mongodb swing jframe 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
}

我可以打印像productname,brand和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中?

1 个答案:

答案 0 :(得分:1)

由于数据被添加到对象数组中,您只需将所有内容视为对象:

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