Example usage for javax.swing.text DefaultFormatterFactory DefaultFormatterFactory

List of usage examples for javax.swing.text DefaultFormatterFactory DefaultFormatterFactory

Introduction

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

Prototype

public DefaultFormatterFactory(JFormattedTextField.AbstractFormatter defaultFormat,
        JFormattedTextField.AbstractFormatter displayFormat) 

Source Link

Document

Creates a DefaultFormatterFactory with the specified JFormattedTextField.AbstractFormatters.

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   w  ww .  j  a  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  ww  . j  a  v  a2 s.  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;
}

From source file:org.angnysa.yaba.swing.BudgetFrame.java

private void buildSimulationPanel() {

    simulationPanel = new JPanel();
    SpringLayout springLayout = new SpringLayout();
    simulationPanel.setLayout(springLayout);

    // chart data
    simulationDataset = new SimulationDataset(service);
    simulationDataset.setInitial(0D);//from  w w w .j av  a 2 s  .  c  o  m
    simulationDataset.setStart(new LocalDate());
    simulationDataset.setEnd(new LocalDate().plus(Years.ONE));
    simulationDataset.setPeriod(Period.months(1));
    simulationDataset.updateDataset();
    transactionModel.addTableModelListener(new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent e) {
            simulationDataset.updateDataset();
        }
    });
    reconciliationModel.addTableModelListener(new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent e) {
            simulationDataset.updateDataset();
        }
    });

    // initial amount label
    JLabel amountLbl = new JLabel(Messages.getString("simulation.field.initial-amount")); //$NON-NLS-1$
    springLayout.putConstraint(SpringLayout.WEST, amountLbl, 10, SpringLayout.WEST, simulationPanel);
    simulationPanel.add(amountLbl);

    // initial amount field
    amountFld = new JFormattedTextField(
            new DefaultFormatterFactory(new NumberFormatter(NumberFormat.getNumberInstance()),
                    new NumberFormatter(NumberFormat.getCurrencyInstance())));
    amountFld.setColumns(8);
    amountFld.setValue(simulationDataset.getInitial());
    amountFld.addPropertyChangeListener("value", new PropertyChangeListener() { //$NON-NLS-1$

        @Override
        public void propertyChange(PropertyChangeEvent e) {
            simulationDataset.setInitial(((Number) amountFld.getValue()).doubleValue());
            simulationDataset.updateDataset();
        }
    });
    springLayout.putConstraint(SpringLayout.VERTICAL_CENTER, amountLbl, 0, SpringLayout.VERTICAL_CENTER,
            amountFld);
    springLayout.putConstraint(SpringLayout.WEST, amountFld, 0, SpringLayout.EAST, amountLbl);
    springLayout.putConstraint(SpringLayout.NORTH, amountFld, 10, SpringLayout.NORTH, simulationPanel);
    simulationPanel.add(amountFld);

    // start date label
    JLabel fromLbl = new JLabel(Messages.getString("simulation.field.start-date")); //$NON-NLS-1$
    springLayout.putConstraint(SpringLayout.WEST, fromLbl, 10, SpringLayout.EAST, amountFld);
    simulationPanel.add(fromLbl);

    // start date field
    fromFld = new JFormattedTextField(new JodaLocalDateFormat());
    fromFld.setColumns(8);
    fromFld.setValue(simulationDataset.getStart());
    fromFld.addPropertyChangeListener("value", new PropertyChangeListener() { //$NON-NLS-1$

        @Override
        public void propertyChange(PropertyChangeEvent e) {
            simulationDataset.setStart((LocalDate) fromFld.getValue());
            simulationDataset.updateDataset();
        }
    });
    springLayout.putConstraint(SpringLayout.VERTICAL_CENTER, fromLbl, 0, SpringLayout.VERTICAL_CENTER, fromFld);
    springLayout.putConstraint(SpringLayout.WEST, fromFld, 0, SpringLayout.EAST, fromLbl);
    springLayout.putConstraint(SpringLayout.NORTH, fromFld, 10, SpringLayout.NORTH, simulationPanel);
    simulationPanel.add(fromFld);

    // end date label
    JLabel toLbl = new JLabel(Messages.getString("simulation.field.end-date")); //$NON-NLS-1$
    springLayout.putConstraint(SpringLayout.WEST, toLbl, 10, SpringLayout.EAST, fromFld);
    simulationPanel.add(toLbl);

    // end date field
    toFld = new JFormattedTextField(new JodaLocalDateFormat());
    toFld.setColumns(8);
    toFld.setValue(simulationDataset.getEnd());
    toFld.addPropertyChangeListener("value", new PropertyChangeListener() { //$NON-NLS-1$

        @Override
        public void propertyChange(PropertyChangeEvent e) {
            simulationDataset.setEnd((LocalDate) toFld.getValue());
            simulationDataset.updateDataset();
        }
    });
    springLayout.putConstraint(SpringLayout.VERTICAL_CENTER, toLbl, 0, SpringLayout.VERTICAL_CENTER, toFld);
    springLayout.putConstraint(SpringLayout.WEST, toFld, 0, SpringLayout.EAST, toLbl);
    springLayout.putConstraint(SpringLayout.NORTH, toFld, 10, SpringLayout.NORTH, simulationPanel);
    simulationPanel.add(toFld);

    // period label
    JLabel periodLbl = new JLabel(Messages.getString("simulation.field.period")); //$NON-NLS-1$
    springLayout.putConstraint(SpringLayout.WEST, periodLbl, 10, SpringLayout.EAST, toFld);
    simulationPanel.add(periodLbl);

    // period field
    periodFld = new JFormattedTextField(new JodaPeriodFormat());
    periodFld.setColumns(5);
    periodFld.setValue(simulationDataset.getPeriod());
    periodFld.addPropertyChangeListener("value", new PropertyChangeListener() { //$NON-NLS-1$

        @Override
        public void propertyChange(PropertyChangeEvent e) {
            simulationDataset.setPeriod((ReadablePeriod) periodFld.getValue());
            simulationDataset.updateDataset();
        }
    });
    springLayout.putConstraint(SpringLayout.VERTICAL_CENTER, periodLbl, 0, SpringLayout.VERTICAL_CENTER,
            periodFld);
    springLayout.putConstraint(SpringLayout.WEST, periodFld, 0, SpringLayout.EAST, periodLbl);
    springLayout.putConstraint(SpringLayout.NORTH, periodFld, 10, SpringLayout.NORTH, simulationPanel);
    simulationPanel.add(periodFld);

    // chart panel
    JFreeChart chart = ChartFactory.createLineChart("", Messages.getString("simulation.chart.date-axis-label"), //$NON-NLS-1$//$NON-NLS-2$
            Messages.getString("simulation.chart.amount-axis-label"), simulationDataset, //$NON-NLS-1$
            PlotOrientation.VERTICAL, false, true, false);
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesFilled(true);
    renderer.setBaseShapesVisible(true);
    renderer.setBaseToolTipGenerator(new SimulationTooltipGenerator(service));
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setDismissDelay(3600000);
    chartPanel.setInitialDelay(0);
    springLayout.putConstraint(SpringLayout.NORTH, chartPanel, 15, SpringLayout.SOUTH, periodFld);
    springLayout.putConstraint(SpringLayout.WEST, chartPanel, 10, SpringLayout.WEST, simulationPanel);
    springLayout.putConstraint(SpringLayout.SOUTH, chartPanel, -10, SpringLayout.SOUTH, simulationPanel);
    springLayout.putConstraint(SpringLayout.EAST, chartPanel, -10, SpringLayout.EAST, simulationPanel);
    simulationPanel.add(chartPanel);
}