在单元格中渲染double值

时间:2014-03-13 14:14:10

标签: java swing jtable tablecellrenderer tablecelleditor

我使用自定义表模型(MyTableModel.java)创建了一个JTable,并使用了一些CellRenderer类来查看单元格的效果。我使用CustomTableCellRenderer类将当前单元格更改为绿色背景。它运作得很好。同时,我创建了一个RenderPrice类来显示十进制类型的单元格,如果我编辑了未更新的单元格,它会在我第一次执行时获取十进制值,它将显示{{ 1}}签名作为前缀。我想解决这个问题。请尽快给出一些解决方案。感谢

RenderPrice类代码

$

我的Tableclass(Accept.java)的前面代码

class RenderPrice extends DefaultTableCellRenderer {
    Object result; 
    RenderPrice() { 
        setHorizontalAlignment(SwingConstants.RIGHT);  
    }

    public void setValue(Object aValue) {
        result = aValue;
        if ((aValue != null) && (aValue instanceof Number)) {
            Number numberValue = (Number)aValue;
            NumberFormat formatter = NumberFormat.getCurrencyInstance();
            result = formatter.format(numberValue.doubleValue());
        } 
        super.setValue(result);
    }   
} 

2 个答案:

答案 0 :(得分:2)

您应该在此类渲染器中存储任何状态。你可能正在寻找像这样的东西

class RenderPrice extends DefaultTableCellRenderer
{
    @Override
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int column)
    {
        super.getTableCellRendererComponent(
            table, value, isSelected, hasFocus, row, column);

        if ((value != null) && (value instanceof Number))
        {
            Number numberValue = (Number)value;
            NumberFormat formatter = NumberFormat.getCurrencyInstance();
            String valueString = formatter.format(numberValue.doubleValue());
            setText(valueString);
        }

        return this;
    }
}

编辑:回应评论和示例

import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.DefaultCellEditor;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.text.NumberFormatter;

public class CurrencyCellEditorTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTable table = new JTable(
            new Object[][]{
                { 1.2, 3.4 },
                { 5.6, 7.8 }
            },
            new Object[] {
                "A", "B"
            });


        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setCellEditor(new PriceEditor());
        column0.setCellRenderer(new PriceRenderer());

        TableColumn column1 = table.getColumnModel().getColumn(1);
        column1.setCellEditor(new PriceEditor());
        column1.setCellRenderer(new PriceRenderer());

        f.getContentPane().add(table);
        f.setSize(400,400);
        f.setLocationRelativeTo(null);
        f.setVisible(true);

    }
}

class PriceRenderer extends DefaultTableCellRenderer
{
    PriceRenderer()
    {
        setHorizontalAlignment(SwingConstants.RIGHT);
    }

    @Override
    public void setValue(Object aValue)
    {
        Object result = aValue;
        if ((aValue != null) && (aValue instanceof Number))
        {
            Number numberValue = (Number) aValue;
            NumberFormat formatter = NumberFormat.getCurrencyInstance();
            result = formatter.format(numberValue.doubleValue());
        }
        super.setValue(result);
    }
}

class PriceEditor extends DefaultCellEditor
{
    public PriceEditor()
    {
        super(createComponent());
        delegate = new EditorDelegate()
        {
            @Override
            public void setValue(Object value)
            {
                Number numberValue = 0.0;
                if (value != null && value instanceof Number)
                {
                    numberValue = (Number)value;
                }
                getFormattedTextField().setValue(numberValue);
            }

            @Override
            public Object getCellEditorValue()
            {
                try
                {
                    NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
                    String text = getFormattedTextField().getText();
                    Number number = numberFormat.parse(text);
                    return number;
                }
                catch (ParseException e)
                {
                    e.printStackTrace();
                    return new Double(0.0);
                }
            }
        };
    }

    private static JTextField createComponent()
    {
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
        NumberFormatter numberFormatter = new NumberFormatter(numberFormat);
        JFormattedTextField formattedTextField = new JFormattedTextField(numberFormatter);
        formattedTextField.setHorizontalAlignment(SwingConstants.RIGHT);
        return formattedTextField;
    }

    private JFormattedTextField getFormattedTextField()
    {
        return (JFormattedTextField)getComponent();
    }

}

答案 1 :(得分:1)

查看Table Format Renderers,了解创建自定义渲染器的简便方法。您只需指定一个特定的Formatter。

在这种情况下,该类具有货币格式化的便捷方法。

TableColumnModel m = table.getColumnModel();
m.getColumn(???).setCellRenderer(NumberRenderer.getCurrencyRenderer());