Example usage for javax.swing JFormattedTextField getFormatter

List of usage examples for javax.swing JFormattedTextField getFormatter

Introduction

In this page you can find the example usage for javax.swing JFormattedTextField getFormatter.

Prototype

@BeanProperty(visualUpdate = true, description = "TextFormatter, responsible for formatting the current value")
public AbstractFormatter getFormatter() 

Source Link

Document

Returns the AbstractFormatter that is used to format and parse the current value.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JFormattedTextField f = new JFormattedTextField(new SimpleDateFormat("yyyy-M-d"));
    f.setValue(new Date());

    DateFormatter fmt = (DateFormatter) f.getFormatter();
    fmt.setFormat(new SimpleDateFormat("d/M/yyyy"));

    f.setValue(f.getValue());//  w  w w.jav a 2s .c om
}

From source file:Main.java

public static DefaultFormatter getSpinnerFormatter(JSpinner spinner) {
    JComponent comp = spinner.getEditor();
    JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
    return (DefaultFormatter) field.getFormatter();
}

From source file:com.alvermont.terraj.util.ui.FormattedTextFieldVerifier.java

/**
 * Called to verify the state of the component
 *
 * @param input The input component//from  w ww.j  ava  2 s .  c o m
 * @return <pre>true</pre> if the input is in a valid state for the component
 */
public boolean verify(JComponent input) {
    if (input instanceof JFormattedTextField) {
        final JFormattedTextField ftf = (JFormattedTextField) input;

        final AbstractFormatter formatter = ftf.getFormatter();

        if (formatter != null) {
            final String text = ftf.getText();

            try {
                formatter.stringToValue(text);

                return true;
            } catch (ParseException pe) {
                // not a valid value
                return false;
            }
        }
    }

    return true;
}

From source file:org.revager.tools.GUITools.java

/**
 * Formats the given spinner./*from  w  ww . j a v  a 2s  . co m*/
 * 
 * @param sp
 *            the spinner
 */
public static void formatSpinner(JSpinner sp, boolean hideBorder) {
    JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor) sp.getEditor();
    JFormattedTextField ftf = defEditor.getTextField();
    ftf.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
    InternationalFormatter intFormatter = (InternationalFormatter) ftf.getFormatter();
    DecimalFormat decimalFormat = (DecimalFormat) intFormatter.getFormat();
    decimalFormat.applyPattern("00");
    DecimalFormatSymbols geSymbols = new DecimalFormatSymbols(Data.getInstance().getLocale());
    decimalFormat.setDecimalFormatSymbols(geSymbols);

    if (hideBorder) {
        sp.setBorder(null);
    }
}

From source file:org.rockyroadshub.planner.core.gui.calendar.FormPane.java

private void setAllowsInvalid(JSpinner s) {
    JFormattedTextField txt = ((JSpinner.NumberEditor) s.getEditor()).getTextField();
    ((NumberFormatter) txt.getFormatter()).setAllowsInvalid(false);
    s.addChangeListener((ChangeEvent e) -> {
        if (s.getValue().equals(24)) {
            s.setValue(0);//from ww w  . j  av  a  2 s  .  c o m
        }
    });
}

From source file:org.rockyroadshub.planner.core.gui.calendar.FormPane.java

private void setAllowsInvalid(JSpinner s1, JSpinner s2) {
    JFormattedTextField txt = ((JSpinner.NumberEditor) s1.getEditor()).getTextField();
    ((NumberFormatter) txt.getFormatter()).setAllowsInvalid(false);
    s1.addChangeListener((ChangeEvent e) -> {
        if (s1.getValue().equals(60)) {
            s1.setValue(0);//from  w  w  w.j ava2s  .  co m
            int i = (int) s2.getValue() + 1;
            s2.setValue(i);
        }
    });
}