Example usage for javax.swing.text DefaultFormatter setCommitsOnValidEdit

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

Introduction

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

Prototype

public void setCommitsOnValidEdit(boolean commit) 

Source Link

Document

Sets when edits are published back to the JFormattedTextField.

Usage

From source file:com.jgoodies.validation.tutorial.formatted.NumberExample.java

/**
 * Appends the demo rows to the given builder and returns the List of
 * formatted text fields.//from ww w.  ja  v  a2 s.c o m
 * 
 * @param builder  the builder used to add components to
 * @return the List of formatted text fields
 */
private List appendDemoRows(DefaultFormBuilder builder) {
    // The Formatter is choosen by the initial value.
    JFormattedTextField defaultNumberField = new JFormattedTextField(new Long(42));

    // The Formatter is choosen by the given Format.
    JFormattedTextField noInitialValueField = new JFormattedTextField(NumberFormat.getIntegerInstance());

    // Uses a custom NumberFormat.
    NumberFormat customFormat = NumberFormat.getIntegerInstance();
    customFormat.setMinimumIntegerDigits(3);
    JFormattedTextField customFormatField = new JFormattedTextField(new NumberFormatter(customFormat));

    // Uses a custom NumberFormatter that prints natural language strings.
    JFormattedTextField customFormatterField = new JFormattedTextField(new CustomNumberFormatter());

    // Uses a custom FormatterFactory that used different formatters
    // for the display and while editing.
    DefaultFormatterFactory formatterFactory = new DefaultFormatterFactory(new NumberFormatter(),
            new CustomNumberFormatter());
    JFormattedTextField formatterFactoryField = new JFormattedTextField(formatterFactory);

    // Wraps a NumberFormatter to map empty strings to null and vice versa.
    JFormattedTextField numberOrNullField = new JFormattedTextField(new EmptyNumberFormatter());

    // Wraps a NumberFormatter to map empty strings to -1 and vice versa.
    Integer emptyValue = new Integer(-1);
    JFormattedTextField numberOrEmptyValueField = new JFormattedTextField(new EmptyNumberFormatter(emptyValue));
    numberOrEmptyValueField.setValue(emptyValue);

    // Commits values on valid edit texts.
    DefaultFormatter formatter = new NumberFormatter();
    formatter.setCommitsOnValidEdit(true);
    JFormattedTextField commitOnValidEditField = new JFormattedTextField(formatter);

    // Returns number values of type Integer
    NumberFormatter numberFormatter = new NumberFormatter();
    numberFormatter.setValueClass(Integer.class);
    JFormattedTextField integerField = new JFormattedTextField(numberFormatter);

    Format displayFormat = new DisplayFormat(NumberFormat.getIntegerInstance());
    Format typedDisplayFormat = new DisplayFormat(NumberFormat.getIntegerInstance(), true);
    List fields = new LinkedList();
    fields.add(Utils.appendRow(builder, "Default", defaultNumberField, typedDisplayFormat));
    fields.add(Utils.appendRow(builder, "No initial value", noInitialValueField, displayFormat));
    fields.add(Utils.appendRow(builder, "Empty <-> null", numberOrNullField, displayFormat));
    fields.add(Utils.appendRow(builder, "Empty <->   -1", numberOrEmptyValueField, displayFormat));
    fields.add(Utils.appendRow(builder, "Custom format", customFormatField, displayFormat));
    fields.add(Utils.appendRow(builder, "Custom formatter", customFormatterField, displayFormat));
    fields.add(Utils.appendRow(builder, "Formatter factory", formatterFactoryField, displayFormat));
    fields.add(Utils.appendRow(builder, "Commits on valid edit", commitOnValidEditField, displayFormat));
    fields.add(Utils.appendRow(builder, "Integer Result", integerField, typedDisplayFormat));

    return fields;
}

From source file:com.jgoodies.validation.tutorial.formatted.DateExample.java

/**
 * Appends the demo rows to the given builder and returns the List of
 * formatted text fields.//  w w  w  . j  a v a2s.co  m
 * 
 * @param builder  the builder used to add components to
 * @return the List of formatted text fields
 */
private List appendDemoRows(DefaultFormBuilder builder) {
    // The Formatter is choosen by the initial value.
    JFormattedTextField defaultDateField = new JFormattedTextField(new Date());

    // The Formatter is choosen by the given Format.
    JFormattedTextField noInitialValueField = new JFormattedTextField(DateFormat.getDateInstance());

    // Uses a custom DateFormat.
    DateFormat customFormat = DateFormat.getDateInstance(DateFormat.SHORT);
    JFormattedTextField customFormatField = new JFormattedTextField(new DateFormatter(customFormat));

    // Uses a RelativeDateFormat.
    DateFormat relativeFormat = new RelativeDateFormat();
    JFormattedTextField relativeFormatField = new JFormattedTextField(new DateFormatter(relativeFormat));

    // Uses a custom DateFormatter that allows relative input and
    // prints natural language strings.
    JFormattedTextField relativeFormatterField = new JFormattedTextField(new RelativeDateFormatter());

    // Uses a custom FormatterFactory that used different formatters
    // for the display and while editing.
    DefaultFormatterFactory formatterFactory = new DefaultFormatterFactory(
            new RelativeDateFormatter(false, true), new RelativeDateFormatter(true, true));
    JFormattedTextField relativeFactoryField = new JFormattedTextField(formatterFactory);

    // Wraps a DateFormatter to map empty strings to null and vice versa.
    JFormattedTextField numberOrNullField = new JFormattedTextField(new EmptyDateFormatter());

    // Wraps a DateFormatter to map empty strings to -1 and vice versa.
    Date epoch = new Date(0); // January 1, 1970
    JFormattedTextField numberOrEmptyValueField = new JFormattedTextField(new EmptyDateFormatter(epoch));
    numberOrEmptyValueField.setValue(epoch);

    // Commits value on valid edit text
    DefaultFormatter formatter = new RelativeDateFormatter();
    formatter.setCommitsOnValidEdit(true);
    JFormattedTextField commitOnValidEditField = new JFormattedTextField(formatter);

    // A date field as created by the BasicComponentFactory:
    // Uses relative date input, and maps empty strings to null.
    ValueModel dateHolder = new ValueHolder();
    JFormattedTextField componentFactoryField = ExampleComponentFactory.createDateField(dateHolder);

    Format displayFormat = new DisplayFormat(DateFormat.getDateInstance());
    List fields = new LinkedList();
    fields.add(Utils.appendRow(builder, "Default", defaultDateField, displayFormat));
    fields.add(Utils.appendRow(builder, "No initial value", noInitialValueField, displayFormat));
    fields.add(Utils.appendRow(builder, "Empty <->  null", numberOrNullField, displayFormat));
    fields.add(Utils.appendRow(builder, "Empty <-> epoch", numberOrEmptyValueField, displayFormat));
    fields.add(Utils.appendRow(builder, "Short format", customFormatField, displayFormat));
    fields.add(Utils.appendRow(builder, "Relative format", relativeFormatField, displayFormat));
    fields.add(Utils.appendRow(builder, "Relative formatter", relativeFormatterField, displayFormat));
    fields.add(Utils.appendRow(builder, "Relative factory", relativeFactoryField, displayFormat));
    fields.add(Utils.appendRow(builder, "Commits on valid edit", commitOnValidEditField, displayFormat));
    fields.add(Utils.appendRow(builder, "Relative, maps null", componentFactoryField, displayFormat));

    return fields;
}