Reputation: 1
I am working on a project and I am stuck on this area. I am reading text from a file and I am saving it into an arraylist. the problem is the content from the file appears in one line of text in the jtable but i want each line to be displayed in rows. I am passing the data from another class and I know this is working because I can see the contents printed out in the console row after row . I have tried a few different ways but I've run out of ideas. Any help appreciated.
Below is the code I have wrote.
for (String item : helper.getItems() )
{
System.out.println(item);
storage.add(item);
}
JTable t1 = new JTable();
t1.setModel(new DefaultTableModel(
new Object[][]{
{storage.toString()}
},
new String[]{
"Tool Equipment"
}
));
Upvotes: 0
Views: 287
Reputation: 1
The best way to add items to a JTable
using the DefaultTableModel
object is to utilize the .addRow
method of the DefaulTableModel
. You'll need to parse the storage
string to deliminate the values you want placed into each row/col
For Example:
DefaultTableModel model=new DefaultTableModel();
// parsing of the storage obj, to get individual values for each col/row
// depending on the structure of storage a looping construct would be beneficial here
String col1= storage.substring(startindex, endindex);
String col2=storage.substring(startindex, endindex);
//add items to the model in a new row
model.addRow(new Object[] {col1, col2});
// if you used a loop to parse the data in storage, end it here
// add model to the table
t1.setModel(model);
Upvotes: 0
Reputation: 1847
storage.toString()
will give you string representation of your ArrayList
. What you want is probably List#toArray
Upvotes: 1