Просто используйте собственный форматтер для JFormattedTextField
из DefaultEditor
из JSpinner
, как показано ниже:
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JFormattedTextField.AbstractFormatterFactory;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.SpinnerNumberModel;
public class Main {
public static class CommmaDotFormatter extends AbstractFormatter {
private boolean useComma = false;
@Override
public Object stringToValue(String text) throws ParseException {
if (text == null || text.trim().isEmpty())
throw new ParseException("Null or empty text.", 0);
try {
useComma = (text.contains(",") && (text.indexOf(',') == text.lastIndexOf(',')));
return Double.valueOf(useComma? text.replace(',', '.'): text);
}
catch (final NumberFormatException nfx) {
//Find the location of the error (so as to generate appropriate ParseException):
int i = 0;
for (final int cp: text.codePoints().toArray()) {
if (!Character.isDigit(cp) && cp != ',' && cp != '.')
throw new ParseException("Failed to parse text \"" + text + "\".", i);
++i;
}
//Could happen if the text is composed by digits and more than one dot/comma:
throw new ParseException("Failed to parse text \"" + text + "\".", 0);
}
}
@Override
public String valueToString(final Object value) throws ParseException {
final String text = String.format("%.2f", value);
return useComma? text: text.replace(',', '.');
}
}
public static class CommmaDotFormatterFactory extends AbstractFormatterFactory {
@Override
public AbstractFormatter getFormatter(final JFormattedTextField tf) {
if (!(tf.getFormatter() instanceof CommmaDotFormatter))
return new CommmaDotFormatter();
return tf.getFormatter();
}
}
public static void main(final String[] args) {
final JSpinner spin = new JSpinner(new SpinnerNumberModel(0, -10, 10, 0.01));
((DefaultEditor) spin.getEditor()).getTextField().setFormatterFactory(new CommmaDotFormatterFactory());
final JFrame frame = new JFrame("JSpinner infinite value");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(spin);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Примечание: свойство useComma
можно не указывать. Он просто существует для поддержания состояния последнего ввода пользователя. Например, если пользователь вводит значение с запятой, а не с точкой, счетчик будет продолжать вращаться с запятыми. То же самое для точек. Независимо от того, что пользователь вводит, он останется для будущих значений, и, конечно, он может снова изменить его в любое время. Но ваш вопрос может быть удовлетворен и без этого свойства: вам просто нужно будет делать text.replace(',', '.')
каждый раз, когда данный текст находится в stringToValue
, прежде чем анализировать его в double
.
person
gthanop
schedule
06.01.2020