• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Update JTable cell values inside a loop  RSS feed

 
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi my Java Experts,
I have a program where i want to indicate the user when i have completed a task which i am running inside a for loop. Below is my code.


I have used netbeans to build my app. So there creating jTable is out of my control as that part was inside auto-generated code. I have just used the jTable.setValue().

My problem is, above method is inside a button click event. Updated values not displaying until the loops ends Please help me... I have less knowledge in java swing area.
Thanks in Advance- Jude
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bad idea to use auto-generated code because it tends to generate unnecessary code.

Back to the problem. Ideally for a button click (some kind of action), it is possible to get the row/column of the JTable without looping the entire set of table data.

How to do this? Using a TableModel.

Have a look at the Swing tutorial for a start, JTable in particular for your case.
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear K. Tsang,
i appreciate your reply to my post. I looked into that doc, according to that, i have to create JTable manually. But in my case, i got them from netbeans auto-generated codes. I am simply asking, how to modify my code to update the data in the jTable at the runtime. Highly appreciate if you can demonstrate a simple code block.

Thanks, Jude
 
Rancher
Posts: 3042
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Updated values not displaying until the loops ends



Your code is probably being executed from within an ActionListener. This code executes on the Event Dispatch Thread (EDT) which is responsible for handling events and undating the GUI. Therefore the GUI is not updated until the loop is finished executing. So the solution is to use a separate Thread for the long running task so you don't block the EDT.

Read the section from the Swing tutorial on Concurrency for more information. In particular, you will probably want to use a "SwingWorker" which executes on a separate Thread and then you can "publish" results to be dynamically updated in your table as required.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!