0

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"
            }

    ));
7
  • You have a loop that adds the data to an ArrayList. Why are you doing that first? You can just use the DefaultTableModel.addRow(..) method to add the data directly to the model, so there is no need for an ArrayList. Commented Nov 15, 2013 at 20:29
  • thanks for the comment, i dont really understand what you are saying. can i not use an arraylist to display the text in the jtable? Commented Nov 15, 2013 at 20:41
  • No, the data needs to be added to a TableModel. Commented Nov 15, 2013 at 21:09
  • so i add the the item to the tablemodel inside the for loop and then just set the model to that for the table? Commented Nov 15, 2013 at 21:17
  • What happened when you tried it? I hope you didn't wait 4 hours for me to respond to your question.; Commented Nov 16, 2013 at 2:05

2 Answers 2

1

storage.toString() will give you string representation of your ArrayList. What you want is probably List#toArray

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for replying, So are you saying to put the storage.toArray() because I have tried that and I dont get anything when I run it.
0

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);

1 Comment

Hi, Thanks for replying. Im not too sure how I would use the startindex and endindex for what I want to do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.