setting tablecellrenderer

I have a table which contains 5 columns:

1-item no(string)

2-itemname(String)

3-rprice(double value)

4-rquantity(integer value)

5-ramount(double value)

To set the double value to be displayed as $00.00,i have a class to set the cellrenderer and append the renderer to the column

class MoneyRendererextends DefaultTableCellRenderer

{

private DecimalFormat formatter;

public MoneyRenderer(String format)

{

super();

formatter =new DecimalFormat(format);

}

publicvoid setValue(Object value)

{

if ((value !=null) && (valueinstanceof Number))

{

Number numberValue = (Number) value;

value = formatter.format(numberValue.doubleValue());

}

super.setValue(value);

}

}

//set the defaultrender

modeli.setDataVector(datas, columnNames);

repairItemTable.setModel(modeli);

TableCellRenderer renderer =new MoneyRenderer("$####0.00");

repairItemTable.setDefaultRenderer(modeli.getColumnClass(2), renderer);

repairItemTable.setDefaultRenderer(modeli.getColumnClass(4), renderer);

Though i have only set the renderer for column 2 and 4, now even column 3(rquantity) is displayed as "$95.00" format. Can anyone enligthen me on what to do? Thanks a lot

[1998 byte] By [Buma] at [2007-11-26 16:30:39]
# 1

Don't use setDefaultRenderer. This sets the renderer for all columns having values of type Class (so essentially your two lines of code do the same job, as both columns 2 and 4 contain the same data type). Instead write:

repairItemTable.getColumn(2).setCellRenderer(renderer);

repairItemTable.getColumn(4).setCellRenderer(renderer);

GJosefa at 2007-7-8 22:55:05 > top of Java-index,Desktop,Core GUI APIs...
# 2

thanks a lot GJosef !!!.. it works now

but i need to get the column name and append the name to the getcolumn ...

btw for me to set the number to align all text to right and set the column size to the text size, i'll still need to use this tablecellrender too right?

Thanks again GJosef !!!!

String column1 = repairItemTable.getColumnName(2);

String column2 = repairItemTable.getColumnName(4);

repairItemTable.getColumn(column1).setCellRenderer(renderer);

repairItemTable.getColumn(column2).setCellRenderer(renderer);

Buma at 2007-7-8 22:55:05 > top of Java-index,Desktop,Core GUI APIs...
# 3

1) First you need to override the getColumnClass(..) method to return the correct class. Then default renderers and editors will be used based on the class. If you don't override the method then the table thinks all columns only contains Object.class and will therefore treat each column like a string. Here is a simple example:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=627108

2) Now to assign a default renderer you would just use:

table.setDefaultRenderer(Double.class, currencyRenderer);

camickra at 2007-7-8 22:55:05 > top of Java-index,Desktop,Core GUI APIs...
# 4

thanks a lot GJosef !!!.. it works now

but i need to get the column name and append the name to the getcolumn ...

Sorry about missing that! I rushed...

btw for me to set the number to align all text to right and set the column size to the text size, i'll still need to use this tablecellrender too right?

I don't know about the calculation of the column width, but for the alignment, yes. Just add setHorizonalAlignment(JLabel.RIGHT); to your TableCellRenderer.

Thanks again GJosef !!!!

Glad it helped!

GJosefa at 2007-7-8 22:55:05 > top of Java-index,Desktop,Core GUI APIs...