package javafx_validatenumber; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; /** * @web http://java-buddy.blogspot.com * * Validate and parse JavaFX TextField to various number, * Integer, Short, Long, Byte, Float, Double */ public class JavaFX_ValidateNumber extends Application { @Override public void start(Stage primaryStage) { Label labelInfo = new Label(); labelInfo.setText( "java.version: " + System.getProperty("java.version") + "\n" + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version") ); TextField textIn = new TextField(); Label labelAnswer = new Label(); Button btnIsInteger = new Button("is Integer?"); btnIsInteger.setOnAction((ActionEvent event) -> { try{ Integer i = Integer.parseInt(textIn.getText()); labelAnswer.setText("Is Integer: " + i); }catch (NumberFormatException e){ labelAnswer.setText("Not Integer!"); } }); Button btnIsShort = new Button("is Short?"); btnIsShort.setOnAction((ActionEvent event) -> { try{ Short i = Short.parseShort(textIn.getText()); labelAnswer.setText("Is Short: " + i); }catch (NumberFormatException e){ labelAnswer.setText("Not Short!"); } }); Button btnIsLong = new Button("is Long?"); btnIsLong.setOnAction((ActionEvent event) -> { try{ Long i = Long.parseLong(textIn.getText()); labelAnswer.setText("Is Long: " + i); }catch (NumberFormatException e){ labelAnswer.setText("Not Long!"); } }); Button btnIsByte = new Button("is Byte?"); btnIsByte.setOnAction((ActionEvent event) -> { try{ Byte i = Byte.parseByte(textIn.getText()); labelAnswer.setText("Is Byte: " + i); }catch (NumberFormatException e){ labelAnswer.setText("Not Byte!"); } }); Button btnIsFloat = new Button("is Float?"); btnIsFloat.setOnAction((ActionEvent event) -> { try{ Float i = Float.parseFloat(textIn.getText()); labelAnswer.setText("Is Float: " + i); }catch (NumberFormatException e){ labelAnswer.setText("Not Float!"); } }); Button btnIsDouble = new Button("is Double?"); btnIsDouble.setOnAction((ActionEvent event) -> { try{ Double i = Double.parseDouble(textIn.getText()); labelAnswer.setText("Is Double: " + i); }catch (NumberFormatException e){ labelAnswer.setText("Not Double!"); } }); VBox vBox = new VBox(); vBox.getChildren().addAll(labelInfo, textIn, btnIsInteger, btnIsShort, btnIsLong, btnIsByte, btnIsFloat, btnIsDouble, labelAnswer); StackPane root = new StackPane(); root.getChildren().add(vBox); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }
Saturday, July 18, 2015
Validate and parse JavaFX TextField to various number
Example to Validate and parse JavaFX TextField to various number: Integer, Short, Long, Byte, Float, Double.
No comments:
Post a Comment