2

我有一个 Jtable(tableSummary)。我需要格式化表格的 2 列,使其内容为十进制形式(例如 1,400.00) 我该怎么做?

这是我的表格代码:

private void tableMarketMouseClicked(java.awt.event.MouseEvent evt)    {                                         

  String sql = "SELECT tblClientInfo.ClientID, tblrefmarket.MarketDesc, tblclientinfo.LastName, tblledger.LoanAmount, "
        + "tblledger.DateStarted, tblledger.DailyPay, tblledger.Expiry FROM tblclientinfo Inner Join tblbusinessinfo ON tblbusinessinfo.ClientID = tblclientinfo.ClientID "
        + "Inner Join tblrefmarket ON tblbusinessinfo.MarketID = tblrefmarket.MarketID "
        + "Inner Join tblledger ON tblledger.ClientID = tblclientinfo.ClientID where MarketDesc = ?";

   try {
        //add column to the table model
         model.setColumnCount(0); //sets the column to 0 para ig utro click, dili mapun-an ang columns
         model.setRowCount(0); //sets the row to 0 para ig utro click, dili mapun-an ang rows
         model.addColumn("C NO");
         model.addColumn("MARKET"); 
         model.addColumn("BORROWER");
         model.addColumn("LOAN");
         model.addColumn("START");
         model.addColumn("DAILY");
         model.addColumn("EXPIRY");
         //model.addColumn("BALANCE");

        int row = tableMarket.getSelectedRow();     
        pst = conn.prepareStatement(sql);  
        pst.setString(1, tableMarket.getModel().getValueAt(row, 0).toString());
        rs = pst.executeQuery();

        while(rs.next()){            
            String id = rs.getString(1);
            String market = rs.getString(2);  
            String name = rs.getString(3);
            String amt = rs.getString(4); 
            String start = rs.getString(5);
            String daily = rs.getString(6); 
            String expiry = rs.getString(7);
            //String area = rs.getString(3); 
            model.addRow(new Object[]{ id, market, name, amt, start, daily, expiry});         
        }     

        tableSummary.setModel(model);
        renderer.setHorizontalAlignment( JLabel.RIGHT );
        renderer2.setHorizontalAlignment( JLabel.CENTER );
        tableSummary.getColumnModel().getColumn(0).setCellRenderer( renderer2 );
        tableSummary.getColumnModel().getColumn(4).setCellRenderer( renderer2 );
        tableSummary.getColumnModel().getColumn(6).setCellRenderer( renderer2 );
        tableSummary.getColumnModel().getColumn(3).setCellRenderer( renderer ); 
        tableSummary.getColumnModel().getColumn(5).setCellRenderer( renderer );


      } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e);
      }
  } 

列 amt 和 daily 是我需要格式化的列。

提前致谢!

4

2 回答 2

4

正如 kleopatra 在她的评论中已经建议的那样

  • 从 Object 到 String 表示(或任何其他表示)的转换是渲染器的任务。你TableModel应该只包含对象
  • 在您的设备上创建并设置适当的渲染器JTable(例如通过调用JTable#setDefaultRenderer或覆盖JTable#getCellRenderer

作为您的Number实例的渲染器,您可以使用一个使用NumberFormatfor 格式的渲染器,如 Samir 的答案所示

于 2012-03-15T14:18:04.473 回答
2
NumberFormat formatter = new DecimalFormat("#,###.00");
String  str = formatter.format(1400);
System.out.println(str);
于 2012-03-15T09:27:28.837 回答