如何将从 mongodb 检索到的整数值打印到 jtable 中?
how to print integer value retrieved from mongodb into 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.but我无法打印评级、功能等整数值
代码如下:
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 循环中,您可以看到注释的代码行,这些都是我尝试打印整数值的所有方式,但都是 failed.Is 还有其他方法可以将整数值打印到 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;
}
}
};
我正在尝试将整数值(存储在 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.but我无法打印评级、功能等整数值
代码如下:
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 循环中,您可以看到注释的代码行,这些都是我尝试打印整数值的所有方式,但都是 failed.Is 还有其他方法可以将整数值打印到 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;
}
}
};