Hej. Prośba chciałbym zmodyfikować poniższy kod tak aby TextField przyjmował tylko Stringi. Teraz karze wpisać tylko cyfry inaczej wyświetla komunikat. Chciałbym żeby było odwrotnie :-) Nijak nie mogę tego ogarnąć :-)
public class TextFieldWithCheckingAValue extends Application
{
Stage blue;
Button red;
Scene verde;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception
{
blue = primaryStage;
blue.setTitle("Weclome");
blue.setWidth(400);
blue.setHeight(400);
Label ela = new Label();
ela.setText("Please Enter Your Age");
TextField ala = new TextField();
ala.setPromptText("Your Age Please");
red = new Button("Check");
red.setOnAction(event -> answer(ala, ala.getText()) );
VBox aga = new VBox(10);
aga.setPadding(new Insets(30,30,30,30));
aga.getChildren().addAll(ela,ala,red);
Scene verde = new Scene(aga);
blue.setScene(verde);
blue.show();
}
public boolean answer (TextField ale, String message)
{
try
{
int a = Integer.parseInt(ale.getText());
System.out.println("User age is : \n" + message);
return true;
}
catch (NumberFormatException e)
{
System.out.println("Sorry but \n"+message+"\n"+"is not a number\n");
return false;
}
}
}