Jtable currency format

0

I'm making a coin-shaped CellRender. I put 13 columns in a jtable, one has the recipes (Salary, 13th salary, Extra hour) .. and outas 12 with the months. every month the person will type this information and then I will add another jtable with the expenses. does anyone have an example of CellRender for this need?

IfoundthisexamplethatservesmewellbutI'mnotabletouseitforajframeandadefaulttablemodethatpullinformationfromthedatabase.

importjava.awt.*;importjava.text.NumberFormat;importjavax.swing.*;importjavax.swing.table.*;publicclassEditorTest{privateJScrollPanegetTableComponent(){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;
    }
}
    
asked by anonymous 13.09.2017 / 16:59

1 answer

3

The example below taken from SOEn might 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 must also set the data type in this column in TableModel . If you are using floating point, just return the equivalent type:

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

See working correctly:

TheNumberFormat.getCurrencyInstance()methodreturnstheformattingofthelocalcurrencyenteredbythesystemandappliestothefieldwheneveranumericvalueispassedtothecolumn.

Ifyouwanttorestricttheformattingtoaparticularcurrencyregardlessofthesystemlocationoftheappinstaller,youneedtoenterthelocationthroughanothermethodwiththesamename:

privatestaticfinalNumberFormatbrazilianFormat=NumberFormat.getCurrencyInstance(newLocale("pt", "BR"));

As above, always apply the format of the Brazilian currency. See a demo on IDEONE , whose format is the US currency.

    
13.09.2017 / 17:28