walters
walters

Reputation: 1467

JTable column headers localization

I have a subclass of javax.swing.table.AbstractTableModel that defines table column headers like this: protected String[] columnNames = new String[] { "Column1", "Column2};. How do I localize the columnNames from a resource bundle? I want to read the column headers from a .properties file instead on hard-coding them in my code. Is there a better way of doing this?

Upvotes: 0

Views: 918

Answers (2)

locka
locka

Reputation: 6029

Easiest way is to modify your assignment of columnNames:

protected String[] columnNames = getColumnNames();
//...
private static String[] getColumnNames() {
  return ResourceBundle.getBundle("AppResources").getString("headings").split(",");
}

Where AppResources (or AppResources_en, AppResources_fr_FR etc.) is a class that extends ResourceBundle and contains a key called "headings" which returns a comma separated list of headings.

Upvotes: 0

Colin Hebert
Colin Hebert

Reputation: 93187

You override the getColumnName() in order to return the localized value of the column name.

For example :

private ResourceBundle res = ResourceBundle.getBundle("MyResource");

@Override
public String getColumnName( int column ) {
    return res.getString(columnNames[column]);
}

Upvotes: 3

Related Questions