Jtable currency format

Asked

Viewed 563 times

0

I am making a Cellrender with currency format. I placed 13 columns in a jtable, one has the recipes (Salary, 13th salary, Overtime).. and other 12 with the months. each month the person will enter this information and then I will add another jtable with the expenses. someone has an example of Cellrender for that need?

inserir a descrição da imagem aqui

I found this example that suits me well, but I’m not able to use it for a jframe and a defaulttablemode that pulls information from the bank.

import java.awt.*;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.table.*;

public class EditorTest {

    private JScrollPane getTableComponent() {
        String[] colNames = {
            "Preço", "Número"
        };
        final Object[][] data = {
            {new Double(2), Double.valueOf(12.21)},
            {Double.valueOf(12.21), Double.valueOf(12.21)},
            {Double.valueOf(12.21), Double.valueOf(12.21)},
            {Double.valueOf(12.21), Double.valueOf(12.21)}
        };
        DefaultTableModel model = new DefaultTableModel(data, colNames) {
            public Class getColumnClass(int col) {
                return data[0][col].getClass();
            }
        };
        JTable table = new JTable(model);
        TableColumnModel colModel = table.getColumnModel();
        //colunas
        colModel.getColumn(0).setCellRenderer(new DoubleRenderer());
        colModel.getColumn(1).setCellRenderer(new DoubleRenderer());
        table.setCellSelectionEnabled(true);
        Dimension d = table.getPreferredSize();
        table.setPreferredScrollableViewportSize(d);
        return new JScrollPane(table);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new EditorTest().getTableComponent());
        f.pack();
        f.setLocation(100, 100);
        f.setVisible(true);
    }
}

class DoubleRenderer extends DefaultTableCellRenderer {

    NumberFormat numeroFormatado = NumberFormat.getCurrencyInstance();

    public DoubleRenderer() {
        setHorizontalAlignment(RIGHT);
    }

    public Component getTableCellRendererComponent(JTable table,
            Object value,
            boolean isSelected,
            boolean hasFocus,
            int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected,
                hasFocus, row, column);
        setText(numeroFormatado.format(((Double) value).doubleValue()));
        return this;
    }
}
  • See the recent issue in the reply.

1 answer

3


The example below taken from Soen can serve:

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;
    }
}

For correct display, you also need to set the data type in this column on TableModel. If using floating point, just return the equivalent type:

@Override
public Class<?> getColumnClass(int columnIndex) {
    return columnIndex == 0 ? Double.class : super.getColumnClass(columnIndex);
}

See working properly:

inserir a descrição da imagem aqui

The method NumberFormat.getCurrencyInstance() return the currency formatting of the location reported by the system and apply to the field whenever a numeric value is passed to the column.

If you want to restrict formatting to a specific currency regardless of the location of the system from which the application is executed, you need to inform the location using another homonymous method:

private static final NumberFormat brazilianFormat = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));

In the above form, you will always apply the format of the Brazilian currency. See a demonstration of this in IDEONE, the format of which is recovered from the US currency.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.