1

I have a JTable and I want a particular column to formatted as currency. If i put a number on that column like 3 it will show $3.00. i used TableCellRenderer but i'm not getting into it. Can anyone help ?

Thanks in advance.

3

3 Answers 3

2

Check out Table Format Renderers.

It shows how to use a renderer to do simple formatting by using the Format class. It even supports a convenience method for formatting currencies.

By the way in the last two questions that you posted and accepted an answer, there were better (simpler) answers that you could have used.

2

Here is a simple example. I don't know whether it works (if not - try to fix it).

import java.awt.Component;
import java.text.NumberFormat;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class CurrencyTableCellRenderer extends DefaultTableCellRenderer {

    private static final NumberFormat FORMAT = NumberFormat.getCurrencyInstance();

    @Override
    public final Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        final Component result = super.getTableCellRendererComponent(table, value,
                isSelected, hasFocus, row, column);
        if (value instanceof Number) {
            setHorizontalAlignment(JLabel.RIGHT);
            setText(FORMAT.format(value));
        } else {
            setText("");
        }
        return result;
    }
}

To add it:

table.getColumnModel().getColumn(column).setCellRenderer(new CurrencyTableCellRenderer());
4
  • i tried your code but it's give me errors like can not find symbol getTableCellRendererComponent for the line final Component result = super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column);
    – Arpan
    Commented May 15, 2014 at 11:45
  • I've added the import statements. Now it must compile. Commented May 15, 2014 at 12:20
  • well it did compile !! but after tabbing out the cell the data is just vanished.
    – Arpan
    Commented May 15, 2014 at 13:39
  • 1
    Sorry, but nobody can find all your bugs without to have the code. So you should do it yourself. Commented May 15, 2014 at 13:44
2
import javax.swing.table.DefaultTableCellRenderer;
import java.text.NumberFormat;

/**
 * Created by Ayettey on 06/02/2017.
*/
public class  CurrencyRenderer extends DefaultTableCellRenderer {

CurrencyRenderer(){
    super();

}

public void setValue(Object value){

    Number number=(Number)value;
      if((value!=null)&&(value instanceof Number)){
          NumberFormat numberFormat=NumberFormat.getCurrencyInstance();
          value=numberFormat.format(number.doubleValue());
      }



    super.setValue(value);

}

}
2
  • TableColumnModel model=table.getColumnModel(); TableColumn tableColumn=model.getColumn(t.ACCOUNT_BALANCE); tableColumn.setCellRenderer(new CurrencyRenderer()); Commented Feb 6, 2017 at 11:15
  • First of all, could you please explain how and why this solves the OPs question? And please note, that there is an edit function for answers, questions, and the like. If you want to extend your answer, please edit it and do not mis-use the comments as an edit. Commented Feb 6, 2017 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.