problem overriding a java method in groovy
I am working on a jTable a开发者_运维百科nd want to use setAutoCreateRowSorter(true) and sort it for a defaultTableModel. My code is a mixture of Java & Groovy given the circumstances (I prefer simple java as my programming skills are very basic but thats not an option...).
Table works and I can get sorting but on columns with integers and floats the sorting is not correct as items are seen as String. From the JavaDoc I understand that I need to override the getColumnClass. Now doing this in Java would be easy and something like:
public Class getColumnClass(int column) {
if (column == 2) { //2 is a column with integers
return Integer.class;
} else {return String.class;
}
}
When i write the above, "Unknown Type:Method_Def" at the beginning of "public Class getColumnClass..." shows up.
I don't know how to fix that and thought it must be related to inner class limitations of groovy 1.57 that I have to use, so thought I write it in groovy and I am confused with the syntax and how to do it correctly. I looked around and tried to replicate examples i found:
def s = [getColumnClass: {int column -> {if (column ==n) return Integer.class;}
else {return String.class;} } ] as Class
this does not work, and i am clearly making mistakes..
How can I fix the Java code to not get the "Unknown type Method_def" or fix it by converting that code to groovy code? Both will do and I will be grateful...
Try this:
def model = [getColumnClass:{col -> if(col==2) return Integer.class; else return String.class;}] as TableModel
You need to use as TableModel
because you are overriding a method of TableModel
.
Update: for DefaultTableModel:
JTable tableS = new JTable() ;
def model = [getColumnClass:{col -> if(col==2) return Integer.class; else return String.class;}] as DefaultTableModel ;
model.setRowCount(0);
tableS.setModel(model);
tableS.setAutoCreateRowSorter(true) ;
I'm not sure why you need to do it in Groovy. Java code is perfectly fine, in fact Groovy compiles to Java bytecode.
Did you try just using the Java code you show in your question?
精彩评论