java


JTable repeats one row


I created a JTable and inserted the column header and data into the table.
data is vector of vectors
column_header is vector of string
This is the code:
Vector<Diagnosis_data> Arraylist_object = new Vector<Diagnosis_data>();
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
Diagnosis_data d1 = new Diagnosis_data("92992","1",1422-09-09,1422-09-18,E11.9,"Type 2 diabetes mellitus without complications");
Diagnosis_data d2 = new Diagnosis_data("8199111","2",1415-09-09,1415-09-18,E11.622,"Type 2 diabetes mellitus with other skin ulcer");
Arraylist_object.add(d1);
Arraylist_object.add(d2);
int f = 0;
while(f < Arraylist_object.size())
{
Vector<Object> vector = new Vector<Object>();
for(; f < Arraylist_object.size() ; f++)
{
vector.add(Arraylist_object.get(f).patient_ID);
vector.add(Arraylist_object.get(f).Ad_ID);
vector.add(Arraylist_object.get(f).Ad_start);
vector.add(Arraylist_object.get(f).Ad_End);
vector.add(Arraylist_object.get(f).Diagnosis_code);
vector.add(Arraylist_object.get(f).Diagnosis_Des);
data.add(vector);
}
}
DefaultTableModel tableModel = new DefaultTableModel(data,column_header);
JTable table_2 = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table_2);
scrollPane.setBounds(1, 1, 1050, 500);
panel_1.add(scrollPane);
The result is:
The code you posted is working given the correct data - the problem must be the data. Use a debugger to check it or, just for testing, print out the content of data.
Probably the same Vector is being added to data multiple times or the content of each Vector is same (maybe the Patient object is using static fields?)
Note: data I used to test your code - a new Vector must be created for each row:
Vector<Vector<?>> data = new Vector<>();
Vector<String> column_header = new Vector<>();
column_header.addAll(Arrays.asList("A", "B", "C"));
Vector<String> row;
row = new Vector<>();
row.addAll(Arrays.asList("11", "12", "13"));
data.add(row);
row = new Vector<>();
row.addAll(Arrays.asList("21", "22", "23"));
data.add(row);
row = new Vector<>();
row.addAll(Arrays.asList("31", "32", "33"));
data.add(row);
EDIT: After the generating data code has been added, it is clear that the problem is, as I suspected above: "the same Vector is being added".
Look at this part:
Vector<Object> vector = new Vector<Object>();
for(; f < Arraylist_object.size() ; f++)
{
vector.add(...);
data.add(vector);
}
The vector is created only once OUTSIDE the loop, but being added multiple time inside the loop.
Solution: create the vector INSIDE the loop
And there is no need for the additional while, actually the convention is to declare the variable in the for loop (and use i as counter):
for(int i = 0; i < Arraylist_object.size() ; i++)
{
Vector<Object> vector = new Vector<Object>();
vector.add(Arraylist_object.get(i).patient_ID);
...
data.add(vector);
}
or use foreach:
for (Diagnosis_data diagnosis : Arraylist_object)
{
Vector<Object> vector = ...
vector.add(diagnosis.patient_ID);
vector.add(diagnosis.Ad_ID);
...
}
 
Advice: spend some time learning to use a debugger, it will not be wasted if you want to do serious programming.
Advice2: as commented by Andrew Thompson, use LayoutManager's instead of using constant coordinates to layout your components. Tutorial: Laying Out Components Within a Container

Related Links

Crop from binary image (boolean array) in Java
Loop through a 2d array backwards
Cannot build graph with Dagger 2
Increase step size on JSpinner when a certain key is pressed
Why this raises compilation error when trying to increment the value and put it back to HashMap?
Why doesn't this code throw a ConcurrentModificationException?
Double Buffering with Java Swing leaving a trail
Why won't this application terminate with DISPOSE_ON_CLOSE?
access case object (scala) from java [duplicate]
Not able to See Encyrpted Sent Mail via Java Program through Domino Sever
Websockets- send binary data as chunks
Saving special characters in Enum data type
Java properties : � displayed instead of ä
is hadoop DistributedFileSystem thread safe?
Math.pow and Method Calling
How to find the longest common substring

Categories

HOME
testing
clips
include
setup-deployment
spring-jdbc
windows-server
ravendb
opengl-es-2.0
pheatmap
add
facebook-messenger-bot
here-api
postgres-xl
collectd
caml
foselasticabundle
footer
contextmenu
dbext
swingx
nas
conemu
kryo
tapestry
kvc
php-openssl
trading
crosstab
django-storage
social-media
log4js-node
lightswitch-2013
pim
hybridauth
asset-pipeline
ghost4j
wixsharp
git-merge
swift3.0.2
dynamics-crm-2013
twitch
theano.scan
webix-treetable
unoconv
ensembles
nomethoderror
spring-security-kerberos
no-www
modelmapper
qcombobox
apple-news
boost-multi-index
togetherjs
nstextview
recursive-datastructures
nativeapplication
sqlclient
zendesk-app
account-kit
adxstudio-portals
contact-list
bgp
php-internals
photobucket
hsv
python-3.2
purge
angular-leaflet-directive
splash
mono-embedding
skos
inmobi
geonetwork
sorl-thumbnail
tt-news
fluentautomation
java.nio.file
centos5
openlaszlo
apc
pygit2
cisco-jtapi
transactionscope
qt-jambi
external-accessory
asp.net-mvc-areas
gallio
jquery-ui-droppable
kdbg
backcolor
rtml
document-conversion

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile