JTable无法从Vector获取数据

时间:2015-06-13 23:41:18

标签: java swing jtable

我的JTable有问题:它似乎是emty并且没有从Vector传递给它的对象中获取任何数据。我检查了相关主题,但遗憾的是我没有找到任何理由解决这个问题,几乎相同的代码实际上对我有用。 为了进一步测试,我在按钮上单击创建对象,将它们添加到我的Vector中,然后尝试刷新表格。 Printlns显示对象实际上已添加到Vector中,但表仍为空。我假设,问题必须在表代码中的某处。 我发布了我的测试课和相关课程,希望你能帮助我。

测试类:

public class EntryTableModel extends DefaultTableModel {

    private Vector<Entry> entryList;

    public EntryTableModel(final Vector<Entry> eList, final Vector<String> columnNames) {
        super();
        this.columnIdentifiers = columnNames;
        entryList = (Vector<Entry>) eList;
        this.setDataVector(eList);
    }

    void setDataVector(final Vector<Entry> eList) {
        final Vector<Vector<String>> rows = new Vector<Vector<String>>();
        for (Entry e : eList) {
            final Vector<String> entryAsVector = new Vector<String>();
            entryAsVector.add("" + e.getStudyYear());
            entryAsVector.add("" + e.getContractNumber());
            entryAsVector.add(e.getName());

            entryAsVector.add("" + e.getBirthDate());
            entryAsVector.add(e.getAddress());
            entryAsVector.add("" + e.getPracticeDateStart());

            entryAsVector.add("" + e.getPracticeDateEnd());
            entryAsVector.add(e.getRailway());
            entryAsVector.add(e.getStudyForm());

            entryAsVector.add("" + e.getDirectionNumber());
            entryAsVector.add("" + e.getRating());
            entryAsVector.add("" + e.getAverage());

            entryAsVector.add(e.getProfession());
            entryAsVector.add(e.getPracticeBase());
            entryAsVector.add(e.getInhabitation());
        }
        this.setDataVector(rows, columnIdentifiers);
    }

    public boolean isCellEditable(int row, int column) {
        return false;
    }

    public Entry giveEntryInRow(int row) {
        return entryList.get(row);
    }

TableModel类:

public class EntryControl {
    private Vector <Entry> entryList = new Vector <Entry> ();
    public void addEntry(Entry e) throws EntryExistsException{
        if (entryList.contains(e))
            throw new EntryExistsException(e, "");
        else
            entryList.add(e);
    }
    public Vector<Entry> listAllEntry(){
        return entryList;
    }
//reference in main control class
    public Vector<Entry> giveAllEntry(){
        return entryControl.listAllEntry();
    }

赋予我的对象Vector

的功能
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" id="Application_ID" version="7">
  <display-name>Fantasy</display-name>
  <module>
    <ejb>FantasyEjb.jar</ejb>
  </module>
  <module>
    <web>
      <web-uri>FantasyWeb.war</web-uri>
      <context-root>FantasyWeb</context-root>
    </web>
  </module>
</application>

输出: http://i57.tinypic.com/125geac.jpg

2 个答案:

答案 0 :(得分:1)

setDataVector方法中,您没有向rows添加任何内容。您可能想要添加以下行:

rows.add(entryAsVector);

如果您在将rows传递给this.setDataVector时检查了SELECT COUNT(INS_NAME) as COUNT_A from table_A where INS_NAME IN ( SELECT INS_NAME from table_B WHERE INS_ID IN (SELECT INS_MAP_ID FROM TEN_TO_INST_MAP where T_IN_MAP_ID = (SELECT T_ID FROM TW WHERE TNAM = 'abc'))) AND T_DATE between '2015-01-01' and '2015-07-01' AND INS_NAME NOT LIKE 'x%pr%' AND INS_NAME like 'x%y%' AND AND INS_NAME not like 'x%y%z' UNION ALL SELECT COUNT(INS_NAME) as COUNT_B from table_A where INS_NAME IN ( SELECT INS_NAME from table_B WHERE INS_ID IN (SELECT INS_MAP_ID FROM T_IN_MAP where T_IN_MAP_ID = (SELECT T_ID FROM TW WHERE TNAM = 'abc'))) AND T_DATE between '2015-01-01' and '2015-07-01' AND INS_NAME NOT LIKE 'x%pr%' AND INS_NAME like 'x%as%' UNION ALL SELECT COUNT(INS_NAME) as COUNT_C from table_A where INS_NAME IN ( SELECT INS_NAME from table_B WHERE INS_ID IN (SELECT INS_MAP_ID FROM T_IN_MAP where T_IN_MAP_ID = (SELECT T_ID FROM TW WHERE TNAM = 'abc'))) AND T_DATE between '2015-01-01' and '2015-07-01' AND INS_NAME NOT LIKE 'x%pr%' AND INS_NAME like 'x%dfg%' UNION ALL SELECT COUNT(INS_NAME) as COUNT_D from table_A where INS_NAME IN ( SELECT INS_NAME from table_B WHERE INS_ID IN (SELECT INS_MAP_ID FROM T_IN_MAP where T_IN_MAP_ID = (SELECT T_ID FROM TW WHERE TNAM = 'abc'))) AND T_DATE between '2015-01-01' and '2015-07-01' AND INS_NAME NOT LIKE 'x%pr%' AND INS_NAME like 'x%y%z' 的值,可以通过添加print语句或使用调试器来自行发现此问题。

答案 1 :(得分:1)

你的TableModel类有点精神分裂,因为它扩展了DefaultTableModel,这个类已经有了自己的数据核心,但尽管如此,它还拥有自己的数据核,entryList,忽略了DefaultTableModel父类所具有的核。您需要执行以下两项操作之一:使用您的entryList字段并扩展AbstractTableModel,或者删除它并确保将您的数据传递给父DefaultTableModel的超级构造函数调用。