wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Deleting a row in Jtable as well as from the .txt database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Deleting a row in Jtable as well as from the .txt database" Watch "Deleting a row in Jtable as well as from the .txt database" New topic
Author

Deleting a row in Jtable as well as from the .txt database

Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
hi,

second time i am posting this because i did not get a hint of using the fileChannel class.

my problem is like this :-

I am able to delete a selected row from the JTable , but I am unable to delete that particular row from my database(.txt base). As of that ,after the re-run of the program getting the same datas in JTable.

Please help..
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
When you delete a row from the table, you need to recreate the entire text file based on the data that is still in the TableModel. There is no need to use a FileChannel for this a regular FileWriter will do.
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
thankyou rob,




now how to move ahead with this , i mean if I'm using FileWriter class then the same data will be deleted from the .txt databse too ? Dont I need to compare the selected set of datas and compare it to the data in the .txt database ? or simply it will delete the same data from the database ?

please help.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
You can't just delete a single line from a text file. You need to delete the file and recreate it.
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
Thankyou Rob,

I will follow that . and inform the same to my Lecturer.

Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Why didn't you continue in your other thread? Let's continue in this new one though.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
Dear Rob , and all the acive members of javaranch

Sorry to post late ,,

i am having difficulty in these areas :-

1) After deleting selected rows , how to save the remaining data of the JTable ? eg . is it in array or ???
2) After saving that data , deletion of the .txt file is done (which i can do) , and with the help of PrintWriter , how to write the data back in that .txt file ??
3) If I am creating a new file automatically with the same name , but the datas will not be accurately written i guess , as this is my structure of the rows of the database :-
eg ,,, Diego Maradona:Argentina:Football,
( ":" is the delimeter , and "," is the end of first row ) and with the help of Scanner I have displayed this in JTable , so its totally a mismatch...

My whole idea is not clear at this point ,,,I am just confused .

Can you help me .

As you can see my profile I have started with Writing a file and this all due to the kind support of javaranch members , so I am able to understand Java a better then before. hope to have this support in future .

Regards ,
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Thamu Gurung wrote:1) After deleting selected rows , how to save the remaining data of the JTable ? eg . is it in array or ???

Check out JTable.getModel(), and its methods: getRowCount(), getColumnCount(), getValueAt(int, int). Combine these and you can treat the model as if it were a two-dimensional array.

2) After saving that data , deletion of the .txt file is done (which i can do) , and with the help of PrintWriter , how to write the data back in that .txt file ??

You already know about PrintWriter (and probably also about FileWriter). You just iterate over the model and write each row, column by column.

3) If I am creating a new file automatically with the same name , but the datas will not be accurately written i guess , as this is my structure of the rows of the database :-
eg ,,, Diego Maradona:Argentina:Football,
( ":" is the delimeter , and "," is the end of first row ) and with the help of Scanner I have displayed this in JTable , so its totally a mismatch...

Well, you output everything. So why don't you tell us how you output it all?
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
Dear Rob,

here is the code , which i used to display the data

Notes :-
- I really could not use all those methods of JTable eg,,getRowCount(), can you give me a hint please?? After that only I will be able to move ahead .

Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

That's how you read it, not how you write the data again. But apart from not removing the trailing , the reading seems to be just fine.

TableModel.getRowCount() returns the number of rows, TableModel.getColumnCount() returns the number of columns, and TableModel.getValueAt(int, int) returns the value for one specific cell. Surely you can now use that to loop through the TableModel, row by row, column by column? It's just two nested for-loops.
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
Dear Rob,

Please dont mind , would you please tell me why we should use TableModel.getValueAt(int,int) , in this case ??? can you help with some starting codes .

and still I have not thought of writing, thinking of making use of PrintWriter.

Regards
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
hi,

I tried your suggetion but could not get the results

my codes are like this

Notes :-
- I tried with Printwriter but too much errors
- want to delete the fie and then to store the remaining data of JTable , and then to recreate the file with the same name .

Hoping to have help

Regards,
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you need to brush up on simple basics

int row=model.getRowCount();

returns a 1-based count of the number of rows, so if there's 15 rows, row will be 15
now you try to use it

Object data = model.getValueAt(row,........

where the rows are 0-based, or 0-14, so what do you expect to happen, when row is 15

now, even if you fix that, inside the nested for()'s you have
out.write(data+":");
but 'data' never changes

keep trying, just remember that if your program reads in the data correctly from the text file,
you have to save it in the same format, so it can be read the next time
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Some improvements to your code:

1) the assignment to data must occur within the most inner loop, using i and j as parameters to getValueAt
2) move the creation of the writer outside the loop. You are now opening a new connection to the file for each single cell. And you don't even close that connection. Try this:
You should actually put the closing in a finally block but you can find enough examples on how to do that.
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
Dear Rob,

Thankyou very much for your kind help. Now this is working .

This forum is really really very helpful.

Thanks a lot.

Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deleting a row in Jtable as well as from the .txt database