I've tried many things to display a comma instead of a point in my JTable in "German"-style. In my JTable-Class I wrote a methode for looping a passed ResultSet for filling a DefaultTableModel and returning the Model or the JTable directly.
If I cast a BigDecimal to a String for replacing a point with a comma, the Result will be displayed correctly in my JTable - but then my Sorting-Sequence is not correct because the values will be sorted as a String instead of a BigDecimal...
The other Solution would be to sort directly via SQL-Statement and to dynamically change the sorting-sequence when I click on the row-header - but this is for me not the best solution because the contents of my Table are generated by executing a lot of Queries which take a lot of time.
I tried to set the locale of my JTable but nothing really worked.
Anybody an idea, how to display in german format?
Thanks a lot!
Here is my Class:
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class MyJTable extends JTable {
private static final long serialVersionUID = 1L;
public MyJTable(){
this.setLocale(Locale.GERMANY);
}
public MyJTable(ResultSet rs) throws SQLException{
DefaultTableModel model = this.getTableModel(rs);
this.setModel(model);
RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
this.setRowSorter(sorter);
this.setLocale(Locale.GERMANY);
}
public DefaultTableModel getTableModel(ResultSet rs) throws SQLException{
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
String bigdecimal=null;
Object obigdecimal;
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
if (metaData.getColumnClassName(columnIndex).equals("java.math.BigDecimal")){
bigdecimal=rs.getString(columnIndex);
if (bigdecimal!=null){
bigdecimal=bigdecimal.replace(".",",");
obigdecimal=bigdecimal;
vector.add(obigdecimal);
}
}
else
{
vector.add(rs.getObject(columnIndex));
}
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames){
private static final long serialVersionUID = 1L;
public Class getColumnClass(int column) {
Class returnValue=Object.class;
try{
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
}
catch(Exception e){}
return returnValue;
}
};
}
}