获取IllegalArgumentException:使用setSortKeys()的SortKey无效

时间:2015-01-14 18:59:40

标签: java swing

我在处理SortKeys方面遇到了一些麻烦。我有一个使用自定义TableModel的表。我试图使用SortKeys对表进行排序。但是出于某些原因,我在第sorter.setSortKeys(sortKeys);行收到错误。有人可以帮忙吗?

我的TableModel类是这样的:

class WrongAnswersTableModel extends AbstractTableModel {

private static final long serialVersionUID = 1L;

private ArrayList<Card> wrongAnswers;

public WrongAnswersTableModel(ArrayList<Card> wrongAnswers) {
    this.wrongAnswers = wrongAnswers;
}

@Override
public int getColumnCount() {
    return 3;
}

@Override
public int getRowCount() {
    return wrongAnswers.size();
}

@Override
public String getColumnName(int column) {
    String name = "";

    switch (column) {
    case 0:
        name = "Word";
        break;
    case 1:
        name = "Translation";
        break;
    case 2:
        name = "Times Incorrect";
    }

    return name;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Card card = wrongAnswers.get(rowIndex);
    String value = "";

    switch (columnIndex) {
    case 0:
        value = card.getLangFromPrompt();
        break;
    case 1:
        value = card.getLangToPrompt();
        break;
    case 2:
        value = "" + card.getGuessedWrong();
        break;
    }

    return value;
}
}

我查看了StackOverflow的其余部分,并且有一些关于getColumnCount返回0的内容,但在我的模型中,getColumnCount肯定会返回3.

我按照以下方式设置了我的桌子和分拣机:

List<SortKey> sortKeys = new ArrayList<SortKey>();
sortKeys.add(new SortKey(2, SortOrder.DESCENDING));     
watm = new WrongAnswersTableModel(parent.getTest().getIncorrectCards());
table = new JTable(watm);
table.setEnabled(false);
table.setRowHeight(table.getFont().getSize() + 10);
sorter = new TableRowSorter<TableModel>(table.getModel());
sorter.setSortKeys(sortKeys);
table.setRowSorter(sorter);
((AbstractTableModel) table.getModel()).fireTableDataChanged();
sorter.sort();

感谢您提供任何帮助!

0 个答案:

没有答案