Fix incorrectly-displayed column names when creating a custom JTableModel that extends AbstractTableModel.

When we write a custom JTable that extends AbstractTableModel, its column names may not be displayed correctly if we forget to override a particular method. By the “incorrect display”, I mean that instead of showing names of the columns specified in the custom class, the column names were displayed as “A”, “B”, “C”, … 

To fix this problem, we need to implement getColumnName() in our custom TableModel. This method is not required for extending AbstractTableModel. So, some of us may not do it.

For example, if we define column names of a table as

String[] columnNames={"name1", "name2"};

Then, we can override the getColumnName(int columnIndex) as


public String getColumnName(int columnIndex){
return columnNames[columnIndex];
}

Well, do not get confused between:
getColumnNames() and getColumnName(int columnIndex).

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s