java.lang.ArrayIndexOutOfBoundsException: 0 >= 0 attempting to populate JTable

前端 未结 10 1597
猫巷女王i
猫巷女王i 2020-12-20 13:37

I\'m subclassing JTable and using a DefaultTableModel to model my table data. The following class sets up the JTable, and adds one row to the model.

import          


        
相关标签:
10条回答
  • 2020-12-20 14:14

    From the JTable.setAutoCreateColumnsFromModel() API:

    "This method calls createDefaultColumnsFromModel if autoCreateColumnsFromModel changes from false to true. "

    Vector throws ArrayIndexOutOfBoundsException - if the index is out of range ( index < 0 || index >= size())

    I guess the table model is missing the columns, as suggested by Dmitry

    0 讨论(0)
  • 2020-12-20 14:22

    I think you need to add columns to your TableModel. Your code adds UI columns to the table but doesn't add them to the model

    0 讨论(0)
  • 2020-12-20 14:26

    Replace your code with the following Here you need to remove first row only that should be iterated for all the rows

    private void refreshTable() {
    
       int rowCount= model.getRowCount();
    
      // System.out.println(rowCount);
    
       for(int i=0;i<rowCount;i++ ){
            model.removeRow(0);
            //System.out.println(i);
       }
    
    }
    
    0 讨论(0)
  • 2020-12-20 14:26

    replace

    tabWindow.addTab(...);
    

    with

    SwingUtilities.invokeLater(new Runnable() {
        @Override public void run() {
            tabWindow.addTab(...);
        }
    });
    

    Such situation can hapen when you change tabbed pane in action listener.

    0 讨论(0)
  • 2020-12-20 14:28

    It is necessary to add isCellEditable method to your table model class with return false.

    @Override
      public boolean isCellEditable(int row, int column)
      {
        return false;
      }
    
    0 讨论(0)
  • 2020-12-20 14:29

    Dmitry is right. Replace

    this.addColumn(ColumnName);
    this.addColumn(ColumnSize);
    this.addColumn(ColumnRmIcon);
    

    with

    Model.addColumn(ColumnName);
    Model.addColumn(ColumnSize);
    Model.addColumn(ColumnRmIcon);
    

    and now the Model knows about the columsn and won't throw an Exception anymore when you try to add a row to a model which thinks it has 0 columns

    0 讨论(0)
提交回复
热议问题