2011-03-05 7 views

답변

6

개체에 setFormatterFactory(JFormattedTextField.AbstractFormatterFactory)을 호출 할 수 있습니다.

당신은 이런 식으로 사용할 수 있습니다 :

// Define the number factory. 
NumberFormat nf = NumberFormat.getIntegerInstance(); // Specify specific format here. 
NumberFormatter nff = new NumberFormatter(nf); 
DefaultFormatterFactory factory = new DefaultFormatterFactory(nff); 

// Define the decimal factory. 
DecimalFormat df = new DecimalFormat(); // And here.. 
NumberFormatter dnff = new NumberFormatter(df); 
DefaultFormatterFactory factory2 = new DefaultFormatterFactory(dnff); 

// Then you set which factory to use. 
JFormattedTextField field = new JFormattedTextField(); 
field.setFormatterFactory(factory); 
//field.setFormatterFactory(factory2);   

이벤트가 발생하면 그러니 그냥 공장을 설정합니다.

DefaultFormatterFactory의 생성자는 여러 포맷터를 사용할 수 있습니다. 디폴트 값, 포커스가 없을 때의 디스플레이 포맷, 포커스가있을 때의 편집 포맷, 그리고 필드가 널값을 가질 때의 널 포맷이있다.

+0

권장하기 전에 JFormattedTextField 인스턴스에서 setFormatter를 직접 호출하려고 시도 했습니까? 그게 보호 된 방법이라는 것을 알지? –

+0

setFormatter()가 보호되고 있다는 사실을 놓쳤습니다. –

+0

@netrom Formatter와 Format은 실제로 같지 않습니다. 포맷터 팩토리를 설정할 수는 있지만 필드의 형식이 어떻게 변경되는지는 알 수 없습니다. 어쩌면 나는 뭔가를 놓치고있다. 예제가 있다면 정말보고 싶습니다. 감사. – m0s

1

example은 을 사용하여 기본 형식을 변형합니다. Date 용으로 설계되었지만 접근 방식을 Number에 맞게 조정할 수 있습니다.

관련 문제