gpt4 book ai didi

java - GlazedLists 清除在排序的 JTable 上抛出错误

转载 作者:行者123 更新时间:2023-12-01 14:32:42 24 4
gpt4 key购买 nike

我想在使用 JTables 和 JXTables 的现有项目中包含 Gladedlists。主要是因为事件处理更容易。我做了一个 View 测试,在交换完整列表内容时遇到了问题。我通过调用来做到这一点

eventList.clear();
eventList.addAll(...);

仅当激活 JTable 的标准表排序时才会发生该错误。

网上有很多关于这个主题的提示,但我不明白这里的问题是什么。至少,gladedlists 不能与标准 JTable 排序一起使用吗?我使用的是java7,glanedLists 1.9.0,示例代码是:

package poc;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.swing.AdvancedTableModel;
import ca.odell.glazedlists.swing.GlazedListsSwing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
* TestGlazedLists
*
*/
public class TestGlazedLists {

public static class Person implements Comparable<Person>{
private String name;
private String surname;

public Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}

@Override
public int compareTo(Person o) {
return this.getName().compareTo(o.getName());
}
}

private BasicEventList eventList = new BasicEventList();
private SortedList sortedList = new SortedList(eventList);

private java.util.List<Person> createList() {
ArrayList<Person> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add(new Person("James" + i, "Bond" + i));
}
return list;
}

public TestGlazedLists() {
}

public static void main(String[] args) {
new TestGlazedLists().openDialog();
}

private void openDialog() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAll();
}
});
}

public void initAll() {
JDialog d = new JDialog();
d.setLayout(new BorderLayout());
JButton refreshB = new JButton("refresh");
d.add(refreshB, BorderLayout.NORTH);
refreshB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
refreshList();
}
});
JTable t = new JTable();
t.setAutoCreateRowSorter(true);

d.add(new JScrollPane(t), BorderLayout.CENTER);

String[] propertyNames = {"name", "surname"};
String[] columnLabels = {"name", "surname"};
boolean[] writable = {false, false};

AdvancedTableModel<Person> model = GlazedListsSwing.eventTableModel(eventList, propertyNames, columnLabels, writable);
t.setModel(model);

//TableFormat tableFormat = GlazedLists.tableFormat(propertyNames, columnLabels, writable);
//JXTableSupport.install(t, eventList, tableFormat, sortedList, AbstractTableComparatorChooser.MULTIPLE_COLUMN_MOUSE);

d.setPreferredSize(new Dimension(500, 300));
d.pack();
d.setVisible(true);
}

public void refreshList() {
eventList.clear();
eventList.addAll(createList());
}
}

点击第一次“刷新”有效,点击第二次崩溃

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Invalid range
at javax.swing.DefaultRowSorter.rowsDeleted(DefaultRowSorter.java:880)
at javax.swing.JTable.notifySorter(JTable.java:4273)
at javax.swing.JTable.sortedTableChanged(JTable.java:4117)
at javax.swing.JTable.tableChanged(JTable.java:4394)
at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:296)
at ca.odell.glazedlists.swing.DefaultEventTableModel.handleListChange(DefaultEventTableModel.java:140)
at ca.odell.glazedlists.swing.DefaultEventTableModel.listChanged(DefaultEventTableModel.java:118)
at ca.odell.glazedlists.event.ListEventAssembler$ListEventFormat.fire(ListEventAssembler.java:424)
at ca.odell.glazedlists.event.ListEventAssembler$ListEventFormat.fire(ListEventAssembler.java:421)
at ca.odell.glazedlists.event.SequenceDependenciesEventPublisher$SubjectAndListener.firePendingEvent(SequenceDependenciesEventPublisher.java:445)
at ca.odell.glazedlists.event.SequenceDependenciesEventPublisher.fireEvent(SequenceDependenciesEventPublisher.java:344)
at ca.odell.glazedlists.event.ListEventAssembler.commitEvent(ListEventAssembler.java:317)
at ca.odell.glazedlists.BasicEventList.clear(BasicEventList.java:206)

我也尝试过 JXTable 的变体,但结果是相同的。

最佳答案

让 GlazedLists 为您处理 JTable 排序。

第 1 步:删除/注释掉自动行排序器行:

t.setAutoCreateRowSorter(true); // remove me: I interfere with GlazedLists

第二步:在模型中使用排序的事件列表,而不是基本的:

AdvancedTableModel<Person> model = GlazedListsSwing.eventTableModel(sortedList, propertyNames, columnLabels, writable);

第三步:安装 GlazedLists TableComparitorChooser到您的 JTable,这会向 JTable 添加行排序功能:

TableComparatorChooser<Person> tableSorter = TableComparatorChooser.install(
t, sortedList, TableComparatorChooser.SINGLE_COLUMN);

您现在应该会发现刷新后一切正常。单击列标题将启用列排序。如果您不喜欢 TableComparitorChooser 的多列排序方式,您可以尝试使用它的一些选项。

关于java - GlazedLists 清除在排序的 JTable 上抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16738232/

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