Jtable中的数字格式 [英] Number Format in Jtable

查看:114
本文介绍了Jtable中的数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jtable(tableSummary). 我需要格式化表格的2列,以便其内容为DECIMAL格式(例如1,400.00) 我该怎么办?

I have a Jtable (tableSummary). I need to format 2 columns of the table so it's content is in DECIMAL form (e.g. 1,400.00) How can i do it?

这是我的表格代码:

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.

the columns, amt and daily are the columns i need to be formatted.

预先感谢!

推荐答案

正如kleopatra在她的评论中所建议的

As kleopatra already suggested in her comments

  • 从Object到String表示形式(或任何其他表示形式)的转换是渲染器的任务.您的TableModel应该只包含对象
  • JTable上创建并设置适当的渲染器(例如,通过调用
  • The conversion from Object to a String representation (or any other representation) is the task of the renderer. Your TableModel should just contain the objects
  • Create and set the appropriate renderer on your JTable (for example by calling JTable#setDefaultRenderer or overriding JTable#getCellRenderer)

作为Number实例的渲染器,您可以使用使用NumberFormat进行格式化的渲染器,如Samir的答案

As renderer for your Number instances you can use one which uses the NumberFormat for formatting as shown in the answer of Samir

这篇关于Jtable中的数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆