• Post Reply Bookmark Topic Watch Topic
  • New Topic

Text File Loading Problem in Jtable

 
Jeff Zak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is a shot of what I'm running into. The problem starts where my fixed data ends right at "Rear Tire". My Part column holds 35 pieces of fixed data
which the user then enters a description, price, and weight of. In the image it ends at Rear Tire. I was able to modify my save loop that it ignores the Part
column, so, when I save my data it only gets Desc, Wt, Price, Desc, Wt, Price, ect...to the text file. You just change the value from 0 to 1 to start counting at the second column.

Problem is when it loads it its not loading it row by row. Its just adding more lines to the Part column.






Here's the writing to JTable code. I'm thinking its the addRow part giving the problem but all the examples i see use this. Problem is that I'm using
a fixed number of rows. Any ideas?



 
Tony Docherty
Bartender
Posts: 3101
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not 100% sure I understand your problem so the following may or may not be be appropriate.

1. If your first column has a fixed set of data in it then surely your table already has all the rows it needs and so you should be adding the loaded data piece by piece to the appropriate cells and not be adding new rows.

2. Looking at the data in the image you have provided it looks like your save is saving the column header for each piece of data - please can you show us a sample section of the saved file and the code that is saving the data.
 
Jeff Zak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I'm not 100% sure I understand your problem so the following may or may not be be appropriate.

1. If your first column has a fixed set of data in it then surely your table already has all the rows it needs and so you should be adding the loaded data piece by piece to the appropriate cells and not be adding new rows.

2. Looking at the data in the image you have provided it looks like your save is saving the column header for each piece of data - please can you show us a sample section of the saved file and the code that is saving the data.


Hey Tony,

Here is the code for the saving the save button.



Here is a sample of my test save file.. The Part column has 35 parts that make up a mountain bike. The following is 2 test entries I made and saved.

Description Goblin 22"
Weight 4
Price 249
Description Reba 29"er
Weight 4
Price 350
Description
Weight
Price
Description
Weight
Price
 
Paul Clapham
Sheriff
Posts: 21708
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the example you copied expects each row in the text file to represent one row of the displayed table, with columns separated by commas. But your text file doesn't look like that, so copying the example wasn't quite right.

Assuming the format of the text file which you showed us is what you have to use, then you'd need to write some code which read three lines of code and extracted the relevant data from them before calling the addRow() method. Although your table has four columns and seems to want some Part data, which I don't see anywhere in your text format. So I'm a bit confused by that.
 
Tony Docherty
Bartender
Posts: 3101
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you have a particular requirement to save the file in the format you have provided it may be easier to change the way you are saving the data.

BTW Why are you saving each column header with piece piece of data but not saving the part no? Your file is full of redundant information but is missing what I'd consider a piece of vital information ie which part no the data relates to.

If you save the table data in CSV format you could easily read it back in and recreate your table - you may want to consider using a free CSV library such as https://sourceforge.net/projects/javacsv/ as reading/writing CSV is not necessarily as easy as it first sounds.
 
Jeff Zak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

BTW Why are you saving each column header with piece piece of data but not saving the part no? Your file is full of redundant information but is missing what I'd consider a piece of vital information ie which part no the data relates to.


My Part column is holding the part type in each cell. That is my fixed cell data. I'm making a sort of build sheet for a mountain bike but I'd like to do it in Java instead of Excel. Part has all the parts listed in it already.

Part
______

Frame:
Fork:
Rear Shock:
Headset:
ect..

This column is all fixed since there are only so many parts on a bike, about 35. This way the user can just enter a model/description, price, and weight and not have to worry about what parts they need or forgetting to include something since its all listed there for them. They can just go easily down the list.

That is the best way I can explain it.

All I want to do i re-write my saved data back into the table without adding rows below where my fixed data ends. 35 rows for 35 parts. If I can do that ignoring my part column, wonderful. If it has to be overwritten that's fine too.

I'm thinking I would need to set the values in each cell for the given row somehow, then repeat this process by using a similar for loop as to when the data was saved.

 
Tony Docherty
Bartender
Posts: 3101
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I understand what your part column is for and how it works, the problem is your saved data has no link back to the part the data represents. Now if nothing ever changes this is fine but if you ever change the order of the parts, insert new parts etc then all your saved data will no longer work with the new parts list. It's also very odd that you are blindly relying on the data aligning with the current parts rows yet for every piece of data you are saving the column headers!

Personally I'd save the data something like:

Frame, Goblin 22", 4, 249
Fork, Reba 29"er, 4, 350

If you can't/won't change the format of the saved file then you need to read each line in, strip the column name and extraneous whitespace (I notice it's full of tabs as well as space chars) from the data and then write the data to a specific table cell.
 
Jeff Zak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes I understand what your part column is for and how it works, the problem is your saved data has no link back to the part the data represents. Now if nothing ever changes this is fine but if you ever change the order of the parts, insert new parts etc then all your saved data will no longer work with the new parts list. It's also very odd that you are blindly relying on the data aligning with the current parts rows yet for every piece of data you are saving the column headers!


Well, I ended up ditching the fixed the column data. I started to realize after going through all this that if I let the user specify the part its more
advantageous to the user that way. I thought of so much the user could do with a blank slate that it just made way more sense in the long run.

I'm not sure why I kept saving the column headers, then. Mostly confusion. However, now, I'm pretty confident on it. At any rate, thanks again for the help.

Here is the save button code




Here is the import button




 
Jeff Zak
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to add to this over an issue I wan into just now. Use something more unique than " " to split your cell information. If you
have a string with two words in it, that single space will be read and the next word will appear in the next cell that accepts strings. So,
use a "\t" or something else more unique to split your data up when saving and loading the text file.

 
Tony Docherty
Bartender
Posts: 3101
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies, I had spotted this when you first posted and thought I'd warned you but must have got distracted and forgot to post a reply.

BTW This is one of the reasons I suggested you use an existing CSV style library as they should handle cases where your actual data contains the delimiter.
 
Hadijah Mohamad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you give me your sourcecode? because i can not retrieve image from database. thanks in advance. ...@...
 
Jeff Zak
Greenhorn
Posts: 23
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hadijah Mohamad wrote:can you give me your sourcecode? because i can not retrieve image from database. thanks in advance. ...@...


Can't give you my source code, sorry. That's Top Secret information. I'm also not using a database but text files instead. However, do a google search for "get image from database Java" and you'll probably find your answer in those tutorials. Same with Youtube videos you can follow. Quite a few tutorials that should help you out from the looks of it.

 
Campbell Ritchie
Sheriff
Posts: 52055
90
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hadijah Mohamad wrote:can you give me your sourcecode? because i can not retrieve image from database. thanks in advance. somebody@email.com
No. I shall remove the email address from the previous posts.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!