/** Overriden to unconditionally allow the replace if ignoreDocumentMutate is true. */
 void replace(
     DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
     throws BadLocationException {
   if (ignoreDocumentMutate) {
     fb.replace(offset, length, text, attrs);
     return;
   }
   super.replace(fb, offset, length, text, attrs);
 }
 /**
  * Installs the <code>DefaultFormatter</code> onto a particular <code>JFormattedTextField</code>.
  * This will invoke <code>valueToString</code> to convert the current value from the <code>
  * JFormattedTextField</code> to a String. This will then install the <code>Action</code>s from
  * <code>getActions</code>, the <code>DocumentFilter</code> returned from <code>getDocumentFilter
  * </code> and the <code>NavigationFilter</code> returned from <code>getNavigationFilter</code>
  * onto the <code>JFormattedTextField</code>.
  *
  * <p>Subclasses will typically only need to override this if they wish to install additional
  * listeners on the <code>JFormattedTextField</code>.
  *
  * <p>If there is a <code>ParseException</code> in converting the current value to a String, this
  * will set the text to an empty String, and mark the <code>JFormattedTextField</code> as being in
  * an invalid state.
  *
  * <p>While this is a public method, this is typically only useful for subclassers of <code>
  * JFormattedTextField</code>. <code>JFormattedTextField</code> will invoke this method at the
  * appropriate times when the value changes, or its internal state changes.
  *
  * @param ftf JFormattedTextField to format for, may be null indicating uninstall from current
  *     JFormattedTextField.
  */
 public void install(JFormattedTextField ftf) {
   super.install(ftf);
   updateMaskIfNecessary();
   // invoked again as the mask should now be valid.
   positionCursorAtInitialLocation();
 }
 /** Overriden to update the mask after invoking supers implementation. */
 void updateValue(Object value) {
   super.updateValue(value);
   updateMaskIfNecessary();
 }
예제 #4
0
  public FormatTestFrame() {
    JPanel buttonPanel = new JPanel();
    okButton = new JButton("Ok");
    buttonPanel.add(okButton);
    add(buttonPanel, BorderLayout.SOUTH);

    mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(0, 3));
    add(mainPanel, BorderLayout.CENTER);

    JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField.setValue(new Integer(100));
    addRow("Number:", intField);

    JFormattedTextField intField2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField2.setValue(new Integer(100));
    intField2.setFocusLostBehavior(JFormattedTextField.COMMIT);
    addRow("Number (Commit behavior):", intField2);

    JFormattedTextField intField3 =
        new JFormattedTextField(
            new InternationalFormatter(NumberFormat.getIntegerInstance()) {
              protected DocumentFilter getDocumentFilter() {
                return filter;
              }
            });
    intField3.setValue(new Integer(100));
    addRow("Filtered Number", intField3);

    JFormattedTextField intField4 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField4.setValue(new Integer(100));
    intField4.setInputVerifier(
        new InputVerifier() {
          public boolean verify(JComponent component) {
            JFormattedTextField field = (JFormattedTextField) component;
            return field.isEditValid();
          }
        });
    addRow("Verified Number:", intField4);

    JFormattedTextField currencyField = new JFormattedTextField(NumberFormat.getCurrencyInstance());
    currencyField.setValue(new Double(10));
    addRow("Currency:", currencyField);

    JFormattedTextField dateField = new JFormattedTextField(DateFormat.getDateInstance());
    dateField.setValue(new Date());
    addRow("Date (default):", dateField);

    DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
    format.setLenient(false);
    JFormattedTextField dateField2 = new JFormattedTextField(format);
    dateField2.setValue(new Date());
    addRow("Date (short, not lenient):", dateField2);

    try {
      DefaultFormatter formatter = new DefaultFormatter();
      formatter.setOverwriteMode(false);
      JFormattedTextField urlField = new JFormattedTextField(formatter);
      urlField.setValue(new URL("http://java.sun.com"));
      addRow("URL:", urlField);
    } catch (MalformedURLException ex) {
      ex.printStackTrace();
    }

    try {
      MaskFormatter formatter = new MaskFormatter("###-##-####");
      formatter.setPlaceholderCharacter('0');
      JFormattedTextField ssnField = new JFormattedTextField(formatter);
      ssnField.setValue("078-05-1120");
      addRow("SSN Mask:", ssnField);
    } catch (ParseException ex) {
      ex.printStackTrace();
    }

    JFormattedTextField ipField = new JFormattedTextField(new IPAddressFormatter());
    ipField.setValue(new byte[] {(byte) 130, 65, 86, 66});
    addRow("IP Address:", ipField);
    pack();
  }