• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving information form Jtable to text file  RSS feed

 
Jameson Locke
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. I'm new to programming so I apologize if the question may seem stupid, I've searched many websites to help resolve the issue, but couldn't find any answers.

I'm currently making a car transport system and need help storing the information I input to the table to a txt file so the information can remain when ever I close the table.

I didn't do much coding since I don't know exactly what to do. I would appreciate it if you could also explain the process. The coding I did though is down below.

Image of table >  http://imgur.com/WqbhDer

 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Can you really get an Aveo with a 1.6l engine?

Thank you for putting code tags round your code but one of the lines was too long and there were too many empty lines, so I sorted that out for you.
You have come to the right place, and that ain't a stupid question. Can we do that backwards please. Do you know how to write something to a text file?

* * * * * * *

I also suggest you remove all business logic from the display classes. In fact I don't like subclassing GUI classes if at all possible.That is dreadfully abbreviated, but I hope you get the idea from that. I think the date handling belongs in another class altogether. But for the time being, let's get the writing to a file sorted out. You may of course wish to do some reading from files, too. Once that is sorted out, we can get the other parts sorted.

Beware: I have forgotten just about everything I ever knew about JTables. But I know where to look it up.
 
Jameson Locke
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for organizing the code.

Do you know how to write something to a text file? 

Yes I do, but I'm having troubles implementing it with the Jtable that I've created. No videos on YouTube so far explain how to do it.

As for the date handling, I agree but just haven't gotten around to refactoring it yet.

In fact I don't like subclassing GUI classes if at all possible.


If you don't mind me asking, why ?

I'm currently in my first semester of university and this is an assignment. I'm using the exact same methods that the lecturer showed me.

Sorry for the late reply didn't think anyone would reply this quick.
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jameson Locke wrote:. . .
In fact I don't like subclassing GUI classes if at all possible.


If you don't mind me asking, why ? . . .
Because it is hardly ever necessary. You can implement 98% of what you need without making any subclasses at all.
Sorry for the late reply didn't think anyone would reply this quick.
We like to keep you guessing

So you know basic file writing and presumably file reading too. I suggest you create a Car class with a toString method which returns what you want to write in the file. Then rather than writing the text you can do something like:-
  • 1: myBufferedWriter.write(myCar.toString());
    myBufferedWriter.writeLine();
  • 2: myFormatter.format("%s%n", myCar);
  • The Formatter is quicker to write, but the buffered writer has the advantage that you can append to the file rather than overwriting it.

    Shan't be around until tomorrow.
     
    Paul Clapham
    Sheriff
    Posts: 22841
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Regardless of what Campbell said, this is not a GUI problem at all. The GUI is the view part of your application but your data is not stored in the view, it's stored in the model. In your case the DefaultTableModel.

    Now a DefaultTableModel consists of a number of rows, each of which contains the same number of columns. Your task is to get at those rows and write their data into a text file. All of your GUI code is irrelevant to that task. Does that help?
     
    Jameson Locke
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm a bit lost now I understand what both of you are saying but I cant seem to do the code.

    Now I'm having troubles writing the JTable data to text and reading text file in order for the information to stay on the table even when i close the application.

    I don't understand what I'm doing wrong the application runs fine and when I checked an hour ago it even wrote information to a txt file (only worked once).

     
    Campbell Ritchie
    Marshal
    Posts: 56586
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jameson Locke wrote:. . . I'm having troubles writing the JTable data to text and reading text file . . .
    To continue from what Paul told you, don't read from the table to the text file. You have a model which underlies the table. Each row in the model represents a Car object, so you write the details of the Car instance to the text file. Similarly you can populate the model by reading the details from the file and creating a Car object.

    To continue what I said, you need to work from the file to the table not from the table to the file. When you have the file you create Car instances, then you use those instances to create the model then the model is displayed via the table. As Paul said, the problem is not the GUI but the model. Your buttons and text fields are simply an alternative method of creating new Car instances to add to the model. I suggest you start by showing how you are reading the file. Once you have a method for adding Car instances from the file, it will be relatively easy to add more instances from the GUI.

    I am not convinced about how you are writing to the file, because you are mixing the details in the table and the contents of the text fields. You are not creating Cars anywhere that I can see. You are also not validating the contents of the text fields (but that is a minor problem). Please show an example of a file written with your text fields. Does the formatting vary from the previous lines? You are not adding spaces, so are you running all the text together into one line? Why are you using the exists() test? Why did you put the file in TEMP? Why are you keeping engine size as a String? Surely that shou‍ld be a double?

    Another reason for not subclassing display classes is that it makes you keep your business logic separate from the display classes.
     
    Paul Clapham
    Sheriff
    Posts: 22841
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your code for extracting the data from the table model and writing it to the file looks okay to me, assuming that you really wanted it to be space-delimited and all strung together on one line, that is. But I don't understand what the point is of lines 37 to 41?

    Also lines 23-26 are unnecessary, since FileWriter will create the file if isn't already there. And it will re-create the file if it is already there, too. Which is usually what you want to happen.
     
    Jameson Locke
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well I'm officially stumped, I've been trying to read and write the tables content to a txt file for the past 3 days. Watched countless videos and read way too many similar question asked on this forum and other forums.

    What Campbell Ritchie said makes sense but I simply don't know how to act on it. As for Paul is latest comment I fixed those parts.

    As it turns out I simply have not put in enough practice into JAVA to make this system.

    Thanks everyone for helping out. Also I'm not sure whether to mark this page as solved or just leave it as it is.
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think part of the reason that you are stumped is that you're not modularizing your code enough. Here is a very simplified framework. Lots of the methods here will need to be flushed out and some entirely new methods created.

    Please take the time to have all variable and method names start with a lower case letter.

    You need to make a design decision as to how a Car will be represented in your file. A simple approach is one line per Car delimited with a character that should never appear in the Car object itself. A space is not a very good delimiter, commas may be ok depending on the data you expect. Tabs would be better.
     
    Jameson Locke
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I somehow managed to do part of it. I created a demo system as a test (using text field instead of JTable to show information by clicking a button) and it was a  success, I managed to create a txt file and the text field would read it and display the information.

    Now I created a txt file for Cars, and am in the process of letting the JTable read it.

    Below is the code I used to create the txt file. (Line 34 to be precise)





    Making the TextField read was quite simple, making a JTable isn't as straight forward. I went to table events and clicked on propertychange since that makes the most since and inserted the same code I used for the TextField but there is an error on line 6. When I siwtched it out for the textfield variable name it worked fine. Did I choose the wrong events or do I have to do something else entirely.


     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How come you're not writing status and condition to the file? Won't you need them the next time you start the program?
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jameson Locke wrote:

    Once you create the BufferedReader wrapper around the FileReader object you should no longer access the FileReader directly as you are doing on line 6.
     
    Norm Radder
    Rancher
    Posts: 2240
    28
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!