This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting Record Into JTable For An Specified Interval

 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How to insert a new row into JTable at every interval of 5 minutes?
Also how to fix the maximum number of rows in the table? Let it be 50 as maximum number of rows are allowed. At the time of 51st row insertion, it should look for the row which was inserted earliest.

Scenario :

There is a table, "mytable" in database, with columns {createdTime, name, address}.
Now i want to display these data on swing UI based on JTable, which gets refreshed at interval of 5 minutes, i.e if there is insertion of any new in "mytable" during the 5 minute interval, then it should insert the same data into JTable.

Thanks.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to insert a new row into JTable at every interval of 5 minutes?

Check out javax.swing.Timer For more complicated scheduling scenarios google for "Quartz"

Also how to fix the maximum number of rows in the table? Let it be 50 as maximum number of rows are allowed. At the time of 51st row insertion, it should look for the row which was inserted earliest.

Since your data is coming from the DB, the easiest approach would be modify your DB query such that it picks out the latest 50 records based on insertion time or whatever your criteria is.
 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maneesh,

I tried to implement the JTable. Created my own model by extending "AbstractDataModel" and defined essential method like getValueAt etc. Some methods i want to show :



and to populate data i used this :


And in main method calling in this way :


Now just only one data is being shown in JTable. Where am i doing mistake? Please point it out.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tanzy akhtar wrote:Hi Maneesh,

I tried to implement the JTable. Created my own model by extending "AbstractDataModel" and defined essential method like getValueAt etc.


I've not heard of "AbstractDataModel" but perhaps you're referring to "AbstractTableModel". If the latter, please note that many of us have had good success using classes that extend DefaultTableModel where much of the model's functionality had already been built for you.

 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've not heard of "AbstractDataModel" but perhaps you're referring to "AbstractTableModel".

Thank you for coreting me Pete.

Well, some people say that it would be better, if model class extends AbstractTableModel instead of DefaultTableModel.
Let me tell you my need. I need to fire a query, every ten minutes, to database for table "T",having column of type Date,Integer and String.
Now need to show this data on UI using JTable up to maximum 10 rows.
Now let me know your advice, whether it will good to follow DefaultTableModel or need to extend AbstractTableModel?

Thanks,
Tanzy.
 
mark goking
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi tanzy, you do not need to insert the same data in your jtable object. you only need to insert the data into your table model object

as someone mentioned awhile ago, you can use a Timer for that. or, create your own thread and have your jtable object reload its table model contents every 5 minutes
 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mark, Pete and Maneesh for helping to resolve the problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic