JTable method calling issue

I had an interesting observation with my JTable implmentation: The "getColumnCount" method was called many times (likely to be equal to the number of columns) when a JTable is rendered. Similarly, the "getRowCount" method was called many times as well. Interestingly, when I scroll the table, only "getRowCount" was called together with "getValueAt". "getColumnCount" was no longer called after the initiation of JTable. I am not sure whether this is normal behavior of JTable and would appreciate any comment and help.

Here is a sample of the code ( did not paste in all the code but hopefully it is enough for you to help me :-) ).

Thanks a lot!

TableModel dataTableModel = new AbstractTableModel() {

public int getColumnCount() {

int cCount = 20;

System.out.println("column count: "+ cCount);

return cCount;

}

public int getRowCount() {

int rCount = 20;

System.out.println("row count: "+ rCount);

return rCount;

}

public Object getValueAt(int row, int col) {

if (col==0){

cellValue="A";

}

else {

cellValue="B";

System.out.println("cell values at row "+row+" and column "+col+" is: "+cellValue);

}

return cellValue;

}

}

JTableHeader jTableHeader1;

jTable1 = new JTable(dataTableModel) {

public Dimension getPreferredScrollableViewportSize() {

return new Dimension(20 * getModel().getColumnCount(), 20 * getModel().getRowCount());

}

public boolean getScrollableTracksViewportWidth() {

return false;

}

};

[1614 byte] By [bronze-starDukes] at [2007-11-26 12:14:26]
# 1

1) Swing related questions should be posted in the Swing forum.

2) Use the "Code Formatting Tags" when posting code so the code is readable

> I am not sure whether this is normal behavior of JTable and would appreciate any comment and help.

I never worry about little details like this. You will drive yourself crazy if you try to understand every single little implementation detail. But I would say its normal

I would be more concerned about the using the table efficiently. For example why are you extending AbstractTableModel. Use the DefaultTableModel. It has already implemented all the methods.

platinumsta at 2007-7-7 14:16:58 > top of Java-index,Archived Forums,Socket Programming...
# 2

Thanks, camickr, for your quick comment. Next time I will post my issue to the right forum.

The reason for me to worry about it is that I have a table with half a million columns. I have yet to be able to visualize that table because of super long wait time, which prompted me to look at the calling frequency of each method.

Because I have to do custom implementation of those getColumnCount, getRowCount, getValueAt methods (draw data from a passed data object), I thought it is better to use AbstractTableModel.

Thanks again.

bronzestar at 2007-7-7 14:16:58 > top of Java-index,Archived Forums,Socket Programming...