Jtable with monetary value in a single column

1

Looking to implement monetary value formatting in a jtable, I've tried using the Rafael Chaves as a base and I was able to do the code below.

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;

public class PrincipalTabelaValor extends JFrame {

    private final JTable table;
    private final int id1 = 1;
    private final int id2 = 2;
    private final int id3 = 3;
    private final double qtd1 = 10.10;
    private final double qtd2 = 9.20;
    private final double qtd3 = 8.30;
    private final double vl1 = 5.50;
    private final double vl2 = 6.51;
    private final double vl3 = 7.52;

    public PrincipalTabelaValor() {
        super("Tabela de Cadastro de Produtos");

        // constructs the table
        String[] columnNames = new String[]{"ID", "Descrição", "quantidade", "Preço"};
        Object[][] rowData = new Object[][]{
            {id1, "TOMATE", qtd1, vl1},
            {id2, "BANANA", qtd2, vl2},
            {id3, "UVA", qtd3, vl3}
        };

        table = new JTable(rowData, columnNames);
        table.setDefaultRenderer(Object.class, new CONTabelaProduto());

        add(new JScrollPane(table));

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(640, 150);
        setLocationRelativeTo(null);
    }

//  CONTROLA A TABELA
    public class CONTabelaProduto extends DefaultTableCellRenderer {

        private final NumberFormat FORMAT = NumberFormat.getCurrencyInstance();

        @Override
        public Component getTableCellRendererComponent(JTable jtable, Object conteudo, boolean linhaSelecionada, boolean hasFocus, int linha, int coluna) {
            super.getTableCellRendererComponent(jtable, conteudo, linhaSelecionada, hasFocus, linha, coluna);

            if (linha % 2 == 0) {
                setBackground(new Color(102, 102, 255, 80));
            } else {
                setBackground(new Color(102, 102, 255, 20));
            }

            if (linhaSelecionada) {
                setBackground(new Color(0, 0, 102, 100));
            }

            if (conteudo instanceof Double) {
                setText(FORMAT.format(conteudo));
            }

            jtable.setRowHeight(25);
            jtable.setFont(new Font("Verdana", 0, 14));
            jtable.setOpaque(false);
            return this;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PrincipalTabelaValor().setVisible(true);
            }
        });
    }
}

Asyoucansee,theformattingwasalsoassignedinthe"quantity" column, and I would like to assign this formatting only in the "price" column.

I tried to follow the example of Rob Camick creating the class below.

import java.text.NumberFormat;
import javax.swing.SwingConstants;

public class NumberRenderer extends FormatRenderer {

    public NumberRenderer(NumberFormat formatter) {
        super(formatter);
        setHorizontalAlignment(SwingConstants.RIGHT);
    }

    public static NumberRenderer getCurrencyRenderer() {
        return new NumberRenderer(NumberFormat.getCurrencyInstance());
    }
}

And calling the class with

jtable.getColumnModel().getColumn(3).setCellRenderer(NumberRenderer.getCurrencyRenderer());

But it ends up overwriting the defined background.

I would like to check if there is a way to assign this type of formatting without overwriting the background definition.

    
asked by anonymous 13.06.2018 / 04:58

1 answer

1

The simplest solution in this code is to get the index of the DefaultTableCellRenderer column instead of the column type, and when the index is the same, apply the formatting:

if (coluna == 3) {
    setText(FORMAT.format(conteudo));
}

or if the price column is always the last, regardless of the number of columns:

if (coluna == (jtable.getColumnCount()-1)) {
    setText(FORMAT.format(conteudo));
}
    
13.06.2018 / 05:13