Number Format in Jtable
Asked Answered
D

2

2

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?

here's my code for the table:

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

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

Thanks in Advance!

Dine answered 15/3, 2012 at 9:22 Comment(2)
all your problems have the same home-brewn reason: do not add the string representation of the objects to the tableModel, instead add the objects themselves. Then you can configure the string rep as needed, f.i. with formats in custom renderersKrell
You should simplify your code to the core of the problem, and on the other hand try to provide a self running example, such that the people, trying to help, can easily test, if they reached their goal.Prosthodontist
G
4

As kleopatra already suggested in her comments

  • 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)

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

Guarino answered 15/3, 2012 at 14:18 Comment(0)
B
2
NumberFormat formatter = new DecimalFormat("#,###.00");
String  str = formatter.format(1400);
System.out.println(str);
Bytom answered 15/3, 2012 at 9:27 Comment(3)
thanks . but what i am formatting are the columns in the table. String amt = rs.getString(4); and String daily = rs.getString(6); before i insert this into the table, how can i format it first?Dine
Just apply this conversion before you call addRow().Could
@JakubZaverka better do it the other way round :-)Krell

© 2022 - 2024 — McMap. All rights reserved.