Example usage for javax.swing.text DefaultFormatter setValueClass

List of usage examples for javax.swing.text DefaultFormatter setValueClass

Introduction

In this page you can find the example usage for javax.swing.text DefaultFormatter setValueClass.

Prototype

public void setValueClass(Class<?> valueClass) 

Source Link

Document

Sets that class that is used to create new Objects.

Usage

From source file:Main.java

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);//from w w w . j  av a  2 s .c om

    BigDecimal bigValue = (BigDecimal) f.getValue();

}

From source file:org.springframework.richclient.form.FormatterFactory.java

public AbstractFormatter getFormatter(JFormattedTextField source) {
    Object value = source.getValue();
    DefaultFormatter formatter;
    if (value instanceof Date) {
        formatter = new DateFormatter();
    } else if (value instanceof Number) {
        formatter = new NumberFormatter();
    } else {/*from   w w  w.j  av  a  2  s  . c o m*/
        formatter = new DefaultFormatter();
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Factory returning new formatter " + formatter + " for text field " + source);
    }
    valueCommitPolicy.configure(source, formatter);
    if (valueClass != null) {
        formatter.setValueClass(valueClass);
    } else if (value != null) {
        formatter.setValueClass(value.getClass());
    }
    return formatter;
}