jtable数列格式12.345.678,98 for new Double(12345678.98)

时间:2014-03-05 05:08:10

标签: java jtable

如果列类型为Double,如何在JTable中将列值new Double(12345678.98)格式化为12.345.678,98?

DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator(',')
dfs.setGroupingSeparator('.');`
DecimalFormat dfCurrency = new DecimalFormat("#,###.##");
dfCurrency.setDecimalFormatSymbols(dfs);
System.out.println(dfCurrency.format(new Double(300909090.76)));


// print: 300.909.090,76

我需要在“PRICE”列中使用dfs格式符号。如何在JTable列(价格列)中使用DecimalFormatSymbols(dfs)?

这里是示例代码。

import java.awt.*;
import javax.swing.*;

class SimpleTableExample extends JFrame {
   private final JPanel topPanel;
   private final JTable table;
   private final JScrollPane scrollPane;

   public SimpleTableExample() {
      setTitle( "Simple Table Application" );
      setSize( 300, 200 );
      setBackground( Color.gray );
      setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);

      topPanel = new JPanel();
      topPanel.setLayout( new BorderLayout() );
      getContentPane().add( topPanel );

      table = new JTable();
      table.setModel(new javax.swing.table.DefaultTableModel(
          new Object [][] {{"IT01", "Shoes",  new Double(323233.87)}, {"IT02", "Hammer",  new Double(321233.87)}},
          new String [] { "ID", "ITEM", "PRICE" }
      ) {
          Class[] types = new Class [] {
              java.lang.String.class, java.lang.String.class, java.lang.Double.class
          };
          public Class getColumnClass(int columnIndex) {
              return types [columnIndex];
          }
      });

      scrollPane = new JScrollPane(table);
      topPanel.add(scrollPane, BorderLayout.CENTER);
   }

   public static void main( String args[] ) {
      SimpleTableExample mainFrame = new SimpleTableExample();
      mainFrame.setVisible( true );
   }
}

1 个答案:

答案 0 :(得分:0)

也许你可以使用我的解决方案。你可以使用cellrenderer

import java.awt.*;
import java.text.DecimalFormat;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.table.*;

public class DemoRenderer extends JFrame {

    public static void main( String[] args ) {
        DemoRenderer frame = new DemoRenderer();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
    }

    public DemoRenderer() {

                JTable table = new JTable();
                table.setModel(new MyTablemodel());
                table.setDefaultRenderer(Object.class, new MyCustomTableCellRenderer());

                // Tell the table what to use to render our column of doubles

                table.repaint();
                //table.getColumnModel().getColumn(1).setCellRenderer(new DecimalFormatRenderer() );                
        getContentPane().add(new JScrollPane(table));
    }

}

/**
         Here is our class to handle the formatting of the double values
         */

class MyCustomTableCellRenderer extends DefaultTableCellRenderer{

    private static final DecimalFormat formatter = new DecimalFormat( "#0.00" );

    public Component getTableCellRendererComponent (JTable table, 
             Object obj, boolean isSelected, boolean hasFocus, int row, int column) {

        if(column==1) obj = formatter.format((Number)obj);        
        Component cell = super.getTableCellRendererComponent(
            table, obj, isSelected, hasFocus, row, column);
    if (isSelected) {
    cell.setBackground(Color.green);
    } 
    else {
    if (row % 2 == 0) {
    cell.setBackground(Color.cyan);
    }
    else {
    cell.setBackground(Color.lightGray);
    }
    }    
    return cell; 
    }
}

class MyTablemodel extends AbstractTableModel{

    Object[] columnNames = { "A", "B", "C" };
    Object[][] data = {
    { "1abc", new Double(850.503), 53 },
    { "2def", new Double(36.23254), 6 },
    { "3ghi", new Double( 8.3 ), 7 },
    { "4jkl", new Double( 246.0943 ), 23 }};

    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }

    /*
    public Class getColumnClass(int columnIndex) {
        Object o = getValueAt(0, columnIndex);
        if (o == null) {
          return Object.class;
        } else {
          return o.getClass();
        }
      }
   */ 
}