Mega Code Archive

 
Categories / Java / Swing JFC
 

A BigDecimal object custom formatter

import java.math.BigDecimal; import java.text.DecimalFormat; import javax.swing.JFormattedTextField; import javax.swing.text.DefaultFormatter; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.NumberFormatter; public class Main {   public static void main(String[] argv) {     JFormattedTextField f = new JFormattedTextField(new BigDecimal("123.4567"));     DefaultFormatter fmt = new NumberFormatter(new DecimalFormat("#.0###############"));     fmt.setValueClass(f.getValue().getClass());     DefaultFormatterFactory fmtFactory = new DefaultFormatterFactory(fmt, fmt, fmt);     f.setFormatterFactory(fmtFactory);     BigDecimal bigValue = (BigDecimal) f.getValue();   } }