java


how to print integer value retrieved from mongodb into jtable?


I am trying to print integer values (which are stored in MongoDB)into jtable.
here is one of the MongoDB entry:
Inserted Document: 108
{
"_id": {
"$oid": "5912effdf9c13f1ab9377eb6"
},
"SrNo": "53",
"Brand": "Vivo",
"Product Name": "Y51L",
"Price": 10000,
"Brand Reputation": 7,
"Features": 10,
"Rating": 4
}
I am able to print string values like productname, brand and srno.but I am unable to print integer values like ratings, features etc.
Here is the code:
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();
Inside the while loop you can see the commented lines of codes those are all ways that I tried to print integer values but it all failed.Is there any other way to print integer values into jtable?
Since the data gets added to an Object Array, you j\Just treat everything as an Object:
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} );
Now the trick is to tell the TableModel what the data types are for each column so you need to override the getColumnClass(...) method of the TableModel.
Some generic code would be:
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;
}
};
Or you can be more specific:
DefaultTableModel model = new DefaultTableModel(columns, 0)
{
#Override
public Class getColumnClass(int column)
{
switch (column)
{
case 3: return Double.class;
default: return String.class;
}
}
};

Related Links

Java File Reader not finding appropriate file
Unit testing of encrypt/decrypt
Accessing HDFS from Eclipse outside Hadoop cluster
HibernateException: save is not valid without active transaction
MyBatis association error
Only get android contacts that are saved to the phone or sim
Do annotations on a class still get called when only accessing static members?
How to add 9 patch to this layout to make it like bubble chat?
using same thread variable in different functions
Java and error in Cast programming--AttrCertExample?
Can I duplicate a Stream in Java 8?
Draw image using rectangles
playing music continuously in the loop
StartException in service persistenceunit - possible issue Hibernate queries?
Intellij - RUn vs debug issues
Using List collection makes Hibernate throw stackoverflow error

Categories

HOME
openlayers
variables
asp.net-core
zeromq
tizen
relayjs
electron
c#-2.0
umd
retrofit
fingerprint
azure-storage-tables
facebook-messenger-bot
gz
event-handling
ups
circuit
telephony
lldb
arabic
core-text
devops
rundeck
java-7
tdd
dynamic-featured-image
filezilla
bpel
media-queries
revolution-slider
vxworks
main
sqlite2
fedex
powershell-dsc
arena-simulation
checkboxlist
cubic-spline
fault
python-webbrowser
veracode
integrity
mu
knockout-components
typed-lambda-calculus
webdriverjs
crosswalk-runtime
azure-sdk
android-textview
merge-conflict-resolution
paypal-express
make-install
bgp
asp.net5
pyke
wso2cloud
reactive-banana
t4mvc
metaclass
feedback
php-ci
fluid-dynamics
ami
bridge.net
gwidgets
coldbox
gui-test-framework
code-access-security
contenttype
phpthumb
valueconverter
dealloc
mbr
html5-notifications
tws
easy-install
jelly
android-hardware
frameset
osql
buildr
libc++
data-loss
xmlspy
suppress
xsdobjectgen
routedevent
vc90
compiler-specific
aquaticprime

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App