javax.swing.text.DefaultFormatterFactory Java Examples

The following examples show how to use javax.swing.text.DefaultFormatterFactory. 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: ValueFormatter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 7 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example #2
Source File: SkillInfoTab.java    From pcgen with GNU Lesser General Public License v2.1 7 votes vote down vote up
private SkillRankSpinnerEditor(SkillRankSpinnerModel model)
{
	super(model);
	this.model = model;

	DefaultEditor editor = new DefaultEditor(spinner);
	NumberFormatter formatter = new NumberFormatter(new DecimalFormat("#0.#")); //$NON-NLS-1$
	formatter.setValueClass(Float.class);
	DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);

	JFormattedTextField ftf = editor.getTextField();
	ftf.setEditable(true);
	ftf.setFormatterFactory(factory);
	ftf.setHorizontalAlignment(SwingConstants.RIGHT);

	spinner.setEditor(editor);
}
 
Example #3
Source File: SwingNullableSpinner.java    From atdl4j with MIT License 6 votes vote down vote up
private NumberEditorNull(JSpinner spinner, DecimalFormat format) {
	super(spinner);
	if (!(spinner.getModel() instanceof SpinnerNumberModelNull)) {
		return;
	}
	
	SpinnerNumberModelNull model = (SpinnerNumberModelNull) spinner.getModel();
	NumberFormatter formatter = new NumberEditorFormatterNull(model, format);
	DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
	JFormattedTextField ftf = getTextField();
	ftf.setEditable(true);
	ftf.setFormatterFactory(factory);
	ftf.setHorizontalAlignment(JTextField.RIGHT);
	
	try {
		String maxString = formatter.valueToString(model.getMinimum());
		String minString = formatter.valueToString(model.getMaximum());
		ftf.setColumns(Math.max(maxString.length(), minString.length()));
	}
	catch (ParseException e) {
		// TBD should throw a chained error here
	}
}
 
Example #4
Source File: ReactionBonusEditor.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
protected void rebuildSelf(FlexGrid grid, FlexRow right) {
    ReactionBonus bonus = (ReactionBonus) getFeature();
    FlexRow       row   = new FlexRow();
    row.add(addChangeBaseTypeCombo());
    LeveledAmount amount = bonus.getAmount();
    row.add(addLeveledAmountField(amount, -99999, 99999));
    row.add(addLeveledAmountCombo(amount, false));
    row.add(new FlexSpacer(0, 0, true, false));
    grid.add(row, 0, 0);

    row = new FlexRow();
    row.setInsets(new Insets(0, 20, 0, 0));

    DefaultFormatter formatter = new DefaultFormatter();
    formatter.setOverwriteMode(false);
    mSituationField = new EditorField(new DefaultFormatterFactory(formatter), this, SwingConstants.LEFT, bonus.getSituation(), null);
    add(mSituationField);
    row.add(mSituationField);
    row.add(new FlexSpacer(0, 0, true, false));
    grid.add(row, 1, 0);
}
 
Example #5
Source File: FeatureEditor.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * @param amt The current {@link LeveledAmount}.
 * @param min The minimum value to allow.
 * @param max The maximum value to allow.
 * @return The {@link EditorField} that allows a {@link LeveledAmount} to be changed.
 */
protected EditorField addLeveledAmountField(LeveledAmount amt, int min, int max) {
    AbstractFormatter formatter;
    Object            value;
    Object            prototype;
    if (amt.isIntegerOnly()) {
        formatter = new IntegerFormatter(min, max, true);
        value = Integer.valueOf(amt.getIntegerAmount());
        prototype = Integer.valueOf(max);
    } else {
        formatter = new DoubleFormatter(min, max, true);
        value = Double.valueOf(amt.getAmount());
        prototype = Double.valueOf(max + 0.25);
    }
    EditorField field = new EditorField(new DefaultFormatterFactory(formatter), this, SwingConstants.LEFT, value, prototype, null);
    field.putClientProperty(LeveledAmount.class, amt);
    UIUtilities.setToPreferredSizeOnly(field);
    add(field);
    return field;
}
 
Example #6
Source File: SkillInfoTab.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
private SkillRankSpinnerEditor(SkillRankSpinnerModel model)
{
	super(model);
	this.model = model;

	DefaultEditor editor = new DefaultEditor(spinner);
	NumberFormatter formatter = new NumberFormatter(new DecimalFormat("#0.#")); //$NON-NLS-1$
	formatter.setValueClass(Float.class);
	DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);

	JFormattedTextField ftf = editor.getTextField();
	ftf.setEditable(true);
	ftf.setFormatterFactory(factory);
	ftf.setHorizontalAlignment(SwingConstants.RIGHT);

	spinner.setEditor(editor);
}
 
Example #7
Source File: ContainedWeightReductionEditor.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
protected void rebuildSelf(FlexGrid grid, FlexRow right) {
    ContainedWeightReduction feature = (ContainedWeightReduction) getFeature();
    FlexRow                  row     = new FlexRow();
    row.add(addChangeBaseTypeCombo());
    EditorField field = new EditorField(new DefaultFormatterFactory(new WeightReductionFormatter()), (event) -> {
        EditorField source = (EditorField) event.getSource();
        if ("value".equals(event.getPropertyName())) {
            feature.setValue(source.getValue());
            notifyActionListeners();
        }
    }, SwingConstants.LEFT, feature.getValue(), new WeightValue(new Fixed6(999999999), getRow().getDataFile().defaultWeightUnits()), I18n.Text("Enter a weight or percentage, e.g. \"2 lb\" or \"5%\"."));
    UIUtilities.setToPreferredSizeOnly(field);
    add(field);
    row.add(field);
    row.add(new FlexSpacer(0, 0, true, false));
    grid.add(row, 0, 0);
}
 
Example #8
Source File: NumberPropertyEditor.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
public NumberPropertyEditor(Class type) {
  if (!Number.class.isAssignableFrom(type)) {
    throw new IllegalArgumentException("type must be a subclass of Number");
  }

  editor = new JFormattedTextField();
  this.type = type;
  ((JFormattedTextField)editor).setValue(getDefaultValue());
  ((JFormattedTextField)editor).setBorder(LookAndFeelTweaks.EMPTY_BORDER);

  // use a custom formatter to have numbers with up to 64 decimals
  NumberFormat format = NumberConverters.getDefaultFormat();

  ((JFormattedTextField) editor).setFormatterFactory(
      new DefaultFormatterFactory(new NumberFormatter(format))
  );
}
 
Example #9
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
protected LocalDateTimeEditor(JSpinner spinner, String dateFormatPattern) {
  super(spinner);
  if (!(spinner.getModel() instanceof SpinnerLocalDateTimeModel)) {
    throw new IllegalArgumentException("model not a SpinnerLocalDateTimeModel");
  }
  dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormatPattern);
  model = (SpinnerLocalDateTimeModel) spinner.getModel();
  DefaultFormatter formatter = new LocalDateTimeFormatter();

  EventQueue.invokeLater(() -> {
    formatter.setValueClass(LocalDateTime.class);
    JFormattedTextField ftf = getTextField();
    try {
      String maxString = formatter.valueToString(model.getStart());
      String minString = formatter.valueToString(model.getEnd());
      ftf.setColumns(Math.max(maxString.length(), minString.length()));
    } catch (ParseException ex) {
      // PENDING: hmuller
      ex.printStackTrace();
      UIManager.getLookAndFeel().provideErrorFeedback(ftf);
    }
    ftf.setHorizontalAlignment(SwingConstants.LEFT);
    ftf.setEditable(true);
    ftf.setFormatterFactory(new DefaultFormatterFactory(formatter));
  });
}
 
Example #10
Source File: NumberPropertyEditor.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
public NumberPropertyEditor(Class type) {
  if (!Number.class.isAssignableFrom(type)) {
    throw new IllegalArgumentException("type must be a subclass of Number");
  }

  editor = new JFormattedTextField();
  this.type = type;
  ((JFormattedTextField)editor).setValue(getDefaultValue());
  ((JFormattedTextField)editor).setBorder(LookAndFeelTweaks.EMPTY_BORDER);

  // use a custom formatter to have numbers with up to 64 decimals
  NumberFormat format = NumberConverters.getDefaultFormat();

  ((JFormattedTextField) editor).setFormatterFactory(
      new DefaultFormatterFactory(new NumberFormatter(format))
  );
}
 
Example #11
Source File: SingleIntegerFieldOptionsPanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Sets integer number format to JFormattedTextField instance,
 * sets value of JFormattedTextField instance to object's field value,
 * synchronizes object's field value with the value of JFormattedTextField instance.
 *
 * @param textField  JFormattedTextField instance
 * @param owner      an object whose field is synchronized with {@code textField}
 * @param property   object's field name for synchronization
 */
public static void setupIntegerFieldTrackingValue(final JFormattedTextField textField,
                                                  final InspectionProfileEntry owner,
                                                  final String property) {
    NumberFormat formatter = NumberFormat.getIntegerInstance();
    formatter.setParseIntegerOnly(true);
    textField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(formatter)));
    textField.setValue(getPropertyValue(owner, property));
    final Document document = textField.getDocument();
    document.addDocumentListener(new DocumentAdapter() {
        @Override
        public void textChanged(DocumentEvent e) {
            try {
                textField.commitEdit();
                setPropertyValue(owner, property,
                        ((Number) textField.getValue()).intValue());
            } catch (ParseException e1) {
                // No luck this time
            }
        }
    });
}
 
Example #12
Source File: WeaponEditor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new text field.
 *
 * @param protoValue A prototype value. If not {@code null}, will be used to set the only size
 *                   the field can have.
 * @param tooltip    The tooltip to set on the field.
 * @return The newly created field.
 */
protected EditorField createTextField(String protoValue, String tooltip) {
    DefaultFormatter formatter = new DefaultFormatter();
    formatter.setOverwriteMode(false);
    EditorField field = new EditorField(new DefaultFormatterFactory(formatter), this, SwingConstants.LEFT, "", protoValue, tooltip);
    if (protoValue != null) {
        UIUtilities.setToPreferredSizeOnly(field);
    }
    return field;
}
 
Example #13
Source File: ValueFormatter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example #14
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static DefaultFormatterFactory makeFFactory(DecimalFormatSymbols dfs) {
  DecimalFormat format = new DecimalFormat("0.00", dfs);

  NumberFormatter displayFormatter = new NumberFormatter(format);
  displayFormatter.setValueClass(Double.class);

  NumberFormatter editFormatter = new NumberFormatter(format);
  editFormatter.setValueClass(Double.class);
  return new DefaultFormatterFactory(displayFormatter, displayFormatter, editFormatter);
}
 
Example #15
Source File: NoGroupingSpinner.java    From microba with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public NoGroupingNumberEditor(JSpinner spinner, SpinnerModel model) {
	super(spinner);
	JFormattedTextField ftf = (JFormattedTextField) this
			.getComponent(0);
	NumberFormat fmt = NumberFormat.getIntegerInstance();
	fmt.setGroupingUsed(false);
	ftf.setFormatterFactory(new DefaultFormatterFactory(
			new NumberFormatter(fmt)));
	revalidate();
}
 
Example #16
Source File: FormattedDecimalField.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
/**
 * @param min           The minimum value
 * @param max           The maximum value
 * @param decimalPlaces The number of decimal places to show (padding as required)
 * @param maxLength     The maximum length
 */
public FormattedDecimalField(double min, double max, int decimalPlaces, int maxLength) {

    super();

    Preconditions.checkNotNull(min, "'min' must be present");
    Preconditions.checkNotNull(max, "'max' must be present");
    Preconditions.checkState(min < max, "'min' must be less than 'max'");

    Preconditions.checkState(decimalPlaces >= 0 && decimalPlaces < 15, "'decimalPlaces' must be in range [0,15)");

    // setInputVerifier(new ThemeAwareDecimalInputVerifier(min, max));

    setBackground(Themes.currentTheme.dataEntryBackground());

    // Build number formatters
    NumberFormatter defaultFormatter = new NumberFormatter();
    defaultFormatter.setValueClass(BigDecimal.class);

    NumberFormatter displayFormatter = Numbers.newDisplayFormatter(decimalPlaces, maxLength);
    NumberFormatter editFormatter = Numbers.newEditFormatter(decimalPlaces, maxLength);

    setFormatterFactory(new DefaultFormatterFactory(
            defaultFormatter,
            displayFormatter,
            editFormatter
    ));

}
 
Example #17
Source File: DateTimePicker.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void updateTextFieldFormat() {
    if (timeSpinner == null) {
        return;
    }
    JFormattedTextField tf = ((JSpinner.DefaultEditor) timeSpinner.getEditor()).getTextField();
    DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
    DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();
    // Change the date format to only show the hours
    formatter.setFormat(timeFormat);
}
 
Example #18
Source File: DateTimePicker.java    From nosql4idea with Apache License 2.0 5 votes vote down vote up
private void updateTextFieldFormat() {
    if (timeSpinner == null) return;
    JFormattedTextField tf = ((JSpinner.DefaultEditor) timeSpinner.getEditor()).getTextField();
    DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
    DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();
    // Change the date format to only show the hours
    formatter.setFormat(timeFormat);
}
 
Example #19
Source File: TimeMaskFormatter.java    From nmonvisualizer with Apache License 2.0 5 votes vote down vote up
public static DefaultFormatterFactory createFormatterFactory(boolean start) {
    // START and END may need to be cloned if used in multiple threads
    if (start) {
        return new DefaultFormatterFactory(START, START, START, START);
    }
    else {
        return new DefaultFormatterFactory(END, END, END, END);
    }
}
 
Example #20
Source File: CustomAxisEditionDialog.java    From Girinoscope with Apache License 2.0 5 votes vote down vote up
private JFormattedTextField createMumberField(List<NumberFormatter> dynamicFormatters) {
    final NumberFormatter defaultFormatter = new NumberFormatter(new DecimalFormat(axisBuilder.getFormat()));
    final NumberFormatter displayFormatter = new NumberFormatter(new DecimalFormat(axisBuilder.getFormat()));
    final NumberFormatter editFormatter = new NumberFormatter(new DecimalFormat());

    dynamicFormatters.add(defaultFormatter);
    dynamicFormatters.add(displayFormatter);

    JFormattedTextField numberField = new JFormattedTextField( //
            new DefaultFormatterFactory(defaultFormatter, displayFormatter, editFormatter));
    numberField.setColumns(12);
    return numberField;
}
 
Example #21
Source File: ColorValueFormatter.java    From darklaf with MIT License 5 votes vote down vote up
public static ColorValueFormatter init(final DarkColorModel model, final int index,
                                       final boolean hex, final JFormattedTextField text) {
    ColorValueFormatter formatter = new ColorValueFormatter(model, index, hex);
    formatter.setText(text);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
    return formatter;
}
 
Example #22
Source File: PageField.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private static AbstractFormatterFactory getFormatterFactoryForType(GURPSCharacter character, String type) {
    if (GURPSCharacter.ID_BASIC_THRUST.equals(type) || GURPSCharacter.ID_BASIC_SWING.equals(type)) {
        return new DefaultFormatterFactory(new DiceFormatter(character));
    }
    AbstractFormatterFactory factory = FACTORY_MAP.get(type);
    return factory != null ? factory : DEFAULT_FACTORY;
}
 
Example #23
Source File: CSearchInputPanel.java    From binnavi with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the input field according to parameters selected in the panel.
 */
private void updateTextFieldMask() {
  if (m_modeBox.getSelectedIndex() == 0) {
    m_inputField.setFormatterFactory(new DefaultFormatterFactory());
  } else {
    m_inputField.setFormatterFactory(new DefaultFormatterFactory(new CHexFormatter(8)));
  }
}
 
Example #24
Source File: CHelpSaveFormattedField.java    From binnavi with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new formatted field object.
 *
 * @param factory Formatter factory used to format the text field.
 * @param information Provides context-sensitive help.
 */
public CHelpSaveFormattedField(final DefaultFormatterFactory factory, final IHelpInformation information)
{
	super(factory);

	m_information = information;
}
 
Example #25
Source File: ValueFormatter.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example #26
Source File: ValueFormatter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example #27
Source File: DatePicker.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public void setFormats(DateFormat... formats) {
    if (formats != null) {
        Contract.asNotNull(formats, "the array of formats " + "must not contain null elements");
    }
    DateFormat[] old = getFormats();
    for (DateFormat format : formats) {
        format.setLenient(false);
    }
    getEditor().setFormatterFactory(new DefaultFormatterFactory(
            new DatePicker.CustomDatePickerFormatter(formats, getLocale())));
    firePropertyChange("formats", old, getFormats());
}
 
Example #28
Source File: ValueFormatter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example #29
Source File: ValueFormatter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example #30
Source File: ValueFormatter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}