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 );
}
}
}
}

您将在下面看到添加到行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 );
}
}

每当有条目指向JTextFieldRowSorter,请使用下面的方法进行排序。

/**
* 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)上完成。

看起来您正在尝试从单独的线程更新模型,并且某些逻辑执行顺序不正确。

通过将添加/删除代码包装在SwingUtilities.invokeLater()中,确保对模型的所有更新都在EDT上完成。

最新更新