0

I want that the number in jtable cell will be 9.12 but I got 9.1200.

This code has not changed any thing and the double value appears 9.1200. What can I do ?

The table in the database where the variables are stored is a currency type.

  try (Connection con = DriverManager.getConnection(jd+acc)) {
                    Statement stm=con.createStatement();
                    ResultSet rs=stm.executeQuery("select * from product ");
                    while(rs.next()){
                        rows=rs.getRow();
                        cell=new String[rows][10];
                    }
                }


            try (Connection con = DriverManager.getConnection(jd+acc)) {
                Statement stm=con.createStatement();
                ResultSet rs=stm.executeQuery("select * from product ");
                while(rs.next()){
                    rows=rs.getRow();
                    cell=new String[rows][10];
                }
            }



catch(SQLException ex){} 


            try (Connection con = DriverManager.getConnection(jd+acc)) {
                Statement stm=con.createStatement();
                ResultSet rs=stm.executeQuery("select * from product ");
                while(rs.next()){
                    for(colum=0;colum<10;colum++){
                        cell[row][colum]="   "+rs.getString(colum+1)+"   " ;
                    }
                    row++ ;
                }
            }


       catch(SQLException ex){}


DefaultTableModel table1=new DefaultTableModel(cell,header);

          table=new JTable(table1);

           table.getColumnModel().getColumn(7).setCellRenderer(
     new DecimalFormatRenderer() );

          JScrollPane scrol=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
          table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.setRowSelectionAllowed(false);
          table.setBackground(Color.WHITE);

add(scrol);

// and this class to handle the formatting of the double values

 public static class DecimalFormatRenderer extends DefaultTableCellRenderer {
    private static final long serialVersionUID = 1L;
    private static final DecimalFormat formatter = new DecimalFormat( "#.##" );
public DecimalFormatRenderer(){
    super();
    formatter.setMinimumFractionDigits(2);
}

@Override
public Component getTableCellRendererComponent(
    JTable table, Object value, boolean isSelected, boolean hasFocus, int r, int column) {

    if(value instanceof Double){

       value = formatter.format((Number)value);
    }

    return super.getTableCellRendererComponent(
        table, value, isSelected, hasFocus, r, column );
} 
} 
idan gal
  • 79
  • 5
mahmoud
  • 1
  • 1

0 Answers0