简体   繁体   English

从向量动态更新JTable <vector<string> &gt;

[英]Dynamically updating a JTable from a vector<vector<string>>

I have a function called FetchInbox() which fetches the header information (Sender, subject, date sent) of an email and then adds it to a Vector of String Vectors. 我有一个名为FetchInbox()的函数,该函数获取电子邮件的标头信息(发件人,主题,发送日期),然后将其添加到String Vectors的Vector中。

What I want to be able to do is to refresh this table as new emails come in and update the table by first running FetchInbox() again, and then using this to repopulate the table. 我希望能够做的是在收到新电子邮件时刷新此表并通过再次运行FetchInbox()来更新表,然后使用它重新填充表。

I know this can be done using a TableModel, but I have yet to find a example which uses Vectors and not Object[][]. 我知道可以使用TableModel完成此操作,但是我还没有找到使用Vector而不使用Object [] []的示例。 Any assistance with this would be appreciated. 任何帮助,将不胜感激。

DefaultTableModel has constructors and methods that take Vectors instead of Object[]s. DefaultTableModel具有采用Vector而不是Object []的构造函数和方法。

The old version of DefaultTableModel only used Vectors, the Object[] parameters are newer methods that were added around the time Generics came to Java. DefaultTableModel的旧版本仅使用Vector,而Object []参数是较新的方法,它们是在泛型Java出现时添加的。

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

When you create a table without providing it a model , it will have DefaultTableModel as it's default model. 在不提供model的情况下创建表时,它将具有DefaultTableModel作为其默认模型。 This model has two function: 该模型具有两个功能:

  1. setDataVector(Vector dataVector, Vector columnIdentifiers) : Where dataVector is a Vector (which represents the data rows of table) of Vector and comlumnIdentifiers is Vector containing identifiers. setDataVector(Vector dataVector, Vector columnIdentifiers) :其中dataVectorVector (表示表的数据行)的Vector而comlumnIdentifiers是包含标识符的Vector It will show your table as you are providing the Vector. 提供Vector时,它将显示您的表格。

  2. addRow(Vector dataRow) : it will add a data row to your dataVector as defined above. addRow(Vector dataRow) :它将如上所述将数据行添加到您的dataVector中。

So it is really simple to get the model and invoke these function: 因此,获取模型并调用以下函数确实非常简单:

 DefaultTableModel model = (DefaultTableModel) table.getModel();
 model.setDataVector(dataVector, comlnIdentifiers);

In your context, dataVector has the type vector<vector<string> > . 在您的上下文中, dataVector的类型为vector<vector<string> > But depending on Vector is not really a good choice. 但是依赖Vector并不是一个很好的选择。 It is much safer and effective if your directly work with Object[] . 如果直接使用Object[]它将更加安全有效。 The DefaultTableModel has similar function with Object array too. DefaultTableModel也具有与Object数组相似的功能。

  1. setDataVector(Object[][] dataVector, Object[] columnIdentifiers)
  2. addRow(Object[] rowData)

Check out the Tutorial page: How to Use Table to know many more things you can do with table and it's model. 请查看“教程”页面: 如何使用表,以了解您可以对表及其模型进行的更多操作。

This should work, but @jzd's answer is probably what you want, with the caveat that, according to the documentation, the column Vector s might be truncated or padded if their length does not match the number of columns you want in your table. 这应该可以,但是@jzd的答案可能是您想要的,但需要注意的是,根据文档,如果Vector的长度与表中所需的列数不匹配,则Vector的列可能会被截断或填充。

import javax.swing.*;
import javax.swing.table.*;
import java.util.*;

class test{
  public static void main(String[] _) {

    // Test data.
    final Vector<Vector<String>> rows = new Vector<Vector<String>>();
    for (int i = 0; i < 4; i++) {
      Vector<String> row = new Vector<String>();
      for (int j = 0; j < 5; j++) {
        row.add(String.format("%s, %s", i, j));
      }
      rows.add(row);
    }

    // With AbstractTableModel, you only need to implement three methods.
    TableModel model = new AbstractTableModel() {
      public int getRowCount() {
        return rows.size();
      }
      public int getColumnCount() {
        return rows.elementAt(0).size();
      }
      public Object getValueAt(int row, int column) {
        return rows.elementAt(row).elementAt(column);
      }
    };

    // Test the TableModel in a JTable.
    JFrame jf = new JFrame("test");
    jf.setSize(512, 384);
    jf.setContentPane(new JScrollPane(new JTable(model)));
    jf.show();

  }
}

have a look at GlazedLists - it'll save you a ton of time. 看看GlazedLists-它可以为您节省大量时间。 with it you can dynamically bind a JTable to a List of objects such that any change in the objects is reflected in the table and vice-versa. 使用它,您可以将JTable动态绑定到对象列表,这样对象中的任何更改都会反映在表中,反之亦然。

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

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