JTable排序为添加和删除

时间:2018-10-25 13:48:12

标签: java swing 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]

我在做什么错? 预先谢谢你。

1 个答案:

答案 0 :(得分:0)

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

您似乎正在尝试从单独的线程更新模型,并且某些逻辑未按顺序执行。

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