Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bit4woo committed Apr 23, 2024
1 parent 521fe17 commit 83ab06c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/title/LineTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -995,20 +995,22 @@ public void addNewLineEntry(LineEntry lineEntry){
LineEntry ret = lineEntries.put(key,lineEntry);
//以前的做法是,put之后再次统计size来判断是新增还是替换,这种方法在多线程时可能不准确,
//concurrentHashMap的put方法会在替换时返回原来的值,可用于判断是替换还是新增
titleDao.addOrUpdateTitle(lineEntry);//写入数据库

int index = lineEntries.IndexOfKey(key);
try {
if (ret == null) {
fireTableRowsInserted(index, index);
//出错只会暂时影响显示,不影响数据内容,不再打印
//这里偶尔出现IndexOutOfBoundsException错误,但是debug发现javax.swing.DefaultRowSorter.checkAgainstModel在条件为false时(即未越界)抛出了异常,奇怪!
//大概率是因为排序器和数据模型不同步导致的,但是每次同步排序器会导致界面数据不停刷新,这个过程中难以操作数据表。
}else {
fireTableRowsUpdated(index, index);
}
} catch (Exception e) {
e.printStackTrace(stderr);
//e.printStackTrace(stderr);
}

titleDao.addOrUpdateTitle(lineEntry);//写入数据库
//need to use row-1 when add setRowSorter to table. why??
//https://stackoverflow.com/questions/6165060/after-adding-a-tablerowsorter-adding-values-to-model-cause-java-lang-indexoutofb
//fireTableRowsInserted(newsize-1, newsize-1);
Expand Down
16 changes: 3 additions & 13 deletions src/title/TitlePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,29 +442,19 @@ private void loadData(LineTableModel titleTableModel){
TableRowSorter<LineTableModel> tableRowSorter = new TableRowSorter<>(titleTableModel);
titleTable.setRowSorter(tableRowSorter);

if (titleTableModel != null && tableRowSorter != null) {
boolean enable =false;//deprecate这段代码
if (enable && titleTableModel != null && tableRowSorter != null) {
titleTableModel.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.INSERT ||
e.getType() == TableModelEvent.DELETE ||
e.getType() == TableModelEvent.UPDATE) {
//addNewLineEntry中fireTablexxx经常发生越界错误,大概率是因为排序器和数据模型不同步导致的
tableRowSorter.modelStructureChanged(); // 更新排序器的视图,这会导致排序被重置
/*
List<? extends RowSorter.SortKey> currentSortKeys = tableRowSorter.getSortKeys();
tableRowSorter.modelStructureChanged(); // 更新排序器的视图,这会导致排序被重置
if (currentSortKeys != null && !currentSortKeys.isEmpty()) {
tableRowSorter.setSortKeys(currentSortKeys); // 恢复之前的排序状态
//java.lang.IllegalArgumentException: Comparison method violates its general contract!
}
*/

tableRowSorter.modelStructureChanged(); // 更新排序器的视图,这会导致排序被重置,导致界面不停刷新,影响数据操作,Deprecated
}
}
});
}else {
System.out.println("TableModel or TableRowSorter is null");
}

titleTable.setModel(titleTableModel);
Expand Down

0 comments on commit 83ab06c

Please sign in to comment.