• Post Reply Bookmark Topic Watch Topic
  • New Topic

Update JTable cell values inside a loop  RSS feed

 
Jude Niroshan
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
 
K. Tsang
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
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!