Post Script, it seems to me this would be a simple matter of inverting the saveAction but for the life of me I don't see how to do it.

Honestly, at this point I am willing to use a different class just to get it to read out.

06-04-2012, 04:47 AM

leszer

Help importing (or reading) from text file to jtable

I'm trying to get a code that saves its entries to a text file from a jtable to read that text file when the application is run, so it can basically append new entries.

But I'm pretty new to it all and I have failed every attempt. I've been googling my but off for four hours with zero success. I've found ways that use vector to build new tables for read the table but I would really like it to go into the one I already have created.

Unless I have missed something this has nothing to do with applets (things that typically live on web pages, and manipulate their resources with the help of a web browser). I'll move to the Swing forum.

Again, I may have missed it, but what, exactly, is the problem: ie how does the behaviour of what you have posted differ, when run, from the expected or intended behaviour?

-----

Just a guess - and others more knowledgeable about such things can/will jump in to correct me - but I would have considered writing a subclass of TableModel (maybe extending DefaultTableModel) that was able to load and save its data from file. The idea is to stop Contacts (a frame ie some sort of view) from doing everything itself and becoming long and complex in the process. And for that matter I'd make Contracts a panel not a frame - a thing combining a bunch of components and providing behaviour to coordinate them.

06-04-2012, 05:31 AM

pbrockway2

Re: Help importing (or reading) from text file to jtable

Quote:

it seems to me this would be a simple matter of inverting the saveAction

Try that.

The load action would have to read the file. From the first line it would obtain a list (Vector instance) of column names. Then, line by line, it would build up a list of lists containing the data. Finally it would use these lists (of column names and data) to create a new table model and set the table's model to this new model. Contact would have to "remember" the table whose model it is going to alter: ie contactTable would have to be an instance variable not a variable local to the constructor.