1

Found this nice sample program illustrating the use of a TableModel.

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TableExample extends JFrame
{
public TableExample()
{
    //headers for the table
    String[] columns = new String[] {
        "Id", "Name", "Hourly Rate", "Part Time"
    };

    //actual data for the table in a 2d array
    Object[][] data = new Object[][] {
        {1, "John", 40.0, false },
        {2, "Rambo", 70.0, false },
        {3, "Zorro", 60.0, true },
    };

    final Class[] columnClass = new Class[] {Integer.class, String.class, Double.class, Boolean.class};

    //create table model with data
    DefaultTableModel model = new DefaultTableModel(data, columns) {

        @Override
        public boolean isCellEditable(int row, int column)
        {
            System.out.println("Inside isCellEditable("+ row +","+ column + ")");
            return false;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex)
        {
            System.out.println("Inside getColumnClass("+ columnIndex +")");
            return columnClass[columnIndex];
        }
    };

    JTable table = new JTable(model);

    //add the table to the frame
    this.add(new JScrollPane(table));

    this.setTitle("Table Example");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    this.pack();
    this.setVisible(true);
}

public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new TableExample();
        }
    });
}   
}

System.out.println statements were added and noticed when the application loaded it produced the following output :

Inside getColumnClass(0)
Inside getColumnClass(1)
Inside getColumnClass(2)
Inside getColumnClass(3)
Inside getColumnClass(0)
Inside getColumnClass(1)
Inside getColumnClass(2)
Inside getColumnClass(3)
Inside getColumnClass(0)
Inside getColumnClass(1)
Inside getColumnClass(2)
Inside getColumnClass(3)
Inside getColumnClass(0)
Inside getColumnClass(1)
Inside getColumnClass(2)
Inside getColumnClass(3)
Inside getColumnClass(0)
Inside getColumnClass(1)
Inside getColumnClass(2)
Inside getColumnClass(3)
Inside getColumnClass(0)
Inside getColumnClass(1)
Inside getColumnClass(2)
Inside getColumnClass(3)

There are 4 columns and 3 rows and expected the getColumnClass to execute 12 times. Essentially executing 4 times (one for each column) and 3 times for each row.

There are actually 6 iterations and not 3. Hmmm, so maybe it executes once for the column headers, but that still leaves 2 extra iterations. Maybe it also executes for the blank row at the end, but then that still leaves 1 extra.

Why are there more iterations than expected?

Unhandled Exception
  • 1,427
  • 14
  • 30
  • 2
    Any time a cell is rendered this method is called, and the cell may be rendered several times on creation. This is not unexpected behavior. – Hovercraft Full Of Eels Aug 06 '16 at 14:45
  • Hmmm, So maybe its gets call once for each row then again for each row for rendering. Ran a few quick tests varying the row count and assuming 2 calls per row fits there were always two executes no matter the row count. Thanks for the info. – Unhandled Exception Aug 06 '16 at 15:50
  • Happily, the method is only called for _visible_ cells. Try this [example](http://stackoverflow.com/a/13919878/230513) having thousands of rows. – trashgod Aug 07 '16 at 05:45
  • I have a problem in this case!!! I have a JTable and a custom table model. I also overrided getColumnClass method. But it doesn't get the last column of my table? Do you know the reason for that? – chamzz.dot May 08 '18 at 08:38

0 Answers0