А как насчет JFormattedTextField
с AbstractFormatter, выполняющим преобразование, и DocumentFilter для отклонения всего, что не является допустимым процентным значением?
Вот пример DocumentFilter (не проверен, из чтения документации):
class PercentageFilter extends DocumentFilter {
insertString(FilterBypass bp, int offset, String adding, AttributeSet attrs) {
Document doc = bp.getDocument();
String text = doc.getText(0, offset) + adding + doc.getText(offset, doc.getLength()-offset);
try {
double d = Double.parseDouble(text);
if(d < 0 || 100 < d) {
// to big or too small number
return;
}
}
catch(NumberFormatException ex) {
// invalid number, do nothing.
return;
}
// if we come to this point, the entered number
// is a valid value => really insert it into the document.
bp.insertString(offset, adding, attrs);
}
}
Вы хотели бы переопределить remove()
и replace
аналогичным образом.
(Я полагаю, что может быть более эффективная реализация, но это будет достаточно быстро для большинства пользователей, набирающих скорость.)
Этот фильтр будет возвращен методом getDocumentFilter
вашей реализации AbstractFormatter.