简体   繁体   English

使用ArrayList填充JTable <String>

[英]Populating a JTable using ArrayList<String>

So I try to populate jtable using my arraylist, I also want to keep 3-layer architecture 所以我尝试使用我的arraylist填充jtable,我也想保留3层架构

My DAL I read data from file and try to populate it into table 我的DAL从文件中读取数据,然后尝试将其填充到表中

   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 我的UI

I want to populate table model with arraylist I had before but im not sure how to do it. 我想用之前拥有的arraylist填充表模型,但是我不确定该怎么做。

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
    }

}

Don't use an ArrayList. 不要使用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. 您可以使用DefaultTableModel的addRow(...)方法,该方法将获取数组中的数据并将数据添加到您的模型中。

So change your method signature. 因此,更改您的方法签名。 Instead of returning an ArrayList you should return a DefaultTableModel. 而不是返回ArrayList,您应该返回DefaultTableModel。 Then you can use the model to create your JTable. 然后,您可以使用模型创建JTable。

A problem I can see you facing is the structure of your ArrayList . 我可以看到的一个问题是ArrayList的结构。 It is only one dimensional. 它只是一维的。 Data in a JTable / TableModel needs to be two dimensional rows/columns JTable / TableModel数据必须是二维rows/columns

If you made your getinformationforthetable() return an ArrayList<ArrayList<String>> then you would be able to more easily populate your table. 如果使getinformationforthetable()返回ArrayList<ArrayList<String>>则可以更轻松地填充表。

Also you might as well just extends DefaultTableModel . 同样,您也可以extends DefaultTableModel I don't see any special functionality added to your AbstractTableModel . 我看不到您的AbstractTableModel添加任何特殊功能。 Then you can just use the method addRow of DefaultTableModel to add rows. 然后,您可以使用DefaultTableModel addRow方法添加行。 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. 甚至根本不扩展DefaultTableModel You can just override it's getColumnClass() , something like this 您可以覆盖它的getColumnClass() ,就像这样

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;
            ...
        }
    }
};

In your example, you need your setTableModel (which you should rename to SetTableModel to be consistent with Java style) to maintain the data. 在您的示例中,您需要setTableModel (应将其重命名为SetTableModel以与Java样式保持一致)来维护数据。 Currently, you're keeping the data in E . 当前,您将数据保留在E

You can add a method to SetTableModel like addRow(String) and that method can do the split and keep the data within SetTableModel . 您可以像addRow(String)一样向SetTableModel添加一个方法,该方法可以进行split并将数据保留在SetTableModel

Then when you override getValueAt(int row, int column) , you pull from the local data. 然后,当您覆盖getValueAt(int row, int column) ,将从本地数据中提取。

Also, getColumnCount() could return COLUMN_HEADERS.length and getRowCount() should return the number of rows maintained in the local data. 另外, getColumnCount()可能return COLUMN_HEADERS.lengthgetRowCount()应该返回本地数据中维护的行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM