13

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 java.io.File;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JTable;  
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class SelectedFileTable extends JTable {
Vector<File> SelectedFiles = new Vector<File>(); 
DefaultTableModel Model    = new DefaultTableModel();

TableColumn ColumnName     = new TableColumn();
TableColumn ColumnSize     = new TableColumn();
TableColumn ColumnRmIcon   = new TableColumn(); 

ImageFilenameFilter Filter = new ImageFilenameFilter();

public SelectedFileTable() {
    super();
    this.setModel(Model);

    ColumnName.setHeaderValue(new String("Name") );
      ColumnName.setMinWidth(200);
    ColumnSize.setHeaderValue(new String("Size") );
      ColumnSize.setMinWidth(50); 
      ColumnSize.setMaxWidth(100);
    ColumnRmIcon.setHeaderValue(new String("Remove?") );
      ColumnRmIcon.setMaxWidth(100);
      ColumnRmIcon.setResizable(false);

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

    this.setShowVerticalLines(false);
    this.setShowHorizontalLines(true);
this.setAutoCreateColumnsFromModel(true);

this.addFile( new File("C:/temp/cfk.jpg") );
}
public void addFile(File file) {
    System.out.println("FileTable adding: " + file.getName() );
    if ( file.isDirectory() ) { 
        for ( File f : file.listFiles(Filter) ) {
            this.addFile(f);
        }
    } else { 
        if ( Filter.accept(file) ) {
            System.out.println("Accepting file; " + file.getName() );
            SelectedFiles.add(file);
            {
                String name = file.getName();
                Long size = new Long( file.length() );
                String tempstr = new String("X");

                System.out.println("RowItems before: " + Integer.toString(Model.getRowCount()) );
                Model.addRow( new Object[] { name, size, tempstr } );
                Model.fireTableDataChanged(); 
                System.out.println("RowItems start : " + Integer.toString(Model.getRowCount()) );
            }
            System.out.println("Done Accepting file; " + file.getName() );
        }
    }
}
public Iterator<File> iterator() { 
    return SelectedFiles.iterator();
}

}

At display/visualization time, the following exception is thrown:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:432)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:622)
at javax.swing.JTable.getValueAt(JTable.java:1903)
at javax.swing.JTable.prepareRenderer(JTable.java:3911)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2072)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1897)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
at javax.swing.JComponent.paintComponent(JComponent.java:743)
at javax.swing.JComponent.paint(JComponent.java:1006)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4979)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4925)
at javax.swing.JComponent.paint(JComponent.java:996)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1709)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
at java.awt.Component.dispatchEventImpl(Component.java:4060)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

I'm ripping my hair out - I haven't been able to find the root cause of this immensely simple use case.

4
  • 1
    I had never seen this out-of-bounds exception message until today. Thanks for helping me debug my application! Commented Dec 14, 2010 at 21:36
  • please learn java naming conventions and stick to them Commented Feb 26, 2012 at 16:35
  • All of them. What exactly is your specific issue? Commented Feb 28, 2012 at 16:26
  • @ChrisKaminski I guess that e. g. instance variables are supposed to start with a lowercase letter in Java Commented Apr 25, 2017 at 9:40

10 Answers 10

19

I ran on this issue too (with a JList and a DefaultListModel). Dmitry's answer is right.

However, there is another thing: this exception can also be thrown if you don't modify the model in Swing's Event Dispatch Thread.

Doing the following can help you avoid this exception:

SwingUtilities.invokeLater(new Runnable(){public void run(){
    //Update the model here
}});

http://www.javakb.com/Uwe/Forum.aspx/java-gui/3012/JList-JScrollPane-DefaultListModel-Updating

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Although the accepted answer is correct in this case, this is very important and was causing an issue for me
18

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

1 Comment

@Chris: More specifically, you should add columns to the TableModel instead of explicitly adding them to the JTable. It's also worth pointing out that you will get better performance from a List instead of a Vector, since you're not using the synchronization (and shouldn't have to pay for the overhead) that comes with a Vector.
5

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

Comments

1

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

Comments

1

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

}

Comments

1

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.

1 Comment

while might be true in some contexts ... it's completely unrelated to the question
1

Another issue might be related when you use RowSorter. When you edit the model RowSorter tries to re-sort the old model. It should be automatically re-created and re-run on each changes of table model.

You can fix it by

tableModel = new DefaultTableModel(data, columnNames);
jTableSentence.setModel(tableModel);
jTableSentence.setRowSorter(new TableRowSorter(tableModel));
jTableSentence.setAutoCreateRowSorter(true);

-Hayri

2 Comments

doesn't make sense to set the autoCreate property after setting the model and manually installing a new rowSorter. Instead, set the property to true before setting the tableModel and be done.
No. From API we know that This method calls createDefaultColumnsFromModel if autoCreateColumnsFromModel changes from false to true. So it can be added afterwards.
1

I was facing this problem because i was adding columns and rows to the JTable not to the model.

best way is this.

Object[][]rows = new Object[ ][ ] { {"a","b"} , {"c","d"} };
Object[]columns = new Object[] {"column1","column2"};
JTable table = new JTable();
table.setModel(new DefaultTableModel(rows,columns));

Comments

0

Dmitry is right, but you simply have to update your model. To do this, add the following to your code:

DefaultTableModel dtm = (DefaultTableModel) table.getModel();
for (int c = 0; c < table.getColumnCount(); c++) {
dtm.addColumn(table.getColumnName(c));
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.