下面列出了java.awt.DefaultFocusTraversalPolicy#javax.swing.text.DefaultFormatterFactory 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @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;
}
@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);
}
@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);
}
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))
);
}
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);
}
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))
);
}
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));
});
}
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);
}
/**
* 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
}
}
});
}
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
}
}
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;
}
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);
}
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);
}
private JComponent createComponent(String filterName) {
final JPanel panel = new JPanel(new BorderLayout());
Border paddingBorder = BorderFactory.createEmptyBorder(1, 5, 1, 5);
Border outsideBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
panel.setBorder(BorderFactory.createCompoundBorder(outsideBorder, paddingBorder));
DefaultFormatterFactory factory = new DefaultFormatterFactory(new DefaultFormatter());
textField = new FilterFormattedTextField(factory, defaultValue);
textField.setName(filterName + " Field"); // for debugging
textField.setColumns(20);
textField.setMinimumSize(textField.getPreferredSize());
// we handle updates in real time, so ignore focus events, which trigger excess filtering
textField.disableFocusEventProcessing();
JLabel label = new GDLabel(filterName + ": ");
panel.add(label, BorderLayout.WEST);
panel.add(textField, BorderLayout.CENTER);
StatusLabel nameFieldStatusLabel = new StatusLabel(textField, defaultValue);
textField.addFilterStatusListener(nameFieldStatusLabel);
textField.addFilterStatusListener(status -> fireStatusChanged(status));
final JLayeredPane layeredPane = new JLayeredPane();
layeredPane.add(panel, BASE_COMPONENT_LAYER);
layeredPane.add(nameFieldStatusLabel, HOVER_COMPONENT_LAYER);
layeredPane.setPreferredSize(panel.getPreferredSize());
layeredPane.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(java.awt.event.ComponentEvent e) {
Dimension preferredSize = layeredPane.getSize();
panel.setBounds(0, 0, preferredSize.width, preferredSize.height);
panel.validate();
}
});
return layeredPane;
}
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);
}
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);
}
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);
}
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);
}
public IntegerEditor(int min, int max) {
super(new JFormattedTextField());
ftf = (JFormattedTextField) getComponent();
minimum = new Integer(min);
maximum = new Integer(max);
// Set up the editor for the integer cells.
integerFormat = NumberFormat.getIntegerInstance();
NumberFormatter intFormatter = new NumberFormatter(integerFormat);
intFormatter.setFormat(integerFormat);
intFormatter.setMinimum(minimum);
intFormatter.setMaximum(maximum);
ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
ftf.setValue(minimum);
ftf.setHorizontalAlignment(JTextField.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
// React when the user presses Enter while the editor is
// active. (Tab is handled as specified by
// JFormattedTextField's focusLostBehavior property.)
ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
ftf.getActionMap().put("check", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (!ftf.isEditValid()) { // The text is invalid.
if (userSaysRevert()) { // reverted
ftf.postActionEvent(); // inform the editor
}
} else
try { // The text is valid,
ftf.commitEdit(); // so use it.
ftf.postActionEvent(); // stop editing
} catch (java.text.ParseException exc) {
}
}
});
}
public AdvancedSecurityPanel(Binding binding, ConfigVersion cfgVersion) {
this.binding = binding;
this.cfgVersion = cfgVersion;
freshnessff = new DefaultFormatterFactory();
NumberFormat freshnessFormat = NumberFormat.getIntegerInstance();
freshnessFormat.setGroupingUsed(false);
NumberFormatter freshnessFormatter = new NumberFormatter(freshnessFormat);
freshnessFormat.setMaximumIntegerDigits(8);
freshnessFormatter.setCommitsOnValidEdit(true);
freshnessFormatter.setMinimum(0);
freshnessFormatter.setMaximum(99999999);
freshnessff.setDefaultFormatter(freshnessFormatter);
skewff = new DefaultFormatterFactory();
NumberFormat skewFormat = NumberFormat.getIntegerInstance();
skewFormat.setGroupingUsed(false);
NumberFormatter skewFormatter = new NumberFormatter(skewFormat);
skewFormat.setMaximumIntegerDigits(8);
skewFormatter.setCommitsOnValidEdit(true);
skewFormatter.setMinimum(0);
skewFormatter.setMaximum(99999999);
skewff.setDefaultFormatter(skewFormatter);
initComponents();
sync();
}
public AdvancedRMPanel(Binding binding, ConfigVersion cfgVersion) {
super();
this.binding = binding;
this.cfgVersion = cfgVersion;
milisecondsff = new DefaultFormatterFactory();
NumberFormat millisecondsFormat = NumberFormat.getIntegerInstance();
millisecondsFormat.setGroupingUsed(false);
NumberFormatter millisecondsFormatter = new NumberFormatter(millisecondsFormat);
millisecondsFormat.setMaximumIntegerDigits(8);
millisecondsFormatter.setCommitsOnValidEdit(true);
millisecondsFormatter.setMinimum(0);
millisecondsFormatter.setMaximum(99999999);
milisecondsff.setDefaultFormatter(millisecondsFormatter);
maxBufff = new DefaultFormatterFactory();
NumberFormat maxBufFormat = NumberFormat.getIntegerInstance();
maxBufFormat.setGroupingUsed(false);
NumberFormatter maxBufFormatter = new NumberFormatter(maxBufFormat);
maxBufFormat.setMaximumIntegerDigits(8);
maxBufFormatter.setCommitsOnValidEdit(true);
maxBufFormatter.setMinimum(0);
maxBufFormatter.setMaximum(99999999);
maxBufff.setDefaultFormatter(maxBufFormatter);
initComponents();
inSync = true;
for (RMDeliveryAssurance assurance : RMDeliveryAssurance.values()) {
deliveryAssuranceCombo.addItem(assurance);
}
inSync = false;
sync();
refresh();
}
/**
* Creates new form STSConfigServicePanel
*/
public STSConfigServicePanel( Project p, Binding binding, ConfigVersion cfgVersion) {
this.project = p;
this.binding = binding;
this.cfgVersion = cfgVersion;
lifeTimeDff = new DefaultFormatterFactory();
NumberFormat lifetimeFormat = NumberFormat.getIntegerInstance();
lifetimeFormat.setGroupingUsed(false);
lifetimeFormat.setParseIntegerOnly(true);
lifetimeFormat.setMaximumFractionDigits(0);
NumberFormatter lifetimeFormatter = new NumberFormatter(lifetimeFormat);
lifetimeFormatter.setCommitsOnValidEdit(true);
lifetimeFormatter.setMinimum(0);
lifeTimeDff.setDefaultFormatter(lifetimeFormatter);
initComponents();
inSync = true;
ServiceProvidersTablePanel.ServiceProvidersTableModel tablemodel = new ServiceProvidersTablePanel.ServiceProvidersTableModel();
this.remove(serviceProvidersPanel);
STSConfiguration stsConfig = ProprietarySecurityPolicyModelHelper.getSTSConfiguration(binding);
if (stsConfig == null) {
stsConfig = ProprietarySecurityPolicyModelHelper.createSTSConfiguration(binding);
}
serviceProvidersPanel = new ServiceProvidersTablePanel(tablemodel, stsConfig, cfgVersion);
((ServiceProvidersTablePanel)serviceProvidersPanel).populateModel();
inSync = false;
sync();
}
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);
}
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);
}
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);
}
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);
}
private void createCopiesField(PrintRequestAttributeSet set) {
PrintService service = getService();
if (service.isAttributeCategorySupported(Copies.class)) {
mCopies = new EditorField(new DefaultFormatterFactory(new IntegerFormatter(1, 999, false)), null, SwingConstants.RIGHT, Integer.valueOf(PrintUtilities.getCopies(service, set)), Integer.valueOf(999), null);
UIUtilities.setToPreferredSizeOnly(mCopies);
LinkedLabel label = new LinkedLabel(I18n.Text("Copies"), mCopies);
add(label, new PrecisionLayoutData().setEndHorizontalAlignment());
add(mCopies);
} else {
mCopies = null;
}
}
/**
* @param compare The current string compare object.
* @return The field that allows a string comparison to be changed.
*/
protected EditorField addStringCompareField(StringCriteria compare) {
DefaultFormatter formatter = new DefaultFormatter();
formatter.setOverwriteMode(false);
EditorField field = new EditorField(new DefaultFormatterFactory(formatter), this, SwingConstants.LEFT, compare.getQualifier(), null);
field.putClientProperty(StringCriteria.class, compare);
add(field);
return field;
}
/**
* @param compare The current compare object.
* @param min The minimum value to allow.
* @param max The maximum value to allow.
* @param forceSign Whether to force the sign to be visible.
* @return The {@link EditorField} that allows an integer comparison to be changed.
*/
protected EditorField addNumericCompareField(IntegerCriteria compare, int min, int max, boolean forceSign) {
EditorField field = new EditorField(new DefaultFormatterFactory(new IntegerFormatter(min, max, forceSign)), this, SwingConstants.LEFT, Integer.valueOf(compare.getQualifier()), Integer.valueOf(max), null);
field.putClientProperty(IntegerCriteria.class, compare);
UIUtilities.setToPreferredSizeOnly(field);
add(field);
return field;
}
/**
* @param compare The current compare object.
* @param min The minimum value to allow.
* @param max The maximum value to allow.
* @param forceSign Whether to force the sign to be visible.
* @return The {@link EditorField} that allows a double comparison to be changed.
*/
protected EditorField addNumericCompareField(DoubleCriteria compare, double min, double max, boolean forceSign) {
EditorField field = new EditorField(new DefaultFormatterFactory(new DoubleFormatter(min, max, forceSign)), this, SwingConstants.LEFT, Double.valueOf(compare.getQualifier()), Double.valueOf(max), null);
field.putClientProperty(DoubleCriteria.class, compare);
UIUtilities.setToPreferredSizeOnly(field);
add(field);
return field;
}