首页 > 解决方案 > 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 的条目时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]

我究竟做错了什么?先感谢您。

标签: javaswingjtable

解决方案


Swing 被设计为单线程,对模型或组件的所有更新都应在Event Dispatch Thread (EDT).

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

通过EDT将添加/删除代码包装在SwingUtilities.invokeLater().


推荐阅读