2016-07-15 5 views
2

getValue()関数がいつもnullを返す理由を誰かに説明することはできますか? getValue()の代わりにgetText()を使用すると、すべてが完璧ですが、私が欲しいのは値でありテキストではありません。私はそれを整数に変換することができますが、これが最良の方法だとは思いません。JFormattedTextFieldのKeyListener

public class Test {  
    public static void main(String[] args) { 

     JFrame jf = new JFrame("test jftf"); 
     jf.setSize(100, 100); 
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     jf.setVisible(true); 
     JFormattedTextField jftf = new JFormattedTextField(NumberFormat.getIntegerInstance()); 
     jftf.addKeyListener(new KeyListener() { 
      @Override 
      public void keyTyped(KeyEvent e) { 
      } 

      @Override 
      public void keyPressed(KeyEvent e) { 
      } 

      @Override 
      public void keyReleased(KeyEvent e) { 
       System.out.println(jftf.getValue()); 
      } 

     }); 
     jf.add(jftf); 
    } 

} 

答えて

1

あなたはgetValue()方法に依存している場合は、現在の編集内容がコミットされていることを確認する必要があります。あなたがこの方法commitEdit()呼び出すことによってこれを行うことができそうでない場合は、あなたが期待している値を取得できない場合があります。

@Override 
public void keyReleased(KeyEvent e) { 
    //Forces the current value to be taken from the AbstractFormatter 
    //and set as the current value: 
    try { 
     jftf.commitEdit(); 
    } catch (ParseException e1) { 
     //Handle possible parsing error... 
    } 
    System.out.println(jftf.getValue()); 
} 
+0

答えをくれてありがとうございますが、まず、私はこの試合をキャッチする必要があります。次に、このcommitEdit()関数が必要なのは分かりません。私はActionListenerを使用するとき、私はこの問題はありません。 – ardayigit

+0

例外を処理するように更新されました。私は 'ActionListener'でコミットせずに動作する理由を理解していません。実装に違いがあると仮定することができます。 – StuPointerException

+0

どうもありがとうございました。少なくともコードは動作しています:) – ardayigit

0

これは、値の名前が正確ではありませんどのように機能するかを示すために、一例です。 このように考えてください。 JFormattedTextFieldには、整数を保持するlastValidIntというフィールドがあります。 フィールド内のテキストがフォーマットされるたびに、プログラムはフィールド内のテキストが数字かどうかをチェックします。そうであれば、lastValidIntはその文字列に設定されます。

lastValidIntが初期化されていて、それが持っているならば、それはlastValidIntの値を返す場合getValue()方法を確認します。それがなければヌルを返します。

例: これらは、毎回アプリケーションを再起動していることを前提としています。 フィールドに入力する内容を入力します。 出力は、何かを入力した後にコンソールが印刷するものです。

1: 
Input: a 
Ouput: null 
//a is not a number therefore the output is null 

2: 
Input: 1 
Output: 1 
//1 is a number therefore the output is 1 

3: 
Input1: a 
Ouput: null 
Input2: as 
Ouput: null 
Input3: asd 
Ouput: null 
Input4: asdf 
Ouput: null 
//a is not a number therefore the output is null 

4: 
Input1: 1 
Ouput: 1 
Input2: 1f 
Ouput: 1 
//The first thing the was read here was 1 therefore lastValidInt was set to 1 then the next input is 1f which is not a number therefore lastValidInt remained 1 

5: 
Input1: f 
Ouput: null 
Input2: f1 
Ouput: null 
//f is not a number therefore output is null, f1 is not a number either since it has a character that is not a digit and therefore lastValidInt has not been initialized 

6: 
Input1: f 
Ouput: null 
Input2: 
Output: null 
Input3: 1 
Output: 1 
Input4: 1f 
Output: 1 
//f is not a number therefore output is null, "" is also not a number therefore output is null again then the only thing that exists in the field is 1 which is a number therefore output is 1, finally by adding a f the field is no longer a number so output is same as before. 

7: 
Input1: f 
Ouput: null 
Input2: 1f (moved back to the begining without erasing f) 
Output: null 
//f is not a number therefore output is null, 1f is not a number either since it has a character that is not a digit and therefore lastValidInt has not been initialized 
+0

ちょっと正確なことができますか?たとえば "4"キーを入力すると、getValue()はnullを返しますか?それは数値なので、nullを返すべきではありません。 – ardayigit

+0

@ardayigitあなたが行って、あなたのためのいくつかの例を作った –

+0

実際には、入力が1のとき、出力はnullです... – ardayigit