JTable - DefaultTableCellRenderer - getCurrencyInstance()

时间:2018-06-03 17:19:22

标签: java swing sqlite jtable currency-formatting

我想将第2列(Columna 2)的格式更改为所有数据的货币格式,我正在尝试使用它,但它没有做任何事情。 你能帮帮我吗?

这些是我的数据:

Image with data to change

我想要数据如下:

image that I want:

使用此代码我正在尝试:

public class CurrencyCellRenderer extends DefaultTableCellRenderer {

    @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.CENTER);
            setText(defaultFormat.format(value));
        } else {

            setText("NO");
        }
        return result;
    }
}

jTable1.getColumnModel().getColumn(2).setCellRenderer(new CurrencyCellRenderer());

它没有做任何事,你能帮助我吗? 感谢!!!

2 个答案:

答案 0 :(得分:1)

您需要在更改值的值后创建result 。之前创建结果,只会返回相同的旧数据。

如,

private class CurrencyCellRenderer extends DefaultTableCellRenderer {

    @Override
    public final Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (value == null) {
            value = 0.0;
        } 
        value = myFormat.format(value);

        final Component result = super.getTableCellRendererComponent(table, value,
                isSelected, hasFocus, row, column);

        return result;
    }
}

我的MCVE用于概念验证:

enter image description here

import java.awt.BorderLayout;
import java.awt.Component;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class CurrencyTable extends JPanel {
    private static final String[] TABLE_HEADERS = { "Text", "Value" };
    private static final Object[][] DATA = { { "Foo", 1.1 }, { "Bar", 2.2 }, { "Bax", 3.3 } };
    private DefaultTableModel tableModel = new DefaultTableModel(DATA, TABLE_HEADERS);
    private JTable table = new JTable(tableModel);
    private NumberFormat myFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);

    public CurrencyTable() {
        table.getColumnModel().getColumn(1).setCellRenderer(new CurrencyCellRenderer());

        setLayout(new BorderLayout());
        add(new JScrollPane(table));
    }

    private class CurrencyCellRenderer extends DefaultTableCellRenderer {

        @Override
        public final Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            if (value == null) {
                value = 0.0;
            }
            value = myFormat.format(value);

            final Component result = super.getTableCellRendererComponent(table, value, isSelected,
                    hasFocus, row, column);

            return result;
        }
    }

    private static void createAndShowGui() {
        CurrencyTable mainPanel = new CurrencyTable();

        JFrame frame = new JFrame("CurrencyTable");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

答案 1 :(得分:0)

好的,我有解决方案:

NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();

public class MyRenderer extends JLabel implements TableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus, int row,
            int col) {
        DefaultTableCellRenderer renderer
                = new DefaultTableCellRenderer();
        Component c = renderer.getTableCellRendererComponent(table,
                value, isSelected, hasFocus, row, col);
        String s = "";
        if (col == 2) {
            double d = Double.parseDouble( String.valueOf(value)) ;
            s = defaultFormat.format(d);
            c = renderer.getTableCellRendererComponent(table, s,
                    isSelected, hasFocus, row, col);
            ((JLabel) c).setHorizontalAlignment(SwingConstants.RIGHT);
        }
        return c;
    }
}

jTable1.getColumnModel().getColumn(2).setCellRenderer(new MyRenderer());

Image here

感谢所有人!