Jtable with monetary value in a single column

Asked

Viewed 122 times

0

In order to implement a monetary value formatting in a jtable, I tried to use the Rafael Chaves as a base and I managed to make 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);
            }
        });
    }
}

Jtable

As you can see, the formatting was also assigned in the "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 ends up overwriting the set background.

jtable

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

  • Your table only has 4 columns?

  • The original table has many more columns, I created this code just to demonstrate the dilemma I was going through.

  • @Carlosheuberger agree, this passage would not need to be there, could be defined directly in the main class, along with the table itself.

  • I created that way because my DefaultTableCellRenderer stays in a separate class, but thanks for the tip.

1 answer

1


The simplest solution in this code is, within your DefaultTableCellRenderer take the index from the column preço instead of the column type, and when the Intel 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));
}

Browser other questions tagged

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