2016-04-01 9 views
1

GridFane内にTextFieldを持っていて、ユーザータイプのコンテンツをIntegerに読み込んで複数の(非ダブル)計算に使用することができます。JavaFX TextFieldの内容を整数に変換する

TextField userInput = new TextField(); 

userInput.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> { 
    if (!newValue.matches("\\d*")) { 
     userInput.setText(newValue.replaceAll("[^\\d]", "")); 
    } 
}); 

sortButton.setOnMousePressed(e -> { 
    int savedValue = Integer.parseInt(userInput); 
    int outputMath = savedValue * 3; 
    int outputExpo = savedValue * 10; 
    int outputQuad = savedValue * 4; 
}); 

リスナーは数字が、必ず何が受け入れられないます作るの偉大な仕事をしていませんが、これらの数字は、私が計算に使用することはできていないようだという文字列として読み込まれます。

問題の子は、Integer.parseInt(userInput)がここでそのトリックをしていないようです。何かアドバイス?この行で

+0

ただし、Integer.valueOf()などの他の方法を試すこともできます。 – Elltz

答えて

1

:あなたが実際にやっていること

int savedValue = Integer.parseInt(userInput); 

があなたの代わりにTextFieldtextPropertyの内容を解析する必要がありますTextField.

あるuserInput変数を解析しようとすることです。

int savedValue = Integer.parseInt(userInput.getText()); 
関連する問題