Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

loading a text file database into a Jtable  RSS feed

 
Jimmy Bua
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I posted this at someplace else but no one was any help.. I'm trying to get a Jtable to load a very simple .txt file database. It looks like this:



I would like a line in the database to show up as a row in the JTable. I have searched the net, and there isn't that much info on using a text file like the above example.
Any help would be appreciated!
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's break down the problem. At its simplest, a JTable can be created with a two dimensional array. So, you need to get the content of the txt file into such an array. Do you know how to read a file from Java code? Do you know how to parse a comma-separated line into an array of Strings? If you can do those things, you'll be most of the way there.
 
Campbell Ritchie
Sheriff
Posts: 53760
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can never remember how JTables work, but you need to pass arrays of arrays of Objects to their models. You can read about them in the Java™ Tutorials. Note that your text file is not a database, but a text file. You will have to parse the individual lines to produce different objects to fill the JTable with.
Try creating a table row class with a constructor taking a single String as its parameter. After a few minutes of this you will know why you don't use Strings for that sort of thing. You will have fields like number, name, address, town, zip in your class. You will become dependent on the lines being in the correct format and you may have to throw lots of IllegalArgumentException‍s(←link) if you have the wrong format.
 
Jimmy Bua
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UPDATE:
Having a problem with it not displaying the 2nd column
Here is the current code I have:



Trying with the following Text file for testing purposes:
 
Carey Brown
Saloon Keeper
Posts: 2536
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change line 128 to
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!