• Post Reply Bookmark Topic Watch Topic
  • New Topic

Delete selected row from a JTable as well as the source file

 
Burak Can
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. I'm pretty new to Java and I need your help. I'm using a JTable to display data from a text file. I created an erase button to delete selected rows from the table. I managed to delete rows from JTable but since it doesn't delete the actual data row from the text file, whenever I update the table, all the data comes back. I wanna delete the selected row from the table as well as from the text file. Since I'm new to Java, any kind of help will be appreciated. Thank you in advance.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no easy way to delete a row from your text file. The easiest approach is to just recreate the text field using the data from the TableModel.
 
Burak Can
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain how to recreate a file from the data that is on the table? I already manage to delete the selected row in the table so if I can manage to write all the data that remains on the table to a new file with the same name and location, it would do what I want. Can you help me do this? Thank you for your reply.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Burak Can wrote:Can you please explain how to recreate a file from the data that is on the table?


First, you should learn how to write data to a file. I would point you out to this tutorial for that.
Once you understand that part, it should be quite easy to write the table contents - you have to loop through the rows in the model to read the data.

And, Welcome to the Ranch!
 
Burak Can
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved my problem by using printwriter and filewriter, and recreating the file after deleting the row from the table. Thank you for your kind attention
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!