Populate JTable From File using Vector

Hi All,

I have some questions. Which are,

1. I am going to read from a text file, and store the content in a Vector. Then a part of the line(not the whole line)

1.1 Separated by a specified delimeter

1.2 Not Seperated by a specified delimeter

will be shown in each JTable cell.

I have tried this,but the whole line is coming in a single cell. How to avoid this ?

And how to fetch the data for each cell, from the text line when there is no specified delimeter(In this case, "space/Tab" is the delimeter). In JTable I have used DefaultTableModel(each row is vector of Vectors)

2.Is it fine to use DefaultTableModel as my JTable model, for the type of operation that I am going to perform ? If yes, how, if no Why ?

Hope to get help from you.

Thanks in advance

JDG

[835 byte] By [JoydeepGa] at [2007-11-26 17:21:23]
# 1

> Hi All,

> I have some questions. Which are,

> 1. I am going to read from a text file, and store the

> content in a Vector. Then a part of the line(not the

> whole line)

> 1.1 Separated by a specified delimeter

> 1.2 Not Seperated by a specified delimeter

> will be shown in each JTable cell.

> I have tried this,but the whole line is coming in a

> single cell. How to avoid this ?

> And how to fetch the data for each cell, from the

> text line when there is no specified delimeter(In

> this case, "space/Tab" is the delimeter).

You can use [url http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html]java.util.Scanner[/url] to read the file, or you can parse each line yourself and use [url http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)]String.split[/url]

Either approach should allow you to split the line up however you want.

> 2.Is it fine to use DefaultTableModel as my JTable

> model, for the type of operation that I am going to

> perform ? If yes, how, if no Why ?

You can use whatever TableModel fits your requirements. You should read [url http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]How to use Tables[/url]

Jasprea at 2007-7-8 23:49:23 > top of Java-index,Desktop,Core GUI APIs...
# 2
> 2.Is it fine to use DefaultTableModel as my JTable model, for the type of operation that I am going to perform ?Given your requirements, yes.
camickra at 2007-7-8 23:49:23 > top of Java-index,Desktop,Core GUI APIs...
# 3
So many thanks Jaspre.Its really helpful.But how will be the case if I use CustomTable Model which extends AbstractTableModel ?Plz explain with some code.
JoydeepGa at 2007-7-8 23:49:23 > top of Java-index,Desktop,Core GUI APIs...
# 4
many thanks camickr.But how to "Refresh" (I mean adding new data, to ) a table, with Custom table model which extends AbstractTableModel ?
JoydeepGa at 2007-7-8 23:49:23 > top of Java-index,Desktop,Core GUI APIs...
# 5

> But how will be the case if I use CustomTable Model

> which extends AbstractTableModel ?

I'm not sure what you mean. The java.util.Scanner or String.split methods can be used to parse your data into an array of arrays, or any other data structure that suits your needs. If you plan to use a custom TableModel, then the custom TableModel can access the data structure directly to get the contents. Did you read the tutorial at the link I posted?

> Plz explain with some code.

I'm sorry, you'll have to provide some code first showing what you're trying to do and how you attempted to solve your problem.

Jasprea at 2007-7-8 23:49:23 > top of Java-index,Desktop,Core GUI APIs...
# 6

> But how to adding new data, to a table, with Custom table model which extends AbstractTableModel ?

Why do you want to write the code yourself when the DefaultTableModel already supports this?

If you want to know how its done then read the soruce code for the DefaultTableModel and copy the code into your class.

camickra at 2007-7-8 23:49:23 > top of Java-index,Desktop,Core GUI APIs...