下面列出了怎么用javax.swing.JFormattedTextField.AbstractFormatter的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean hasValidValue(JSpinner spinner) {
NumberEditor numEditor = (NumberEditor) spinner.getEditor();
JFormattedTextField textField = numEditor.getTextField();
AbstractFormatter formatter = textField.getFormatter();
String text = textField.getText();
try {
String roundTrip = formatter.valueToString(formatter.stringToValue(text));
Double textDouble = Double.valueOf(text);
Double roundTripDouble = Double.valueOf(roundTrip);
return Double.compare(textDouble, roundTripDouble) == 0;
}
catch (ParseException e) {
return false;
}
catch (NumberFormatException nfe) {
return false;
}
}
@Override
public boolean verify( JComponent input ) {
if ( !(input instanceof JFormattedTextField) ) {
return true;
}
JFormattedTextField ftf = (JFormattedTextField)input;
AbstractFormatter formatter = ftf.getFormatter();
if ( formatter == null ) {
return true;
}
String text = ftf.getText();
try {
Integer intValue = ((Number) formatter.stringToValue(text)).intValue();
if ( intValue.compareTo( 0 ) < 0 ) {
// no negatives or values over 1
return false;
}
return true;
}
catch ( ParseException e ) {
return false;
}
}
/**
* @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;
}
private boolean checkEditorValue() {
NumberEditor numEditor = (NumberEditor) spinner.getEditor();
JFormattedTextField textField = numEditor.getTextField();
AbstractFormatter formatter = textField.getFormatter();
// to test if the textfield has a valid value, we try and parse it. There are two ways
// in which it is invalid - it can't be parsed or when parsed it doesn't match the spinner
// value.
String text = textField.getText();
try {
Double valueFromTextField = (Double) formatter.stringToValue(text);
Double spinnerValue = (Double) spinner.getValue();
// to compare the two values, convert them back to formatted strings to avoid rounding issues
String valueFromField = formatter.valueToString(valueFromTextField);
String valueFromSpinner = formatter.valueToString(spinnerValue);
if (valueFromField.equals(valueFromSpinner)) {
errorMessage = "";
return true;
}
}
catch (ParseException e) {
// Do nothing
}
errorMessage = "Invalid Value!";
return false;
}
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
return decimalFormatter;
}
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
return numberFormatter;
}
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
return formatter;
}
@Override
public boolean verify( JComponent input ) {
if ( !(input instanceof JFormattedTextField) ) {
return true;
}
JFormattedTextField ftf = (JFormattedTextField)input;
AbstractFormatter formatter = ftf.getFormatter();
if ( formatter == null ) {
return true;
}
String text = ftf.getText();
try {
//
// First, make sure we are within bounds
//
Number number = (Number) formatter.stringToValue(text);
if ( compareNumbers( number, upperRangeValue ) > 0 ||
compareNumbers( number, lowerRangeValue ) < 0 ) {
// no values above or below our max
return false;
}
//
// Second, don't let any value through that crosses our other field's range
//
boolean result = false;
Number otherNumber = (Number) otherField.getValue();
if ( isOtherFieldUpperRange ) {
// make sure our value is below the upper range value
result = compareNumbers( number, otherNumber ) <= 0;
}
// make sure our value is above the lower range value
else {
result = compareNumbers( number, otherNumber ) >= 0;
}
return result;
} catch (ParseException pe) {
return false;
}
}
public static FormattedTextFieldBuilder<Object> formattedTextFieldBuilder(AbstractFormatter formatter) {
return new FormattedTextFieldBuilder<Object>(formatter);
}
public FormattedTextFieldBuilder(AbstractFormatter passedFormat) {
super(new JFormattedTextField(passedFormat));
withGetter((field) -> (V) field.getValue());
withSetter((field, value) -> field.setValue((V) value));
}
FontFormatter(AbstractFormatter deleg) {
setOverwriteMode(false);
this.deleg = deleg;
}
/**
* Creates a {@link JFormattedTextField} for the specified cell. If a formatter is given, will
* apply it to the field. Does not validate the model, so make sure this call works!
*
* @param model
* @param rowIndex
* @param columnIndex
* @param cellClass
* @param formatter
* the formatter or <code>null</code> if none is required
* @param hideUnavailableContentAssist
* @return
*/
public CellTypeTextFieldDefaultImpl(final TablePanelModel model, final int rowIndex, final int columnIndex,
final Class<? extends CellType> cellClass, AbstractFormatter formatter, boolean hideUnavailableContentAssist) {
super();
final JFormattedTextField field = CellTypeImplHelper.createFormattedTextField(model, rowIndex, columnIndex);
setLayout(new BorderLayout());
add(field, BorderLayout.CENTER);
// otherwise 'null' would be restored
Object value = model.getValueAt(rowIndex, columnIndex);
String text = value != null ? String.valueOf(value) : "";
// specical handling when formatter is given
if (formatter != null) {
field.setFormatterFactory(new DefaultFormatterFactory(formatter));
}
field.setText(text);
// set syntax assist if available
String syntaxHelp = model.getSyntaxHelpAt(rowIndex, columnIndex);
if (syntaxHelp != null && !"".equals(syntaxHelp.trim())) {
SwingTools.setPrompt(syntaxHelp, field);
}
// see if content assist is possible for this field, if so add it
ImageIcon icon = SwingTools.createIcon("16/"
+ I18N.getMessageOrNull(I18N.getGUIBundle(), "gui.action.content_assist.icon"));
JButton contentAssistButton = new JButton();
contentAssistButton.setIcon(icon);
if (field.isEnabled() && model.isContentAssistPossibleForCell(rowIndex, columnIndex)) {
contentAssistButton.setToolTipText(I18N.getMessageOrNull(I18N.getGUIBundle(),
"gui.action.content_assist_enabled.tip"));
CellTypeImplHelper.addContentAssist(model, rowIndex, columnIndex, field, contentAssistButton, cellClass);
} else {
contentAssistButton.setToolTipText(I18N.getMessageOrNull(I18N.getGUIBundle(),
"gui.action.content_assist_disabled.tip"));
contentAssistButton.setEnabled(false);
}
if (contentAssistButton.isEnabled() || (!contentAssistButton.isEnabled() && !hideUnavailableContentAssist)) {
add(contentAssistButton, BorderLayout.EAST);
}
// set size so panels don't grow larger when they get the chance
setPreferredSize(new Dimension(300, 20));
setMinimumSize(new Dimension(100, 15));
setMaximumSize(new Dimension(1600, 30));
}