2011-12-22 3 views
0

숫자 데이터가있는 JFormattedTextdields가 여러 개 있습니다. 나는 DecimalFormat, InternationalFormatter, DocumentListener를 사용했으며 CaretPositionListener로 시도했다.Java : JFormattedTextField가 focusLost 및 focusGained에서 setGroupingUsed()를 변경했습니다.

유일한 문제는 숫자 입력이 커지고 문자를 그룹화 할 때 캐럿 위치입니다.

focusGained & onFocusLost에서 각 jformattedtextfields의 DecimalFormat의 setGroupingUsed()를 어떻게 동적으로 설정할 수 있습니까?

어떤 아이디어 나 제안 ....

UPDATE 코드 & 문제 : I 입력 "12345"을 시도, 추가의 "1234"쉼표 "1234"를 나타납니다.

DecimalFormat numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance(); 
    numberFormat.setMaximumFractionDigits(2); 
    numberFormat.setMinimumFractionDigits(2); 
    numberFormat.setRoundingMode(RoundingMode.HALF_UP); 

    final InternationalFormatter formatter = new InternationalFormatter(numberFormat); 
    formatter.setAllowsInvalid(false); 
    formatter.setMinimum(0.00); 
    formatter.setMaximum(999999999.99); 

    return formatter; 

이 내가 정의의 JFormattedTextField에 솔루션로 구현 한 것입니다 : 이것은 내가 사용하는 서식 코드 4. 후 4 & 세 사이의 대신을 캐럿을 제공합니다. 그래서 그냥 caret의 적절한 유지, 값을 입력하는 동안 그룹화 문자를 처리 할 수있는 더 좋은 방법이 있나요 :

public void focusGained(FocusEvent e) { 
    if (numberFormat.isGroupingUsed()) { 
     Object o = this.getValue(); 
     numberFormat.setGroupingUsed(false); 
     formatter.setFormat(numberFormat); 
     this.setFormatterFactory(new AbstractFormatterFactoryImpl()); 
     this.setValue(o); 
     this.setText(o.toString()); 
    } 
} 

public void focusLost(FocusEvent e) { 
    try { 
     this.commitEdit(); 
    } catch (ParseException ex) { 
     //Logger.getLogger(NumberFormattedTextField.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    Object o = this.getValue(); 
    //System.out.println("focusLost : getValue = " + o); 
    numberFormat.setGroupingUsed(true); 
    formatter.setFormat(numberFormat); 
    this.setFormatterFactory(new AbstractFormatterFactoryImpl()); 
    this.setValue(o); 
    this.setText(o.toString()); 
    //System.out.println("focusLost : Text Set = " + o.toString()); 
} 
+3

더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. –

답변

0

감사합니다,

내가 필요한 모든 것을 관리하는 사용자 정의 텍스트 필드를 만들어 해결했다. 개선을위한 제안. 위에 코드가 추가되었습니다.

+0

각 자동 완성 된 JComboBox/JtexField에는 set & moveCaret가 구현되어 있습니다. JTextField에 대해서는 아무 것도 없지만 JComboBox – mKorbel

+0

@mKorbel에서 JTextComponent를 추출하면 텍스트를 입력하는 동안 문제가 발생했습니다. ","가 그룹화되어 있기 때문에 캐럿은 내부가 1 자리수였습니다. focusGained 동안 그룹화를 false로 설정하고 focusLost 동안 true로 복원했습니다. 이것은 나를 도왔다. 내가 말하는 것보다 더 나은 해결책이 있습니까? – Tvd

+0

AutoCompleted JComboBox/JtexField를 검사하면 50pcs를 넣을 수 있으며 Selection을 사용하여 마지막 Carret을 기억하지만 다른 손에는 코드가없는 hight 레벨 workaound에 대해 말하기 때문에 질문에 대답하기가 너무 어렵습니다. 여기에 몇 가지 훌륭한 스윙이 있습니다. 코드 작성자, 코드는 없지만 대답은 :-) – mKorbel