javax.swing.text.MaskFormatter Java Examples

The following examples show how to use javax.swing.text.MaskFormatter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FormatSelector.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Returns format's pattern. 
 * 
 * @return format's pattern.
 */
public String getFormat() {
    if (format != null) return format;
    String fmt = null;
    if (formatter instanceof MaskFormatter) {
        fmt = ((MaskFormatter)formatter).getMask();
    } else if (formatter instanceof InternationalFormatter) {
        Format f = ((InternationalFormatter)formatter).getFormat();
        if (f instanceof DecimalFormat) {
            fmt = ((DecimalFormat)f).toPattern();
        } else if (f instanceof SimpleDateFormat) {
            fmt = ((SimpleDateFormat)f).toPattern();
        }
    }
    return fmt;
}
 
Example #2
Source File: DesktopTimeField.java    From cuba with Apache License 2.0 6 votes vote down vote up
public DesktopTimeField() {
    UserSessionSource uss = AppBeans.get(UserSessionSource.NAME);

    digitWidth = getDigitWidth();

    timeFormat = Datatypes.getFormatStringsNN(uss.getLocale()).getTimeFormat();
    resolution = DateField.Resolution.MIN;
    formatter = new MaskFormatter();
    formatter.setPlaceholderCharacter('_');
    impl = new FlushableFormattedTextField(formatter);
    FieldListener listener = new FieldListener();
    impl.addFocusListener(listener);
    impl.addKeyListener(listener);

    setShowSeconds(timeFormat.contains("ss"));
}
 
Example #3
Source File: HexValue4Field.java    From zxpoly with GNU General Public License v3.0 6 votes vote down vote up
public HexValue4Field() {
  super();
  final JFormattedTextField.AbstractFormatter FORMAT;
  try {
    final MaskFormatter formatter = new MaskFormatter("HHHH");
    formatter.setPlaceholderCharacter('0');
    formatter.setValidCharacters(ALLOWED_CHARS);
    formatter.setAllowsInvalid(false);
    FORMAT = formatter;
  } catch (ParseException ex) {
    throw new Error("Can't prepare formatter", ex);
  }

  setFormatter(FORMAT);
  refreshTextValue();
}
 
Example #4
Source File: HexValue2Field.java    From zxpoly with GNU General Public License v3.0 6 votes vote down vote up
public HexValue2Field() {
  super();
  final JFormattedTextField.AbstractFormatter FORMAT;
  try {
    final MaskFormatter formatter = new MaskFormatter("HH");
    formatter.setValidCharacters(ALLOWED_CHARS);
    formatter.setPlaceholderCharacter('0');
    formatter.setAllowsInvalid(false);
    FORMAT = formatter;
  } catch (ParseException ex) {
    throw new Error("Can't prepare formatter", ex);
  }

  setFormatter(FORMAT);
  refreshTextValue();
}
 
Example #5
Source File: Fields.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public static FormattedTextFieldBuilder<String> formattedTextFieldBuilder(String mask) {
   try {
      return new FormattedTextFieldBuilder<String>(new MaskFormatter(mask));
   }
   catch(ParseException e) {
      throw new IllegalArgumentException("Invalid mask: " + mask, e);
   }
}
 
Example #6
Source File: Register.java    From dctb-utfpr-2018-1 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new form Register
 */
public Register() {
   
    try {
        MaskFormatter maskData = new MaskFormatter("##/##/####");
        maskData.install(txtDataFormatada);
        initComponents();
    } catch (ParseException e) {
        e.printStackTrace();
    } 
      
}
 
Example #7
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  Box box = Box.createVerticalBox();
  box.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

  String mask = "###-####";

  MaskFormatter formatter0 = createFormatter(mask);
  JFormattedTextField field0 = new JFormattedTextField(formatter0);
  box.add(makeTitledPanel("new MaskFormatter(\"###-####\")", field0));
  box.add(Box.createVerticalStrut(15));

  MaskFormatter formatter1 = createFormatter(mask);
  formatter1.setPlaceholderCharacter('_');
  JFormattedTextField field1 = new JFormattedTextField(formatter1);

  MaskFormatter formatter2 = createFormatter(mask);
  formatter2.setPlaceholderCharacter('_');
  formatter2.setPlaceholder("000-0000");
  JFormattedTextField field2 = new JFormattedTextField(formatter2);
  box.add(makeTitledPanel("MaskFormatter#setPlaceholderCharacter('_')", field1));
  box.add(Box.createVerticalStrut(15));

  Font font = new Font(Font.MONOSPACED, Font.PLAIN, 18);
  Insets insets = new Insets(1, 1 + 18 / 2, 1, 1);
  Stream.of(field0, field1, field2).forEach(tf -> {
    tf.setFont(font);
    tf.setColumns(mask.length() + 1);
    tf.setMargin(insets);
  });
  box.add(makeTitledPanel("MaskFormatter#setPlaceholder(\"000-0000\")", field2));
  box.add(Box.createVerticalGlue());

  add(box, BorderLayout.NORTH);
  setPreferredSize(new Dimension(320, 240));
}
 
Example #8
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  MaskFormatter formatter = createFormatter("UUUUUUUUUU");
  // formatter.setAllowsInvalid(true);
  // formatter.setCommitsOnValidEdit(true);
  // formatter.setPlaceholder("_");
  // formatter.setPlaceholderCharacter('?');

  JFormattedTextField field1 = new JFormattedTextField(formatter);
  field1.setFocusLostBehavior(JFormattedTextField.REVERT);

  JFormattedTextField field2 = new JFormattedTextField(formatter);
  field2.setFocusLostBehavior(JFormattedTextField.COMMIT);

  JFormattedTextField field3 = new JFormattedTextField(formatter);
  field3.setFocusLostBehavior(JFormattedTextField.PERSIST);

  JCheckBox check = new JCheckBox("setCommitsOnValidEdit");
  check.addActionListener(e -> formatter.setCommitsOnValidEdit(((JCheckBox) e.getSource()).isSelected()));

  Box box = Box.createVerticalBox();
  box.add(makeTitledPanel("COMMIT_OR_REVERT(default)", new JFormattedTextField(formatter)));
  box.add(Box.createVerticalStrut(5));
  box.add(makeTitledPanel("REVERT", field1));
  box.add(Box.createVerticalStrut(5));
  box.add(makeTitledPanel("COMMIT", field2));
  box.add(Box.createVerticalStrut(5));
  box.add(makeTitledPanel("PERSIST", field3));
  box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  add(box, BorderLayout.NORTH);
  add(check, BorderLayout.SOUTH);
  setPreferredSize(new Dimension(320, 240));
}
 
Example #9
Source File: JFormattedTextFieldExample.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns sample formatter.
 *
 * @param mask formatter mask
 * @return sample formatter
 */
protected static MaskFormatter createFormatter ( final String mask )
{
    try
    {
        return new MaskFormatter ( mask );
    }
    catch ( final ParseException e )
    {
        throw new RuntimeException ( "Unable to parse formatter mask: " + mask, e );
    }
}
 
Example #10
Source File: WebFormattedTextFieldExample.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns sample formatter.
 *
 * @param mask formatter mask
 * @return sample formatter
 */
protected static MaskFormatter createFormatter ( final String mask )
{
    try
    {
        return new MaskFormatter ( mask );
    }
    catch ( final ParseException e )
    {
        throw new RuntimeException ( "Unable to parse formatter mask: " + mask, e );
    }
}
 
Example #11
Source File: Tools.java    From eplmp with Eclipse Public License 1.0 4 votes vote down vote up
public static String increaseId(String id, String mask) throws ParseException {
    LOGGER.info("#### Tools.increaseId id = " + id + " , mask = " + mask);
    MaskFormatter formatter = new MaskFormatter(mask);
    formatter.setValueContainsLiteralCharacters(false);
    String value = formatter.stringToValue(id).toString();
    StringBuilder newValue = new StringBuilder();
    boolean increase = true;
    for (int i = value.length() - 1; i >= 0; i--) {
        char c = value.charAt(i);
        switch (c) {
            case '9':
                newValue.append((increase) ? '0' : '9');
                break;

            case '8':
                newValue.append((increase) ? '9' : '8');
                increase = false;
                break;

            case '7':
                newValue.append((increase) ? '8' : '7');
                increase = false;
                break;

            case '6':
                newValue.append((increase) ? '7' : '6');
                increase = false;
                break;

            case '5':
                newValue.append((increase) ? '6' : '5');
                increase = false;
                break;

            case '4':
                newValue.append((increase) ? '5' : '4');
                increase = false;
                break;

            case '3':
                newValue.append((increase) ? '4' : '3');
                increase = false;
                break;

            case '2':
                newValue.append((increase) ? '3' : '2');
                increase = false;
                break;

            case '1':
                newValue.append((increase) ? '2' : '1');
                increase = false;
                break;

            case '0':
                newValue.append((increase) ? '1' : '0');
                increase = false;
                break;

            default:
                newValue.append(c);
                break;
        }
    }
    return formatter.valueToString(newValue.reverse().toString());
}
 
Example #12
Source File: DesktopTimeField.java    From cuba with Apache License 2.0 4 votes vote down vote up
public FlushableFormattedTextField(MaskFormatter formatter) {
    super(formatter);
}