Java JFormattedTextField get value and cast
import java.awt.BorderLayout; import java.math.BigDecimal; import java.text.DecimalFormat; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.text.DefaultFormatter; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.NumberFormatter; public class Main { public static void main(final String args[]) throws Exception { JFrame frame = new JFrame("Formatted Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JFormattedTextField input = new JFormattedTextField(new BigDecimal("123.4567")); DefaultFormatter fmt = new NumberFormatter(new DecimalFormat("#.0###############")); fmt.setValueClass(input.getValue().getClass()); DefaultFormatterFactory fmtFactory = new DefaultFormatterFactory(fmt, fmt, fmt); input.setFormatterFactory(fmtFactory); BigDecimal bigValue = (BigDecimal) input.getValue(); System.out.println(bigValue); frame.add(input, BorderLayout.NORTH); frame.add(new JTextField(), BorderLayout.SOUTH); frame.setSize(250, 100);/*from w ww . j av a 2 s .c om*/ frame.setVisible(true); } }