Saturday, 5 December 2009

JTable in JScrollPane showing default column names

Well, back to the basics of Java...

Just in case anybody (that's including me ;-)) gets this problem (again) here is the solution.

Basic scenario: building a JTable with a non default model class extending AbstractTableModel as follows. I have omitted necessary includes and actually removed method bodies for simplification purposes.

Spot the error...


public class SyncOperationTableModel extends AbstractTableModel
{
// attributes

public SyncOperationTableModel(ArrayList syncOperations)
{
super();
// initialize attributes
}

public int getRowCount()
{
// return the row count based on attributes (for instance)
}

public int getColumnCount()
{
// return column count base on attributes (for instance)
}

public String getColumName(int column)
{
// return column name
}

public Object getValueAt(int row, int column)
{
// return the value,
// by default setValueAt is not necessary
// (AbstractTableModel returns false to isCellEditable method)
}

}



Symptom? The column names (getColumName(int column)) never get returned and are replaced with default names: A, B, C... At first sight this is strange, since I purposefully add the table to a JScrollPane (which should handle the table header automatically).

Solution? In the above code the getColumnName(int column) does not get called simply because I have created a new method named getColumName (i.e. with a missing 'n' to the 'column' part of the name). Stu-pid!

New method:


@Override
public String getColumnName(int column)
{
// code
}


Reaaaaally hopeless... no wonder this was impossible to find despite GIYF. Now getting on to cell rendering - it's been a while... other silly mistakes ahead I am sure.

More news soon.

Thoughts in the meantime?

No comments:

Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory