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.

Arpan
  • 596
  • 2
  • 10
  • 29

3 Answers3

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());
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • 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 May 15 '14 at 11:45
  • I've added the import statements. Now it must compile. – Sergiy Medvynskyy May 15 '14 at 12:20
  • well it did compile !! but after tabbing out the cell the data is just vanished. – Arpan May 15 '14 at 13:39
  • 1
    Sorry, but nobody can find all your bugs without to have the code. So you should do it yourself. – Sergiy Medvynskyy May 15 '14 at 13:44
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.

camickr
  • 321,443
  • 19
  • 166
  • 288
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);

}

}
CroMagnon
  • 1,218
  • 7
  • 20
  • 32
  • TableColumnModel model=table.getColumnModel(); TableColumn tableColumn=model.getColumn(t.ACCOUNT_BALANCE); tableColumn.setCellRenderer(new CurrencyRenderer()); – Dan Ayettey Feb 06 '17 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. – Paul Kertscher Feb 06 '17 at 11:24