1

So I try to populate jtable using my arraylist, I also want to keep 3-layer architecture

My DAL I read data from file and try to populate it into table

   public class E {
         public  ArrayList<String> getinformationforthetable() {
        Scanner s = null;
          ArrayList<String> data = new  ArrayList<String>();
        try {
            s = new Scanner(new File("songs.txt"));
            while (s.hasNextLine()) {
            String line = s.nextLine();
            if (line.startsWith("")) {
                String[] atoms = line.split("[#]");
                 ArrayList<String> row = new  ArrayList<String>();
                row.add(atoms[0]);
                row.add(atoms[1]);
                        row.add(atoms[2]);
                        row.add(atoms[3]);
                        row.add(atoms[4]);
                        row.add(atoms[5]);
                data.addAll(row);
            }
            }
        }
        catch(IOException e) {
            e.printStackTrace();    
        }
        finally {
            if (s != null) {
            s.close();
            }
        }
        return data;
        }

    }

My UI

I want to populate table model with arraylist I had before but im not sure how to do it.

public class setTableModel extends AbstractTableModel{
   private static final String[] COLUMN_HEADERS =
    {
        "Title", "Artist", "Gengre", "Quality", "Duration","Favorite"
    };
    private static final Class[] COLUMN_TYPES =
    {
       String.class, String.class,String.class,String.class, Integer.class, Boolean.class
    };
    @Override
    public int getRowCount() {
        return COLUMN_HEADERS.length;
    }

    @Override
    public int getColumnCount() {
       return null;
       //todo
    }

    @Override
    public Object getValueAt(int i, int i1) {
       return null;
      //todo
    }

}

3 Answers 3

4

Don't use an ArrayList.

String[] atoms = line.split("[#]");
//ArrayList<String> row = new  ArrayList<String>();
model.addRow( atoms );

You already have the data in an Array. You can use the addRow(...) method of the DefaultTableModel which will take the data in the array and add the data to the model for you.

So change your method signature. Instead of returning an ArrayList you should return a DefaultTableModel. Then you can use the model to create your JTable.

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

2 Comments

Can you explain a littie more about adding it to model ?
Read the DefaultTableModel API. There is a constructor that will allow you create create a TableModel with zero rows and only the column names. So you would do this before you start executing your looping code. Then in the loop you simply use the code I gave in my answer to add each row to the model.
3

A problem I can see you facing is the structure of your ArrayList. It is only one dimensional. Data in a JTable/TableModel needs to be two dimensional rows/columns

If you made your getinformationforthetable() return an ArrayList<ArrayList<String>> then you would be able to more easily populate your table.

Also you might as well just extends DefaultTableModel. I don't see any special functionality added to your AbstractTableModel. Then you can just use the method addRow of DefaultTableModel to add rows. Something like this

DefaultTableModel model = new MyDefaultTableModel();
E e = new E();
ArrayList<ArrayList<String>> list = e.getinformationforthetable();
for (ArrayList<String> row : list) {
    model.addRow(row.toArray());
}
table.setModel(model);

Or don't even extend DefaultTableModel at all. You can just override it's getColumnClass(), something like this

private static final String[] COLUMN_HEADERS =
{
    "Title", "Artist", "Gengre", "Quality", "Duration","Favorite"
};
DefaultTableModel model = new DefaultTableModel(COLUMN_HEADERS, 0) {
    @Override
    public Class getColumnClass(int column) {
        switch (column) {
            case 0: return String.class; break;
            case 1: return String.class; break;
            ...
        }
    }
};

Comments

2

In your example, you need your setTableModel (which you should rename to SetTableModel to be consistent with Java style) to maintain the data. Currently, you're keeping the data in E.

You can add a method to SetTableModel like addRow(String) and that method can do the split and keep the data within SetTableModel.

Then when you override getValueAt(int row, int column), you pull from the local data.

Also, getColumnCount() could return COLUMN_HEADERS.length and getRowCount() should return the number of rows maintained in the local data.

Comments

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.