gpt4 book ai didi

java - JTable 行排序器 - IllegalArgumentException : Invalid SortKey

转载 作者:行者123 更新时间:2023-12-01 23:47:57 26 4
gpt4 key购买 nike

有人可以帮我解决这个问题吗?它一直有效,直到我改变了一些东西试图优化它......该死!

这是我的 table 模型:

class MyTableModel extends DefaultTableModel {

private String[] columnNames = {"Last Name","First Name","Next Visit"}; //column header labels
Object[][] data = new Object[100][3];

public void reloadJTable(List<Customer> list) {
for(int i=0; i< list.size(); i++){
data[i][0] = list.get(i).getLastName();
data[i][1] = list.get(i).getFirstName();
if (list.get(i).getNextVisit()==null) {
data[i][2] = "NOT SET";
} else {
String date = displayDateFormat.format(list.get(i).getNextVisit().getTime());
data[i][2] = date;
}
model.addRow(data);
}
}

public void clearJTable() {
model.setRowCount(0);
}

public String getColumnName(int col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}

/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
}

这就是我实现 JTable 的方式:

// these declarations are all private shared across the model
JTable customerTbl;
MyTableModel model;
List<Customer> customers = new ArrayList<Customer>();
SimpleDateFormat displayDateFormat = new SimpleDateFormat ("EEE dd-MM-yyyy 'at' hh:mm");

//JTable configuration
model = new MyTableModel();
customerTbl = new JTable(model);
model.reloadJTable(customers);
customerTbl.setAutoCreateRowSorter(true); //enable row sorters

DefaultRowSorter sorter = ((DefaultRowSorter)customerTbl.getRowSorter()); //default sort by Last Name
ArrayList list = new ArrayList();
list.add( new RowSorter.SortKey(0, SortOrder.ASCENDING));
sorter.setSortKeys(list); //EXCEPTION HERE
sorter.sort();

customerTbl.getColumnModel().getColumn(0).setPreferredWidth(100); //set Last Name column preferred width
customerTbl.getColumnModel().getColumn(1).setPreferredWidth(80); //set First Name column preferred width
customerTbl.getColumnModel().getColumn(2).setPreferredWidth(150); //set Last Visit column preferred width

我触发了以下异常:

sorter.setSortKeys(列表);

Exception in thread "main" java.lang.IllegalArgumentException: Invalid SortKey
at javax.swing.DefaultRowSorter.setSortKeys(Unknown Source)
at com.vetapp.customer.CustomersGUI.<init>(CustomersGUI.java:128)
at com.vetapp.main.VetApp.main(VetApp.java:31)

我相信这与未正确创建的 TableColumnModel 有关...

最佳答案

主要问题是列计数从 TableModel 的 父类(super class) DefaultTableModel 返回 0。您需要重写此方法

@Override
public int getColumnCount() {
return columnNames.length;
}
<小时/>

另一个潜在的致命问题是 getColumnClass 返回 TableModel 中的元素类。如果表为空,这将抛出 NullPointerException。请使用类文字,例如 String.class

对于 DefaultTableModel 来说,不需要维护单独的支持数据数组。它已经有了自己的数据 vector 。扩展 AbstractTableModel 时使用此方法。

关于java - JTable 行排序器 - IllegalArgumentException : Invalid SortKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16725758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com