zahl = Integer.valueOf(nurintTField.getText());
jtextfield.setInputVerifier(new NumberInputVerifier());
class NumberInputVerifier extends InputVerifier {
public NumberInputVerifier() {
super();
}
@Override
public boolean verify(JComponent input) {
JTextField jtf = (JTextField) input;
boolean ans = false;
String toWorkWith = jtf.getText();
toWorkWith = toWorkWith.replaceAll(",", "."); // Alle Kommas zu Punkten
ans = checkIfNumber(toWorkWith);
return ans;
}
@Override
public boolean shouldYieldFocus(JComponent input) {
if (!verify(input)) {
input.setForeground(Color.RED);
return false;
} else {
input.setForeground(Color.BLACK);
return true;
}
}
private boolean checkIfNumber(String toWorkWith) {
String[] split = toWorkWith.split("\\.");
System.out.println(split.length);
if (split.length > 2) {
return false;
}
boolean ans = true;
for (int j = 0; j < toWorkWith.length(); j++) {
char c = toWorkWith.charAt(j);
switch (c) {
case '0':
break;
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
break;
case '6':
break;
case '7':
break;
case '8':
break;
case '9':
break;
case '.':
break;
default:
ans = false;
break;
}
}
return ans;
}
}
SpinnerNumberModel spinnerModel = new SpinnerNumberModel(0, 0, 65535, 1);
JSpinner spinner = new JSpinner(spinnerModel);
spinner.setEditor(new JSpinner.NumberEditor((JSpinner) valueComponent, "0"));