JTable cell editor number format

前端 未结 4 918
耶瑟儿~
耶瑟儿~ 2020-12-06 07:31

I need to show numbers in jTable with exact 2 decimal places. To accomplish this I have created a custom cell editor as:

public class NumberCellEditor extend         


        
4条回答
  •  时光说笑
    2020-12-06 08:01

    Use the locale to your advantage:

      //Locale myLocale = Locale.GERMANY;  //... or better, the current Locale
    
      Locale myLocale = Locale.getDefault(); // better still
    
      NumberFormat numberFormatB = NumberFormat.getInstance(myLocale);
      numberFormatB.setMaximumFractionDigits(2);
      numberFormatB.setMinimumFractionDigits(2);
      numberFormatB.setMinimumIntegerDigits(1);
    
      edit.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                        new NumberFormatter(numberFormatB)));
    

提交回复
热议问题