java - JTable 排序为添加和删除
问题描述
我有一个JTable
我经常添加的。当行号达到现在的定义值5000
时,它会删除第一个条目并添加到最后一个条目。
这是我调用添加到model
.
@Override
public void notify( final String topic, final AvxPacket packet)
{
if ( this.active.get() && avionixPacket.getPacketType() == RawPacketType.AVR_PACKET )
{
final AVRPacket avrPacket = ( AVRPacket ) packet;
if ( this.validateAgainstFilter( avrPacket ) )
{
try
{
this.model.addRow( AirpacketDecoder.decode( avrPacket ) );
}
catch ( final BadFormatException | UnspecifiedFormatError | CRCError e )
{
AirInspectorTable.LOG.warn( "Something went wrong", e );
}
}
}
}
您将在下面看到添加到 rows 的方法vector
。这vector
是保存所有条目的地方。
/** List of table entries. */
private final transient Vector<AirTableEntry> rows = new Vector<>();
/** Adds an entry to table. Takes care of table size - does not let it
grow above defined size.
@param airPacket data source */
public void addRow( final AirPacket airPacket )
{
if ( airPacket != null )
{
this.rows.add( new AirTableEntry( airPacket ) );
final int rowNumber = this.rows.size() - 1;
this.fireTableRowsInserted( rowNumber, rowNumber );
if ( this.rows.size() > AirTableModel.MAX_ROWS )
{
this.rows.remove( 0 );
this.fireTableRowsDeleted( 0, 0 );
}
}
}
另外我有一个名为的类FilterPanelEventListener
,我通过Table
添加一个RowSorter
.
/**
* Constructor which takes source table and filterTxt as an argument.
* @param sourceTable is the table to be modified.
* @param sourceFilterTxt is the filter txt.
*/
public FilterPanelEventListener( final AirInspectorTable sourceTable,
final JTextField sourceFilterTxt )
{
this.table = sourceTable;
this.filterTxt = sourceFilterTxt;
RowSorter<? extends TableModel> rs = this.table.getRowSorter();
this.rowSorter = new TableRowSorter<>( this.table.getModel() );
for ( int rowIndex = 0; rowIndex < this.table.getColumnCount(); rowIndex++ )
{
this.rowSorter.setSortable( rowIndex, false );
}
if ( rs == null )
{
this.table.setRowSorter( this.rowSorter );
rs = this.table.getRowSorter();
}
this.rowSorter =
rs instanceof TableRowSorter ? ( TableRowSorter<? extends TableModel> ) rs : null;
if ( this.rowSorter == null )
{
throw new RuntimeException( "Cannot find appropriate rowSorter: " + rs );
}
}
每当有 a 的条目时JTextField
,RowSorter
使用下面的这种方法进行排序。
/**
* Updates the filter.
* @param filter is the filter
*/
private void update( final String filter )
{
try
{
if ( filter.trim().length() == 0 )
{
this.rowSorter.setRowFilter( null );
}
else
{
this.rowSorter
.setRowFilter( RowFilter.regexFilter( "(?i)" + filter, ID_COLUMN_INDEX ) );
}
}
catch ( final ArrayIndexOutOfBoundsException ex )
{
LOG.warn( "Something went wrong", ex );
}
}
一切正常,直到Vector
达到最大值,分拣机工作正常。但是,在最大值之后,如果我倾向于排序(在 中写一些东西JTextField
,我得到了几十个异常,表格开始闪烁。我没有注意到这一点,因为我的限制通常是 100000,我实际上并没有等待那么多待填,现在看到是同时添加和删除的时候,有问题,异常是这样的:
java.lang.ArrayIndexOutOfBoundsException: 7
at javax.swing.DefaultRowSorter.convertRowIndexToModel(Unknown Source)
at javax.swing.JTable.convertRowIndexToModel(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
java.lang.NullPointerException: null
at javax.swing.DefaultRowSorter.convertRowIndexToModel(Unknown Source) ~[?:1.8.0_181]
at javax.swing.JTable.convertRowIndexToModel(Unknown Source) ~[?:1.8.0_181]
at javax.swing.JTable.getValueAt(Unknown Source) ~[?:1.8.0_181]
at javax.swing.JTable.prepareRenderer(Unknown Source) ~[?:1.8.0_181]
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source) ~[?:1.8.0_181]
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source) ~[?:1.8.0_181]
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source) ~[?:1.8.0_181]
at javax.swing.plaf.ComponentUI.update(Unknown Source) ~[?:1.8.0_181]
at javax.swing.JComponent.paintComponent(Unknown Source) ~[?:1.8.0_181]
at javax.swing.JComponent.paint(Unknown Source) ~[?:1.8.0_181]
at javax.swing.JComponent.paintChildren(Unknown Source) ~[?:1.8.0_181]
我究竟做错了什么?先感谢您。
解决方案
Swing 被设计为单线程,对模型或组件的所有更新都应在Event Dispatch Thread (EDT)
.
看起来您正在尝试从单独的线程更新模型,并且某些逻辑执行不正确。
通过EDT
将添加/删除代码包装在SwingUtilities.invokeLater()
.
推荐阅读
- c++ - g++:错误:LDFLAGS=-Wall:没有这样的文件或目录
- java - 我得到了我的 insertSorted 方法来对随机整数进行排序,但是现在我的代码没有按预期迭代 25 次,我做错了什么?
- c# - 组织字典以交换数据的最佳方式是什么?
- java - Spring Batch - 即使块引发一些异常,是否有办法提交数据?
- numpy - 将 numpy 转换为张量(keras)(损失函数)
- r - 如何使用 scale_x_discrete 更改 x 轴标签
- api - 将 Arduino 连接到 Heroku 网络连接到 Django 数据库
- python - 打印从回收视图中选择的值
- javascript - React:我使用的 Api 没有给我一个 ID 号用作唯一键
- python - 如何使用 Flask 将数字从 html 导入 Python,在 python 中对它们执行操作并将它们返回到 html