在JTable里有一列整型数据,比如内容是1234,我想正常显示的时候是1,234,编辑的时候变为1234。
这个效果可以把列内容的类型设为Double类型来实现。但是如果设成Double型的话,编辑的时候就会变成1234.0。我不想要后面的小数点和0,不知有什么办法可以做到? :D
可以通过Render来定制显示的格式吧。
[code="java"]public class MyIntRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if(value != null && value instanceof Integer) {
DecimalFormat df = new DecimalFormat("#,##0");
this.setHorizontalAlignment(RIGHT);
this.setValue(df.format(value));
}
return c;
}
}[/code]
然后设置一下你的Integer类型的默认Render就可以了:
[code="java"]table.setDefaultRenderer(Integer.class, new MyIntRenderer()); [/code]
这样看看是否符合你的需求?