) 사용자가 계산할 Jtextfield에 Double 값을 입력해야한다고 가정 해 보겠습니다.
그러나 사용자가 갑자기 1보다 많은 기간을 사용하면 NumberFormatException이 트리거되므로 문서 필터를 사용하여 추가 기간을 필터링하거나 예외를 catch하고 잘못된 입력을 사용자에게 알리는 것으로 가정합니다.문서 필터를 사용하여 여러 마침표 필터링 (
단지 숫자와 기간을 허용하도록하는 DocumentFilter를 사용하여 Currenty하지만 내 문제는 두 번째 기간
PlainDocument filter = new PlainDocument();
filter.setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int off, String str, AttributeSet attr)
throws BadLocationException
{
fb.insertString(off, str.replaceAll("[^0-9.]", ""), attr);
}
@Override
public void replace(FilterBypass fb, int off, int len, String str, AttributeSet attr)
throws BadLocationException
{
fb.replace(off, len, str.replaceAll("[^0-9.]", ""), attr);
}
});
apm.setDocument(filter);
예
잘못된 입력을 필터링하는 방법입니다
유효 INPUT : 1.22
더 나은 도움을 얻기 위해 나에게 도움이되는 아이디어가 없다. [SSCCE] (http://sscce.org/), 짧고 실행 가능한 컴파일 가능 – mKorbel
나는 두 번째 마침표를 허용하지 않기 위해 필터를 작성한다. . 너 뭐 해봤 니? 어떻게 작동하지 않습니까? 그리고 안녕하세요 @ mKorbel! –
나 자신, 나는 기다릴거야. –