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

integer value from object in another activity is not being received properly from another class. Not Answered Yet
Hibernate Reverse Engineering Composite Key Creates Extra ID Class
How JVM does method lookup and invocation?
create a router actor parameterizing the creation of each one of its routees in akka java
NPE error trying to create button at runtime in expandablelistview android studio
changes in velocity template to reflect without server restart
Read and Write excel file using Apache POI in java
Nested JSON Object Parsing in android using volley
Java abstract class constructor and new keyword
From Java popup table to Java table within a gui
approximate pi by iteration JAVA
Why start external java process via shell
Extracting sticky notes using PDFBox
websphere classloader gives java.lang.LinkageError when loading slf4j LoggerFactory
Having trouble with reading text files
Trying to understand data validation of set methods in true/false format

Categories

HOME
google-chrome-extension
omnet++
appx
hp-exstream
hashmap
objectgears
amazon-ecs
umd
cross-validation
iggrid
maude-system
row
n-gram
commonmark
event-handling
ibm-odm
caml
openrefine
google-static-maps
evopdf
nhibernate-envers
django-cms
shared-hosting
emulator
visjs
jquery-ajaxq
graphicsmagick
cultureinfo
h2db
apache-commons-io
minitab
strncpy
assistant
catch-all
protovis
sequential
html5-fullscreen
bytecode-manipulation
wixsharp
bitbucket-pipelines
multilingual
abstract-class
service-discovery
tasker
particles.js
avro4s
netcdf4
outlook-2013
hibernate-tools
integrity
forever
skobbler-maps
mathematica-frontend
ubuntu-10.04
testng-dataprovider
pickadate
hexdump
oauth2client
wapiti
cloudbees
photobucket
uid
natvis
reactive-banana
android-listview
ionic
microbenchmark
unity5.2.3
jsapi
dia
jsonpickle
rdtsc
hippomocks
centos5
uv-mapping
plasma
sqlperformance
enterprisedb
cassini-dev
unc
runas
multipage
cascalog
armcc
tridion-worldserver
jspinclude
ticoredatasync
bubble-chart
libc++
external-accessory
joyent
appendto
datareader
yslow
forums
sef
django-notification
document-conversion

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