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?
views:
18answers:
2
+2
A:
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]);
}
Colin Hebert
2010-09-20 15:22:56
A:
Easiest way is to modify your assignment of columnNames:
protected String []columnNames = getColumnNames();
//...
static private 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.
locka
2010-09-20 16:10:58