Upendra Siripurapu
Upendra Siripurapu

Reputation: 316

Moving JTable rows downwards when new row is added to the table

I have to maintain list of events generated by the user in a JTable.The JTable consists of 2 columns Time and event and 5 rows. When 1st event is generated it's data must be placed in 1st row of JTable, when 2nd event is generated the data in 1st row must be moved to 2nd row and the data of 2nd event must be place in the 1st row of the JTabel and so on up to 5th row. There is no limit to the events(n events can be generated). But i have to display only 5 events in JTable and older once will be stored in text file. I have tried some logics but they are failed to work.

This is my problem. Can any one give me an idea how to move rows downwards...

Regards... Upendra.S

Upvotes: 2

Views: 396

Answers (2)

Robin
Robin

Reputation: 36601

You simply add your new row at index 0 in your TableModel. If your TableModel extends from DefaultTableModel, you can even use the available insertRow method.

For the requirement to only show 5 rows, you simple remove the extra row from your TableModel.

Consult the JTable tutorial for more information

Upvotes: 4

mKorbel
mKorbel

Reputation: 109815

Upvotes: 3

Related Questions