JTable - DefaultTableCellRenderer - getCurrencyInstance()?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (62)

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

这些是我的数据:

我想要这样的数据出来:

有了这个代码,我试着:

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());
提问于
用户回答回答于

解决办法:

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());
用户回答回答于

你需要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用于概念验证:

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

所属标签

可能回答问题的人

  • 最爱开车啦

    8 粉丝503 提问6 回答
  • 优惠活动秘书

    0 粉丝2 提问6 回答
  • uncle_light

    5 粉丝518 提问5 回答
  • Richel

    6 粉丝0 提问5 回答

扫码关注云+社区

领取腾讯云代金券